Object reference not set to an instance of an object.

G

Guest

Hello all, can someone please be my second pair of eyes! I'm getting the
above error at line: for(int i = 0; i <= dlstScope.Items.Count-1; i++).

My code's below: (dlstScope is a nested dlstScope in a repeater). Thanks all.

private void butSave_Click(object sender, System.EventArgs e)
{
DataList dlstScope = (DataList)myRepeater.FindControl("dlstScope");

for(int i = 0; i <= dlstScope.Items.Count-1; i++)
{
string Answer =
((DropDownList)dlstScope.Items.FindControl("txtAnswer")).SelectedValue;
string WorksheetQuestionID =
((Literal)dlstScope.Items.FindControl("lblWorksheetQuestionID")).Text;
string WorksheetID =
((Literal)dlstScope.Items.FindControl("lblWorksheetID")).Text;
Action(WorksheetQuestionID, Convert.ToInt32(WorksheetID),
Convert.ToInt32(Answer));
}
}
 
K

Karl Seguin

Jon,
Seems pretty straightforward with the information you've provided.
dlstScope is null, which means myRepeater.FindControl("dlstScope") didn't
return a value.

As you say, dlstScope is nested within the repeater, presumably the
ItemTemplate. If this is so, dlstScope isn't a child of myRepeater and thus
won't be found (as you are seeing). Instead, dlstScope is a child of an
Item of myRepeater..such as myRepeater.Items[0].FindControl("dlstScope")

What I can't tell you is which Item you want...you might want to loop
through all of them (in which case you'll have multiple dlstScope) or you
might want a particular one..but from the provided code I can't say which or
how to do it.

Hope this helps,
karl
 
M

Matt Berther

Hello Jon,

Clearly the problem is that dlstScope is null. Make sure that you've set
the id properly in the repeater template.
 
O

obeOnline

Is there somewhere that you are using the "new" keyword on dlstScope?
You have to create a new instance of dlstScope at some point.
 
G

Guest

Karl, thats' great! You pointed me in the right direction. This is the code
that worked: Thanks again.

private void butSave_Click(object sender, System.EventArgs e)
{
for(int j = 0; j <= myRepeater.Items.Count-1; j++)
{
DataList dlstScope =
(DataList)myRepeater.Items[j].FindControl("dlstScope");

for(int i = 0; i <= dlstScope.Items.Count-1; i++)
{
string Answer =
((DropDownList)dlstScope.Items.FindControl("txtAnswer")).SelectedValue;
string WorksheetQuestionID =
((Literal)dlstScope.Items.FindControl("lblWorksheetQuestionID")).Text;
string WorksheetID =
((Literal)dlstScope.Items.FindControl("lblWorksheetID")).Text;
Action(WorksheetQuestionID, Convert.ToInt32(WorksheetID),
Convert.ToInt32(Answer));
}
}

Karl Seguin said:
Jon,
Seems pretty straightforward with the information you've provided.
dlstScope is null, which means myRepeater.FindControl("dlstScope") didn't
return a value.

As you say, dlstScope is nested within the repeater, presumably the
ItemTemplate. If this is so, dlstScope isn't a child of myRepeater and thus
won't be found (as you are seeing). Instead, dlstScope is a child of an
Item of myRepeater..such as myRepeater.Items[0].FindControl("dlstScope")

What I can't tell you is which Item you want...you might want to loop
through all of them (in which case you'll have multiple dlstScope) or you
might want a particular one..but from the provided code I can't say which or
how to do it.

Hope this helps,
karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


Jon said:
Hello all, can someone please be my second pair of eyes! I'm getting the
above error at line: for(int i = 0; i <= dlstScope.Items.Count-1; i++).

My code's below: (dlstScope is a nested dlstScope in a repeater). Thanks all.

private void butSave_Click(object sender, System.EventArgs e)
{
DataList dlstScope = (DataList)myRepeater.FindControl("dlstScope");

for(int i = 0; i <= dlstScope.Items.Count-1; i++)
{
string Answer =
((DropDownList)dlstScope.Items.FindControl("txtAnswer")).SelectedValue;
string WorksheetQuestionID =
((Literal)dlstScope.Items.FindControl("lblWorksheetQuestionID")).Text;
string WorksheetID =
((Literal)dlstScope.Items.FindControl("lblWorksheetID")).Text;
Action(WorksheetQuestionID, Convert.ToInt32(WorksheetID),
Convert.ToInt32(Answer));
}
}

 

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

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top