英文:
button to save or update sql database
问题
以下是您提供的代码的中文翻译部分:
protected void Button1_Click(object sender, EventArgs e)
{
if (DropDownList1.SelectedValue == "" || DropDownList2.SelectedValue == "" || TextBox1.Text == "")
{
Label1.Text = "请填写所有字段";
}
if (DropDownList5.SelectedValue != null)
{
SqlConnection con = new SqlConnection("Data Source=ucpdapps2;Initial Catalog=OnCallWeb;Integrated Security=True");
con.Open();
SqlCommand comm = new SqlCommand("更新 Dispatcher_Roles 设置 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('成功更新');", true);
ClearAllData();
}
else
{
SqlConnection con = new SqlConnection("Data Source=ucpdapps2;Initial Catalog=OnCallWeb;Integrated Security=True");
con.Open();
SqlCommand comm = new SqlCommand("插入到 Dispatcher_Roles 中的值 ('" + DropDownList1.SelectedValue + "','" + DropDownList2.SelectedValue + "','" + TextBox1.Text + "','" + DropDownList3.SelectedValue + "','" + DropDownList4.SelectedValue + "')", con);
comm.ExecuteNonQuery();
con.Close();
ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('成功添加');", true);
ClearAllData();
Label1.Text = "";
}
}
请注意,代码中的注释和标签部分未进行翻译。如果您需要对其进行翻译,请提供具体的文本,我将帮助您翻译。
英文:
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 = "";
}
}
答案1
得分: 0
VDWWD的评论解决了我的问题。
英文:
VDWWD comment resolved my issue.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论