Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Stored Procedure fired from C# Code-Behind not working on UPDATE

I have a stored procedure called from a C# code-behind. The code fires but the update command does not get performed. The stored procedure, if run directly, works. I think I am having a brain fart. Please help.

Code:

protected void btnAbout_Click(object sender, EventArgs e)
{
    SqlConnection myConnection = new SqlConnection(strConnection);
    SqlCommand myCommand = new SqlCommand("spUpdateCMSAbout", myConnection);
    myConnection.Open();
    myCommand.CommandType = CommandType.StoredProcedure;
    myCommand.Parameters.Add("@AboutText", SqlDbType.NVarChar, -1).Value = txtAbout.Text.ToString();
    myCommand.ExecuteNonQuery();
    myConnection.Close();
}

Stored procedure:

ALTER PROCEDURE fstage.spUpdateCMSAbout
(
   @AboutText nvarchar(max)
)
AS
BEGIN
SET NOCOUNT ON;
    UPDATE fstage.staticCMS SET About = @AboutText;
END

Markup:

<asp:Button ID="btnAbout" runat="server" 
    Text="Save" CausesValidation="False" onclick="btnAbout_Click" 
    UseSubmitBehavior="False" />

C# .NET 4.0

like image 440
CSSHell Avatar asked May 22 '26 21:05

CSSHell


1 Answers

Your code should work. However you can simplify it a little and make more robust:

using(var myConnection = new SqlConnection(strConnection))
using(var myCommand = myConnection.CreateCommand())
{
    myCommand.CommandText = "spUpdateCMSAbout";
    myCommand.CommandType = CommandType.StoredProcedure;
    myCommand.Parameters.AddWithValue("AboutText", txtAbout.Text);
    myConnection.Open();
    myCommand.ExecuteNonQuery();
}

If it does not, try bypassing your stored proc by calling UPDATE directly:

using (var myConnection = new SqlConnection(strConnection))
using (var myCommand = myConnection.CreateCommand())
{
    myCommand.CommandText = "UPDATE fstage.staticCMS SET About = @AboutText";
    myCommand.Parameters.AddWithValue("AboutText", txtAbout.Text);
    myConnection.Open();
    myCommand.ExecuteNonQuery();
}

If it does not work, than it is likely UPDATE fstage.staticCMS SET About = 'something' does not do anything because of some kind of trigger.

like image 172
Alex Aza Avatar answered May 24 '26 11:05

Alex Aza