I have a some problem.I have saved values in a single column like E1,E2 with Checkbox list.
Now i would like to true the value in asp.net CheckBoxList control where E1 and E2 must be selected when page Load.In Checkbox List Edit Template E1 E2 E3 E4 E5.When Save Mutiple checkbox not check.
string connStr = ConfigurationManager.ConnectionStrings["Thcon"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
txtbookinid.Text = boking.GetMaxID().ToString();
GetSec();
}
protected void Button1_Click(object sender, EventArgs e)
{
boking.Bookid = Convert.ToInt32(txtbookinid.Text);
string date = ddlday.Text + "/" + ddlmth.Text + "/" + ddyear.Text;
string time = ddlshowtime.Text;
CheckBoxList chk = CheckBoxList1.FindControl("CheckListBoxList1") as CheckBoxList;
string sno = "";
for (int i = 0; i < chk.Items.Count; i++)
{
if (chk.Items[i].Selected)
{
sno += chk.Items[i].Value + ",";
}
}
if (CheckBoxList1.Enabled == true)
{
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=TheatreDB;integrated security=true");
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.Parameters.Clear();
cmd.CommandText = "INSERT INTO [TBL_Bookin] ([bookid],[userid],[moviename],[seatno],[date],[time]) VALUES (@id,@userid,@moviename,@seatno,@date,@time)";
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@id", txtbookinid.Text);
cmd.Parameters.AddWithValue("@userid", Session["name"].ToString());
cmd.Parameters.AddWithValue("@moviename", ddlmovielist.SelectedItem.Text);
cmd.Parameters.AddWithValue("@seatno", sno);
cmd.Parameters.AddWithValue("@date", date);
cmd.Parameters.AddWithValue("@time", time);
con.Open();
int result = cmd.ExecuteNonQuery();
con.Close();
}
Response.Redirect("ViewSeating.aspx");
}
public void GetSec()
{
CheckBoxList chbx = CheckBoxList1.FindControl("CheckBoxList1") as CheckBoxList;
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=TheatreDB;integrated security=true");
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT seatno FROM TBL_Bookin";
cmd.Parameters.Clear();
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
ListItem currentChbx = chbx.Items.FindByText(reader["seatno"].ToString());
if (currentChbx != null)
{
currentChbx.Selected = true;
currentChbx.Enabled = false;
}
}
con.Close();
}
Loading
Abhay ShankerPosted Mar 31, 2014, 5:37 AM
1.Get the record from database
2.For the stringArray by that
3. loop to all the checkbox and check the condition whether the current chckbox is checked or not
moe satPosted Mar 31, 2014, 7:27 AM
con.Close();
string query_result = result.ToString();
string[] seatArr = query_result.Split(new char[] { ',' });
foreach (string seatNumber in seatArr)
{
ListItem chkItem = CheckBoxList1.Items.FindByText(seatNumber);
if (chkItem != null)
{
chkItem.Selected = true;
chkItem.Enabled = false;
}
}
When I delete data."Object reference not set to an instance of an object error";
Sivaraman DhamodaranPosted Mar 31, 2014, 4:52 AM
Link