SQL Server中c# CheckBoxList的更新

本文关键字:更新 CheckBoxList Server SQL | 更新日期: 2023-09-27 18:02:07

请检查下面我的c#代码,让我知道我哪里出错了。以下是我的经历:

1)。如果我清空SQL Server中的SendReport列并加载页面,则SendReport的第二行将自动填充为1。

2)。我可以放置一个复选标记,单击按钮和SendReport值成功填充在SQL Server。但是,如果我取消选中其中任何一个并单击按钮,则没有一个值从1变为0。请帮助!

$<asp:CheckBoxList ID="CheckBoxList1" runat="server">
</asp:CheckBoxList>
<br />
<asp:Button ID="Button1" runat="server" Text="Save Changes" OnClick="Button1_Click" />
<br />

BACKPAGE:
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        BindCheckBoxList();
    }
}
//  Setting up the ConnectionString
public string GetConnectionString()
{
    return System.Configuration.ConfigurationManager.ConnectionStrings["IPdataConnectionString"].ConnectionString;
}
//  Binding the CheckBoxList with Data
private void BindCheckBoxList()
{
  DataTable dt = new DataTable();
  SqlConnection connection = new SqlConnection(GetConnectionString());
  try
  {
    connection.Open();
    string sqlStatement = "SELECT Partner, ID, SendReport FROM Rorts";
    SqlCommand sqlCmd = new SqlCommand(sqlStatement, connection);
    SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
    sqlDa.Fill(dt);
    if (dt.Rows.Count > 0)
    {
      CheckBoxList1.DataSource = dt;
      CheckBoxList1.DataTextField = "Partner"; // the items to be displayed in the list items
      CheckBoxList1.DataValueField = "SendReport"; // the id of the items displayed
      CheckBoxList1.DataBind();
      //Setting the Selected Items in the ChecBoxList based from the value in the database
      //to do this, lets iterate to each items in the list
      for (int i = 0; i < dt.Rows.Count; i++)
      {
        if (!string.IsNullOrEmpty(dt.Rows[i]["SendReport"].ToString()))
        {
          CheckBoxList1.Items[i].Selected = Convert.ToBoolean(dt.Rows[i]["SendReport"]);
        }
      }
    }
  }
  catch (System.Data.SqlClient.SqlException ex)
  {
    string msg = "Fetch Error:";
    msg += ex.Message;
    throw new Exception(msg);
  }
  finally
  {
    connection.Close();
  }
}
//  Creating the Method for Saving the CheckBoxList Selected Items to the database
private void Update(string name, bool SendReport)
{
  SqlConnection connection = new SqlConnection(GetConnectionString());
  SqlCommand cmd;
  string sqlStatement = string.Empty;
    try
    {
      // open the Sql connection
      connection.Open();
      sqlStatement = "UPDATE Rorts SET SendReport = @SendReport WHERE Partner = @Partner";
      cmd = new SqlCommand(sqlStatement, connection);
      cmd.Parameters.AddWithValue("@Partner", name);
      cmd.Parameters.AddWithValue("@SendReport", SendReport);
      cmd.CommandType = CommandType.Text;
      cmd.ExecuteNonQuery();
    }
    catch (System.Data.SqlClient.SqlException ex)
    {
      string msg = "Insert/Update Error:";
      msg += ex.Message;
      throw new Exception(msg);
    }
    finally
    {
      // close the Sql Connection
      connection.Close();
    }
  }
//  Calling the Method for Saving the state of  CheckBoxList  selected items
protected void Button1_Click(object sender, EventArgs e)
{
    string PartnerName = string.Empty;
  for (int i = 0; i < CheckBoxList1.Items.Count; i++)
  {
    if (CheckBoxList1.Items[i].Selected)
    {
        PartnerName = CheckBoxList1.Items[i].Text;
        Update(PartnerName, CheckBoxList1.Items[i].Selected);
    }
  }
  //ReBind the List to retain the selected items on postbacks
  BindCheckBoxList();
}

SQL Server中c# CheckBoxList的更新

看起来问题是由于下面的if块从Button1单击事件处理程序。结果是,只有选中的复选框才会将值持久化到数据库中。

        if (CheckBoxList1.Items[i].Selected)
        {
            PartnerName = CheckBoxList1.Items[i].Text;
            Update(PartnerName, CheckBoxList1.Items[i].Selected);
        }

您可以删除if语句并保留所有值,或者添加逻辑以仅保留那些已更改的值