CheckBoxList Control nullReferance Exception

G

Guest

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\imgcontrol.ascx.cs
Line: 67

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an
object.]
Sonar3.Secured.Controls.ImgControl.Delete_Click(Object sender, EventArgs
e) in c:\inetpub\wwwroot\sonar3\secured\controls\imgcontrol.ascx.cs:67
System.Web.UI.WebControls.Button.OnClick(EventArgs e)

System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection 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.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.
 
B

Brock Allen

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...ommandName+ItemCommand+Brock#177bcbd1d9b24188

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...ommandName+ItemCommand+Brock#858b5306e685b389




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\imgcontrol.ascx.cs
Line: 67
Stack Trace:

[NullReferenceException: Object reference not set to an instance of an
object.]
Sonar3.Secured.Controls.ImgControl.Delete_Click(Object sender,
EventArgs
e) in c:\inetpub\wwwroot\sonar3\secured\controls\imgcontrol.ascx.cs:67
System.Web.UI.WebControls.Button.OnClick(EventArgs e)
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.R
aisePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection 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.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.
 
K

Karl Seguin

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 said:
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\imgcontrol.ascx.cs
Line: 67

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an
object.]
Sonar3.Secured.Controls.ImgControl.Delete_Click(Object sender, EventArgs
e) in c:\inetpub\wwwroot\sonar3\secured\controls\imgcontrol.ascx.cs:67
System.Web.UI.WebControls.Button.OnClick(EventArgs e)

System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection 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.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.
 
G

Guest

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 said:
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 said:
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\imgcontrol.ascx.cs
Line: 67

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an
object.]
Sonar3.Secured.Controls.ImgControl.Delete_Click(Object sender, EventArgs
e) in c:\inetpub\wwwroot\sonar3\secured\controls\imgcontrol.ascx.cs:67
System.Web.UI.WebControls.Button.OnClick(EventArgs e)

System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection 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.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.

 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top