Vriti Sharma

Vriti Sharma

  • NA
  • 52
  • 1.6k

Display checked checkbox items in repeater

Aug 22 2018 1:00 AM
Here is my code I m getting  error in if(ch.Checked)  plz fix it:
System.NullReferenceException: 'Object reference not set to an instance of an object.'
 
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
public partial class Assg1DatalistCheckbox : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false)
{
ChkDataList_Bind();
}
}
private void ChkDataList_Bind()
{
SqlDataAdapter adp = new SqlDataAdapter("Select * from tbbook", ConfigurationManager.ConnectionStrings["cn"].ConnectionString);
DataSet ds = new DataSet();
adp.Fill(ds);
DataList1.DataSource = ds;
DataList1.DataBind();
}
 
 
protected void Button1_Click(object sender, EventArgs e)
{
String d = " ";
for(Int32 i = 0; i < DataList1.Items.Count; i++)
{
CheckBox ch;
ch = (CheckBox)(DataList1.Items[i].FindControl("chk"));
if(ch.Checked)
{
// concatenating
d += DataList1.DataKeys[i].ToString() + ",";
}
}
d = d.Substring(0, d.Length - 1);
String qry = "Select * from tbbook where BookId in(" + d + ")";
SqlDataAdapter adp = new SqlDataAdapter(qry, ConfigurationManager.ConnectionStrings["cn"].ConnectionString);
DataSet ds = new DataSet();
adp.Fill(ds);
Repeater1.DataSource = ds;
Repeater1.DataBind();
}
}
 

Answers (1)