使用复选框选中的值更新数据库中的特定行

时间:2016-08-04 15:31:36

标签: c# asp.net sql-server stored-procedures

我有一个网络应用程序,它根据用户输入生成条形码图像。 用户选择存储在数据库中的值,然后显示在数据绑定复选框上。我的过程在数据库中插入用户输入值没有问题,但它在数据库中创建新数据。我想弄清楚的是,如何确定一个Where子句,无论是在我的存储过程中还是在我的代码中,选择哪个数据绑定复选框都会更新数据库中的特定行,而不是创建一个新排。请注意,它只是一个复选框而不是复选框列表。

protected void gen_barcode(object sender, EventArgs e)
{
    int n;
    int i = Int32.Parse(amount.Text);

    string date_picker = datepicker.Text;

    SqlConnection conn = new SqlConnection(GetConnectionString());

    SqlCommand cmd = new SqlCommand();
    cmd.Connection = conn;

    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = "barcode_insert"
    cmd.Parameters.AddWithValue("@Amount", amount.Text);
    cmd.Parameters.AddWithValue("@Date", datepicker);

    if (CheckBox_Code.Checked)
    {
          //generate image code
    }

    cmd.Connection.Open();
    cmd.ExecuteNonQuery();
    conn.Close();
}

复选框绑定

public void CheckBoxBind()
{
    SqlConnection conn = new SqlConnection(GetConnection());

    using (SqlCommand cmd = new SqlCommand())
    {

       cmd.CommandText = "Select CheckBoxCode FROM CodeTable Where CheckBoxCode LIKE 'EX'"
       cmd.Connection = conn;
       conn.Open();

       using (SqlDataReader rdr = cmd. ExecuteReader())
       {
          while (rdr.Read()
          {
             CheckBox_Code.Text = rdr["CheckBoxCode"].ToString()
             cmd.Parameters.AddWithValue("@CheckBoxCode", CheckBox_Code)
          }
       }

       conn.Close();
    }

} 

1 个答案:

答案 0 :(得分:0)

CheckBoxBind()的第6行需要在select语句中使用参数名称:

cmd.CommandText = "Select CheckBoxCode FROM CodeTable Where CheckBoxCode = @CheckBoxCode"