I keep getting the following error message when I try to iterate through a
CheckBoxList control:
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information about
the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.
Source Error:
Line 65: {
Line 66: int i;
Line 67: int a=System.Convert.ToInt32(CheckBoxList1.Items.Count );
Line 68:
Line 69: for(i=0; i<a; i++)
Source File: c:\inetpub\wwwroot\sonar3\secured\controls\imgcont rol.ascx.cs
Line: 67
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an
object.]
Sonar3.Secured.Controls.ImgControl.Delete_Click(Ob ject sender, EventArgs
e) in c:\inetpub\wwwroot\sonar3\secured\controls\imgcont rol.ascx.cs:67
System.Web.UI.WebControls.Button.OnClick(EventArgs e)
System.Web.UI.WebControls.Button.System.Web.UI.IPo stBackEventHandler.RaisePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler
sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection postData)
System.Web.UI.Page.ProcessRequestMain()
The following is the page code:
<table>
<TBODY>
<tr>
<td>
<asp:repeater id="imgRep" runat="server">
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr class="txtTitle">
<td><%# DataBinder.Eval(Container.DataItem, "EventTitle")%></td>
</tr>
<tr>
<td class="txtTitle">Select the files you want to delete:</td>
</tr>
<tr>
<td><asp:CheckBoxList id="CheckBoxList1" DataSource='<%#
GetChildRelation(Container.DataItem,"EVNR_IMG")%>' CssClass="txt"
DataValueField="imgID" DataTextField="ImgName" runat="server" /></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:repeater>
</td>
</tr>
<tr>
<td><P align="right"><asp:Button ID="ImgDelete" Text="Delete"
OnClick="Delete_Click" Runat="server"></asp:Button></P></td>
</tr>
</TBODY>
</table>
The Codebehind is as follows:
public class ImgControl : System.Web.UI.UserControl
{
protected System.Data.SqlClient.SqlConnection clubconn;
protected System.Data.SqlClient.SqlDataAdapter clubAdapt;
protected System.Data.DataSet dsClub;
protected System.Web.UI.WebControls.Repeater imgRep;
//protected System.Web.UI.WebControls.CheckBoxList chkbximg;
protected System.Web.UI.WebControls.Button ImgDelete;
// Image delete function from Database.
protected System.Data.SqlClient.SqlCommand clubcmd;
protected System.Data.SqlClient.SqlParameter imgParam;
protected System.Web.UI.WebControls.CheckBoxList CheckBoxList1;
string strclubconn=ConfigurationSettings.AppSettings["ConnectionString"];
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
BindData();
}
}
public void BindData()
{
int intEV=System.Convert.ToInt32(Request.QueryString["EVNRID"]);
string strclubevnrcmd="SELECT EVNR_tbl.* FROM EVNR_tbl WHERE
EVNR_tbl.EVNRID=" + intEV;
string strclubimgcmd="SELECT img_tbl.* FROM img_tbl";
clubconn=new SqlConnection(strclubconn);
clubconn.Open();
dsClub=new DataSet();
clubAdapt=new SqlDataAdapter(strclubevnrcmd,clubconn);
clubAdapt.Fill(dsClub,"EVNR");
clubAdapt.SelectCommand=new SqlCommand(strclubimgcmd,clubconn);
clubAdapt.Fill(dsClub,"IMG");
dsClub.Relations.Add("EVNR_IMG",
dsClub.Tables["EVNR"].Columns["EVNRID"],
dsClub.Tables["IMG"].Columns["EVNRID"],false);
dsClub.Relations[0].Nested=true;
imgRep.DataSource=dsClub;
imgRep.DataMember="EVNR";
imgRep.DataBind();
}
public void Delete_Click(object sender, System.EventArgs e)
{
int i;
int a=System.Convert.ToInt32(CheckBoxList1.Items.Count );
for(i=0; i<a; i++)
{
if(CheckBoxList1.Items[i].Selected)
{
int intCheckValue=System.Convert.ToInt32(CheckBoxList1 .SelectedValue);
}
}
}
protected DataView GetChildRelation(object dataItem, string relation)
{
DataRowView drv=dataItem as DataRowView;
if(drv != null)
{
return drv.CreateChildView(relation);
}
else
{
return null;
}
}
nothing to fancy but I keep getting the nullreferance exception when I've
referanced the CheckBoxList control properly.
Can someone see where I went wrong? The checkboxlist displays properly.