保存或更新SQL数据库的按钮

huangapple go评论64阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年6月21日 22:46:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76524569.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定