dkirk
09-05-2008, 10:54 AM
alright gang it's me again, hairless and bug eyed.... I have built the simple page below and it does not work. If a checkbox is checked programmatically when the page loads, when the user unchecks it and trys to update the db, the checkbox selected value is still set to true. As you can see, I have tried to set the checkbox programmtically three different ways but it all works the same. What am I doing wrong?
aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:HiddenField ID="idHiddenField" runat="server" />
<div>
<asp:CheckBoxList ID="newsCheckBoxList" runat="server" RepeatColumns="4" >
<asp:ListItem Value="grinding" Text="Grinding" />
<asp:ListItem Value="sales" Text="Sales" />
<asp:ListItem Value="service" Text="Service" />
<asp:ListItem Value="spareParts" Text="Spare Parts" />
<asp:ListItem Value="tooling" Text="Tooling" />
<asp:ListItem Value="training" Text="Training" />
</asp:CheckBoxList>
<br />
<asp:Label ID="confirmLabel" runat="server" ForeColor="Red" />
<br />
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</div>
</form>
</body>
</html>
code behind:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string connectionString = ConfigurationManager.ConnectionStrings["contacts"].ConnectionString;
SqlDataReader reader;
SqlConnection conn = new SqlConnection(connectionString);
SqlCommand comm = new SqlCommand("SELECT ID, sales, service, spareParts, tooling, grinding, training " +
"FROM contactsMain WHERE ID = 100", conn);
try
{
conn.Open();
reader = comm.ExecuteReader();
while (reader.Read())
{
idHiddenField.Value = reader["ID"].ToString();
newsCheckBoxList.ClearSelection();
if (reader["sales"].ToString() == "True")
{
ListItem sale = newsCheckBoxList.Items.FindByValue("sales");if (sale != null)
{
sale.Selected = true;
}
//newsCheckBoxList.Items.FindByValue("sales").Selected = true;
//newsCheckBoxList.Items[1].Selected = true;
}
if (reader["service"].ToString() == "True")
{
ListItem serv = newsCheckBoxList.Items.FindByValue("service");if (serv != null)
{
serv.Selected = true;
}
//newsCheckBoxList.Items.FindByValue("service").Selected = true;
//newsCheckBoxList.Items[2].Selected = true;
}
if (reader["spareParts"].ToString() == "True")
{
ListItem spare = newsCheckBoxList.Items.FindByValue("spareParts");if (spare != null)
{
spare.Selected = true;
}
//newsCheckBoxList.Items.FindByValue("spareParts").Selected = true;
//newsCheckBoxList.Items[3].Selected = true;
}
if (reader["tooling"].ToString() == "True")
{
ListItem tool = newsCheckBoxList.Items.FindByValue("tooling");if (tool != null)
{
tool.Selected = true;
}
//newsCheckBoxList.Items.FindByValue("tooling").Selected = true;
//newsCheckBoxList.Items[4].Selected = true;
}
if (reader["grinding"].ToString() == "True")
{
ListItem grind = newsCheckBoxList.Items.FindByValue("grinding");if (grind != null)
{
grind.Selected = true;
}
//newsCheckBoxList.Items.FindByValue("grinding").Selected = true;
//newsCheckBoxList.Items[0].Selected = true;
}
if (reader["training"].ToString() == "True")
{
ListItem train = newsCheckBoxList.Items.FindByValue("training");if (train != null)
{
train.Selected = true;
}
//newsCheckBoxList.Items.FindByValue("training").Selected = true;
//newsCheckBoxList.Items[6].Selected = true;
}
}
}
catch (SqlException ex)
{
confirmLabel.Text = "A database-related exception occurred!" + ex.ToString();
}
catch (Exception ex)
{
confirmLabel.Text = "A general exception occurred!" + ex.ToString();
}
finally
{
conn.Close();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection conn;
SqlCommand updateComm;
// Read the connection string from Web.config
string connectionString = ConfigurationManager.ConnectionStrings["contacts"].ConnectionString;
// Initialize connection
conn = new SqlConnection(connectionString);
//create command
updateComm = new SqlCommand("UPDATE contactsMain SET sales=@sales, service=@service, spareParts=@spareParts, " +"tooling=@tooling, grinding=@grinding, training=@training WHERE ID=@ID", conn);
//add parameters
updateComm.Parameters.Add("@sales", System.Data.SqlDbType.Bit);
updateComm.Parameters["@sales"].Value = newsCheckBoxList.Items[1].Selected.ToString();
updateComm.Parameters.Add("@service", System.Data.SqlDbType.Bit);
updateComm.Parameters["@service"].Value = newsCheckBoxList.Items[2].Selected.ToString();
updateComm.Parameters.Add("@spareParts", System.Data.SqlDbType.Bit);
updateComm.Parameters["@spareParts"].Value = newsCheckBoxList.Items[3].Selected.ToString();
updateComm.Parameters.Add("@tooling", System.Data.SqlDbType.Bit);
updateComm.Parameters["@tooling"].Value = newsCheckBoxList.Items[4].Selected.ToString();
updateComm.Parameters.Add("@grinding", System.Data.SqlDbType.Bit);
updateComm.Parameters["@grinding"].Value = newsCheckBoxList.Items[0].Selected.ToString();
updateComm.Parameters.Add("@training", System.Data.SqlDbType.Bit);
updateComm.Parameters["@training"].Value = newsCheckBoxList.Items[5].Selected.ToString();
updateComm.Parameters.Add("@ID", System.Data.SqlDbType.Int);
updateComm.Parameters["@ID"].Value = idHiddenField.Value;
try
{
conn.Open();
updateComm.ExecuteNonQuery();
}
catch (SqlException ex)
{
confirmLabel.Text = "A database-related exception occurred!" + ex.ToString();
}
catch (Exception ex)
{
confirmLabel.Text = "A general exception occurred!" + ex.ToString();
}
finally
{
conn.Close();
Response.Redirect("Default.aspx");
}
}
}
aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:HiddenField ID="idHiddenField" runat="server" />
<div>
<asp:CheckBoxList ID="newsCheckBoxList" runat="server" RepeatColumns="4" >
<asp:ListItem Value="grinding" Text="Grinding" />
<asp:ListItem Value="sales" Text="Sales" />
<asp:ListItem Value="service" Text="Service" />
<asp:ListItem Value="spareParts" Text="Spare Parts" />
<asp:ListItem Value="tooling" Text="Tooling" />
<asp:ListItem Value="training" Text="Training" />
</asp:CheckBoxList>
<br />
<asp:Label ID="confirmLabel" runat="server" ForeColor="Red" />
<br />
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</div>
</form>
</body>
</html>
code behind:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string connectionString = ConfigurationManager.ConnectionStrings["contacts"].ConnectionString;
SqlDataReader reader;
SqlConnection conn = new SqlConnection(connectionString);
SqlCommand comm = new SqlCommand("SELECT ID, sales, service, spareParts, tooling, grinding, training " +
"FROM contactsMain WHERE ID = 100", conn);
try
{
conn.Open();
reader = comm.ExecuteReader();
while (reader.Read())
{
idHiddenField.Value = reader["ID"].ToString();
newsCheckBoxList.ClearSelection();
if (reader["sales"].ToString() == "True")
{
ListItem sale = newsCheckBoxList.Items.FindByValue("sales");if (sale != null)
{
sale.Selected = true;
}
//newsCheckBoxList.Items.FindByValue("sales").Selected = true;
//newsCheckBoxList.Items[1].Selected = true;
}
if (reader["service"].ToString() == "True")
{
ListItem serv = newsCheckBoxList.Items.FindByValue("service");if (serv != null)
{
serv.Selected = true;
}
//newsCheckBoxList.Items.FindByValue("service").Selected = true;
//newsCheckBoxList.Items[2].Selected = true;
}
if (reader["spareParts"].ToString() == "True")
{
ListItem spare = newsCheckBoxList.Items.FindByValue("spareParts");if (spare != null)
{
spare.Selected = true;
}
//newsCheckBoxList.Items.FindByValue("spareParts").Selected = true;
//newsCheckBoxList.Items[3].Selected = true;
}
if (reader["tooling"].ToString() == "True")
{
ListItem tool = newsCheckBoxList.Items.FindByValue("tooling");if (tool != null)
{
tool.Selected = true;
}
//newsCheckBoxList.Items.FindByValue("tooling").Selected = true;
//newsCheckBoxList.Items[4].Selected = true;
}
if (reader["grinding"].ToString() == "True")
{
ListItem grind = newsCheckBoxList.Items.FindByValue("grinding");if (grind != null)
{
grind.Selected = true;
}
//newsCheckBoxList.Items.FindByValue("grinding").Selected = true;
//newsCheckBoxList.Items[0].Selected = true;
}
if (reader["training"].ToString() == "True")
{
ListItem train = newsCheckBoxList.Items.FindByValue("training");if (train != null)
{
train.Selected = true;
}
//newsCheckBoxList.Items.FindByValue("training").Selected = true;
//newsCheckBoxList.Items[6].Selected = true;
}
}
}
catch (SqlException ex)
{
confirmLabel.Text = "A database-related exception occurred!" + ex.ToString();
}
catch (Exception ex)
{
confirmLabel.Text = "A general exception occurred!" + ex.ToString();
}
finally
{
conn.Close();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection conn;
SqlCommand updateComm;
// Read the connection string from Web.config
string connectionString = ConfigurationManager.ConnectionStrings["contacts"].ConnectionString;
// Initialize connection
conn = new SqlConnection(connectionString);
//create command
updateComm = new SqlCommand("UPDATE contactsMain SET sales=@sales, service=@service, spareParts=@spareParts, " +"tooling=@tooling, grinding=@grinding, training=@training WHERE ID=@ID", conn);
//add parameters
updateComm.Parameters.Add("@sales", System.Data.SqlDbType.Bit);
updateComm.Parameters["@sales"].Value = newsCheckBoxList.Items[1].Selected.ToString();
updateComm.Parameters.Add("@service", System.Data.SqlDbType.Bit);
updateComm.Parameters["@service"].Value = newsCheckBoxList.Items[2].Selected.ToString();
updateComm.Parameters.Add("@spareParts", System.Data.SqlDbType.Bit);
updateComm.Parameters["@spareParts"].Value = newsCheckBoxList.Items[3].Selected.ToString();
updateComm.Parameters.Add("@tooling", System.Data.SqlDbType.Bit);
updateComm.Parameters["@tooling"].Value = newsCheckBoxList.Items[4].Selected.ToString();
updateComm.Parameters.Add("@grinding", System.Data.SqlDbType.Bit);
updateComm.Parameters["@grinding"].Value = newsCheckBoxList.Items[0].Selected.ToString();
updateComm.Parameters.Add("@training", System.Data.SqlDbType.Bit);
updateComm.Parameters["@training"].Value = newsCheckBoxList.Items[5].Selected.ToString();
updateComm.Parameters.Add("@ID", System.Data.SqlDbType.Int);
updateComm.Parameters["@ID"].Value = idHiddenField.Value;
try
{
conn.Open();
updateComm.ExecuteNonQuery();
}
catch (SqlException ex)
{
confirmLabel.Text = "A database-related exception occurred!" + ex.ToString();
}
catch (Exception ex)
{
confirmLabel.Text = "A general exception occurred!" + ex.ToString();
}
finally
{
conn.Close();
Response.Redirect("Default.aspx");
}
}
}