I am using the following code in the button click event to update the values. If the row does not exist yet, I want to insert a new row into the table. But the code does not execute when I click on the button. Both the stored procedures are just regular insert and update SQL statements.
Any thoughts? Thank you so much!
SqlConnection con = new SqlConnection(conn.GetConnectionString());
SqlCommand cmdnew = new SqlCommand();
cmdnew.CommandType = CommandType.StoredProcedure;
cmdnew.Connection = con;
cmdnew.CommandText = "dbo.UpdateMagtoSpec";
cmdnew.Parameters.AddWithValue("@SpecNo", DropDownList1.SelectedText);
cmdnew.Parameters.AddWithValue("@TestID", ddl_testclass.SelectedValue);
cmdnew.Parameters.AddWithValue("@Max", TextBox6.Text);
cmdnew.Parameters.AddWithValue("@Typical", TextBox8.Text);
cmdnew.Parameters.AddWithValue("@Min", TextBox7.Text);
cmdnew.Parameters.AddWithValue("@Comments", TextArea2.Text);
cmdnew.Parameters.AddWithValue("@Unit", ddl_units.SelectedText);
con.Open();
SqlDataReader rdr = null;
rdr = cmdnew.ExecuteReader();
if (rdr.HasRows)
{
try
{
cmdnew.ExecuteNonQuery();
Alert.Show("Changes Saved!", MessageBoxIcon.Information);
btn_edit.Hidden = false;
Button1.Hidden = true;
Button2.Hidden = true;
TextBox6.Readonly = true;
TextBox7.Readonly = true;
TextBox8.Readonly = true;
ddl_units.Readonly = true;
TextArea2.Readonly = true;
}
catch (Exception ex)
{
if (ex.Message.ToString().Contains("Error"))
{
Alert.Show("Modification Failed!", MessageBoxIcon.Information);
}
}
con.Close();
}
else
{
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = con;
cmd.CommandText = "dbo.InsertMagtoSpec";
cmd.Parameters.AddWithValue("@SpecNo", DropDownList1.SelectedText);
cmd.Parameters.AddWithValue("@TestID", ddl_testclass.SelectedValue);
cmd.Parameters.AddWithValue("@Max", TextBox6.Text);
cmd.Parameters.AddWithValue("@Typical", TextBox8.Text);
cmd.Parameters.AddWithValue("@Min", TextBox7.Text);
cmd.Parameters.AddWithValue("@Comments", TextArea2.Text);
cmd.Parameters.AddWithValue("@Unit", ddl_units.SelectedText);
try
{
cmd.ExecuteNonQuery();
Alert.Show("Records Saved!", MessageBoxIcon.Information);
}
catch (Exception ex)
{
if (ex.Message.ToString().Contains("Error"))
{
Alert.Show("Modification Failed!", MessageBoxIcon.Information);
}
}
con.Close();
}
Show us your aspx code with the button click declaration. Most probably you have to wire the code to the event.
You need both parts:
<asp:Button id="Button1"
Text="Click here for greeting..."
OnClick="GreetingBtn_Click"
runat="server"/>
And
void GreetingBtn_Click(Object sender,
EventArgs e)
{
// When the button is clicked,
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.