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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With