2011-07-15 38 views
0

請檢查下面的C#代碼,並讓我知道我出錯的地方。下面是我遇到:SQL Server中的C#CheckBoxList更新

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(); 
} 
+0

你一直在這裏已經足夠長,知道你應該接受你的問題的答案... – JNK

+0

我已經問了多次如何做到這一點,沒有人會告訴我。 –

+0

選中正確答案下面的複選框。另見這裏:http://stackoverflow.com/faq#howtoask – JNK

回答

1

看起來問題來自Button1單擊事件處理程序中的if塊。結果是隻有選中的複選框纔將值保存到數據庫中。

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

您可以刪除if語句並保留所有值或添加邏輯以僅保留已更改的值。