【问题标题】:Update Specific Row in Database using Check Box Checked Value使用复选框选中值更新数据库中的特定行
【发布时间】:2016-08-04 15:31:36
【问题描述】:

我有一个基于用户输入生成条形码图像的网络应用程序。 用户选择存储在数据库中的值,然后显示在数据绑定复选框上。我的过程在数据库中插入用户输入值没有问题,但它在数据库中创建了新数据。我想弄清楚的是,如何在某种意义上确定 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();
    }

} 

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 实际上,我的初始代码不正确,它实际上应该是 Where CheckBoxCode LIKE 'EX' 所以使用它当然会产生声明错误。我将编辑我的代码以反映这一点。
    猜你喜欢
    • 2013-11-07
    • 1970-01-01
    • 2018-01-29
    • 2015-09-18
    • 1970-01-01
    • 2018-10-26
    • 2014-11-27
    • 2014-10-20
    • 2021-12-17
    相关资源
    最近更新 更多