469,117 Members | 1,348 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,117 developers. It's quick & easy.

CheckBoxList Control nullReferance Exception

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.
Nov 19 '05 #1
3 2659
You can't just reference your CheckBoxList this way since it's in a template.
You need to find the CheckBoxList for the RepeaterItem you're in. I'd suggest
changing how you handle the event to something like this:

http://groups-beta.google.com/group/...7bcbd1d9b24188

And this also has a bit more code but it's not exactly the same as what you're
doing:

http://groups-beta.google.com/group/...8b5306e685b389

-Brock
DevelopMentor
http://staff.develop.com/ballen
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.R
aisePostBackEvent(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.


Nov 19 '05 #2
Sam:
NullReferenceExceptions are pretty easy to debug in my
opinion...CheckBoxList1 doesn't exist. It doesn't exist because it's within
a repeater's item....you could do:

for (RepeaterItem item in imgRep.Items){
CheckBoxList c = (CheckBoxList)item.FindControl("CheckBoxList1");
if (c != null){
//do stuff
}
}

Just guessing, but I take it you don't want to loop thorugh each item but
instead want to delete a specific one...in this case you must move the
button inside hte ItemTemplate and hook into the Repeater's ItemCommand
event..
Incidently, why Convert.ToInt32 the Count? it's already an int'

Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"I am Sam" <Ia****@discussions.microsoft.com> wrote in message
news:89**********************************@microsof t.com...
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.

Nov 19 '05 #3
Hi Karl,

Actually I wanted to loop through and delete all the images that are checked
and skip those that aren't checked so I am going to have to loop through the
CheckBoxList.

"Karl Seguin" wrote:
Sam:
NullReferenceExceptions are pretty easy to debug in my
opinion...CheckBoxList1 doesn't exist. It doesn't exist because it's within
a repeater's item....you could do:

for (RepeaterItem item in imgRep.Items){
CheckBoxList c = (CheckBoxList)item.FindControl("CheckBoxList1");
if (c != null){
//do stuff
}
}

Just guessing, but I take it you don't want to loop thorugh each item but
instead want to delete a specific one...in this case you must move the
button inside hte ItemTemplate and hook into the Repeater's ItemCommand
event..
Incidently, why Convert.ToInt32 the Count? it's already an int'

Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"I am Sam" <Ia****@discussions.microsoft.com> wrote in message
news:89**********************************@microsof t.com...
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.


Nov 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Bryce Budd | last post: by
3 posts views Thread by Robin Day | last post: by
4 posts views Thread by dm_dal | last post: by
2 posts views Thread by Simon Cheng | last post: by
7 posts views Thread by bienwell | last post: by
4 posts views Thread by Patrick.O.Ige | last post: by
reply views Thread by =?Utf-8?B?S04=?= | last post: by
6 posts views Thread by dbuchanan | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.