I have a button and I want it to save data to the sql database if Dropdownlist5 has no value in it, but if Dropdownlist 5 has a value in it, I want the button to update the existing data in sql. The problem is when Dropdownlist 5 has a value selected it just creates a new entry in the sql database and does not update the existing entry in sql.
protected void Button1_Click(object sender, EventArgs e)
{
if (DropDownList1.SelectedValue == "" || DropDownList2.SelectedValue == "" || TextBox1.Text == "")
{
Label1.Text = "Fill In All Fields";
}
if (DropDownList5.SelectedValue !=null)
{
SqlConnection con = new SqlConnection("Data Source=ucpdapps2;Initial Catalog=OnCallWeb;Integrated Security=True");
con.Open();
SqlCommand comm = new SqlCommand("Update Dispatcher_Roles set Name = '" + DropDownList1.SelectedValue + "',Position = '" + DropDownList2.SelectedValue + "',Roles = '" + TextBox1.Text + "',Status = '" + DropDownList3.SelectedValue + "',DispatcherCovering = '" + DropDownList4.SelectedValue + "' where Name='" + DropDownList1.SelectedValue + "'", con);
comm.ExecuteNonQuery();
con.Close();
ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Successfully Updated');", true);
ClearAllData();
}
else
{
SqlConnection con = new SqlConnection("Data Source=ucpdapps2;Initial Catalog=OnCallWeb;Integrated Security=True");
con.Open();
SqlCommand comm = new SqlCommand("Insert into Dispatcher_Roles values ('" + DropDownList1.SelectedValue + "','" + DropDownList2.SelectedValue + "','" + TextBox1.Text + "','" + DropDownList3.SelectedValue + "','" + DropDownList4.SelectedValue + "')", con);
comm.ExecuteNonQuery();
con.Close();
ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Successfully Added');", true);
ClearAllData();
Label1.Text = "";
}
}
VDWWD comment resolved my issue.