Showing posts with label inside. Show all posts
Showing posts with label inside. Show all posts

Thursday, March 29, 2012

Object reference error

I have a datagrid and each column contains a different controls. When Iam trying to access the value of the ListBox controls inside thedatagrid it keeps on throwing "Object reference not set to an instanceof an object". The line is bold where it throws the error.
Here is my code:
private void Button1_Click(object sender, System.EventArgs e)
{
StringBuilder str = new StringBuilder();

foreach(DataGridItem dgi in myDataGrid.Items)
{
TextBox myTextBox = (TextBox)(dgi.Cells[0].Controls[1]);
ListBox myListBox = (ListBox)(dgi.Cells[1].Controls[1]);
DropDownList myList = (DropDownList)(dgi.Cells[2].Controls[1]);
CheckBox myCheckBox = (CheckBox)(dgi.Cells[3].Controls[1]);
str.Append(myTextBox.Text);
if(myListBox.SelectedItem.Value != null)
{
str.Append(myListBox.SelectedItem.Text);
}


str.Append(myList.SelectedItem.Text);
str.Append(myCheckBox.Checked);


}

Probably my eyes but there's no line in bold.

If you debug and check the type of the controls that you're using, are they of the correct type? I know I had some trouble in the past that sometimes I had to take Controls[0] and sometimes Controls[1] to obtain the correct control.

Grz, Kris.


Yeah I tried using Controls[0] but it did not worked and gave me that "Specific cast is not valid" error. The bold line below causes "Object reference not set to an instance of an object" This error is only thrown when I dont select any item in the ListBox.

foreach(DataGridItem dgiin myDataGrid.Items)

{

TextBox myTextBox = (TextBox) (dgi.Cells[0].Controls[1]);

ListBox myListBox = (ListBox) (dgi.Cells[1].Controls[1]);

DropDownList myList = (DropDownList) (dgi.Cells[2].Controls[1]);

CheckBox myCheckBox = (CheckBox) (dgi.Cells[3].Controls[1]);

str.Append(myTextBox.Text);

if(myListBox.SelectedItem.Value !=null)

{

str.Append(myListBox.SelectedItem.Text);

}

str.Append(myList.SelectedItem.Text);

str.Append(myCheckBox.Checked);

}


Okay I got it. I had to get the value from the ListBox into a string variable and than compare the string to null and empty.
The following code works:
foreach(DataGridItem dgi in myDataGrid.Items)
{
TextBox myTextBox = (TextBox)(dgi.Cells[0].Controls[1]);
ListBox myListBox = (ListBox)(dgi.Cells[1].Controls[1]);
DropDownList myList = (DropDownList)(dgi.Cells[2].Controls[1]);
CheckBox myCheckBox = (CheckBox)(dgi.Cells[3].Controls[1]);
str.Append(myTextBox.Text);
string a = myListBox.SelectedValue;
//string b = myListBox.SelectedItem.Value;
//string c = myListBox.SelectedItem.Text;
if(a != null && a != "")
{
str.Append(myListBox.SelectedItem.Text);
}


str.Append(myList.SelectedItem.Text);
str.Append(myCheckBox.Checked);


}

Saturday, March 24, 2012

Object Reference Not Set To An Instance of an Object

I have an asp:table inside an itemtemplate of a datalist (dlstForDetails)
the id in three rows of the table is, tblrMembers1, tblrServices1,
tblrBelongs1

Depending on the "go" value of the querystring I want to show or hide any of
these rows.
The code is the following but I get the error

Object Reference not set to an instance of an object

Dim tblrMembers, tblrServices, tblrBelongs As TableRow

tblrMembers = CType(dlstForDetails.FindControl("tblrMembers1"), TableRow)
tblrServices = CType(dlstForDetails.FindControl("tblrServices1"), TableRow)
tblrBelongs = CType(dlstForDetails.FindControl("tblrBelongs1"), TableRow)

Select Case Request.QueryString("go")
Case "YP"
tblrMembers.Visible = False
tblrServices.Visible = False
tblrBelongs.Visible = False
Case "EP"
tblrMembers.Visible = False
Case "AN"
tblrServices.Visible = False
tblrBelongs.Visible = False
Case "KE"
tblrMembers.Visible = False
tblrBelongs.Visible = False
End Selectthere are many possible causes.

the datalist will create a Header even if it does not show it. Check the
ItemType of the item before trying to hide anything as you table wont be in
the header
Yeap you're right thanks a lot!!!

"Josh" <s@.a.com> wrote in message
news:Ok6z3VjSFHA.3144@.TK2MSFTNGP09.phx.gbl...
> there are many possible causes.
> the datalist will create a Header even if it does not show it. Check the
> ItemType of the item before trying to hide anything as you table wont be
> in the header

Object Reference Not Set To An Instance of an Object

I have an asp:table inside an itemtemplate of a datalist (dlstForDetails)
the id in three rows of the table is, tblrMembers1, tblrServices1,
tblrBelongs1
Depending on the "go" value of the querystring I want to show or hide any of
these rows.
The code is the following but I get the error
Object Reference not set to an instance of an object
Dim tblrMembers, tblrServices, tblrBelongs As TableRow
tblrMembers = CType(dlstForDetails.FindControl("tblrMembers1"), TableRow)
tblrServices = CType(dlstForDetails.FindControl("tblrServices1"), TableRow)
tblrBelongs = CType(dlstForDetails.FindControl("tblrBelongs1"), TableRow)
Select Case Request.QueryString("go")
Case "YP"
tblrMembers.Visible = False
tblrServices.Visible = False
tblrBelongs.Visible = False
Case "EP"
tblrMembers.Visible = False
Case "AN"
tblrServices.Visible = False
tblrBelongs.Visible = False
Case "KE"
tblrMembers.Visible = False
tblrBelongs.Visible = False
End Selectthere are many possible causes.
the datalist will create a Header even if it does not show it. Check the
ItemType of the item before trying to hide anything as you table wont be in
the header
Yeap you're right thanks a lot!!!
"Josh" <s@.a.com> wrote in message
news:Ok6z3VjSFHA.3144@.TK2MSFTNGP09.phx.gbl...
> there are many possible causes.
> the datalist will create a Header even if it does not show it. Check the
> ItemType of the item before trying to hide anything as you table wont be
> in the header
>