Friday, March 16, 2012

Object reference not set to an instance of an object.

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[i].FindControl("txtAnswer")).SelectedValue;
string WorksheetQuestionID =
((Literal)dlstScope.Items[i].FindControl("lblWorksheetQuestionID")).Text;
string WorksheetID =
((Literal)dlstScope.Items[i].FindControl("lblWorksheetID")).Text;
Action(WorksheetQuestionID, Convert.ToInt32(WorksheetID),
Convert.ToInt32(Answer));
}
}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" <Jon@.discussions.microsoft.com> wrote in message
news:C959676E-037C-446E-94B7-90F48431D802@.microsoft.com...
> 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[i].FindControl("txtAnswer")).SelectedValue;
> string WorksheetQuestionID =
> ((Literal)dlstScope.Items[i].FindControl("lblWorksheetQuestionID")).Text;
> string WorksheetID =
> ((Literal)dlstScope.Items[i].FindControl("lblWorksheetID")).Text;
> Action(WorksheetQuestionID, Convert.ToInt32(WorksheetID),
> Convert.ToInt32(Answer));
> }
> }
>
>
Hello Jon,
Clearly the problem is that dlstScope is null. Make sure that you've set
the id properly in the repeater template.
Matt Berther
http://www.mattberther.com

> 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[i].FindControl("txtAnswer")).SelectedVa
> lue;
> string WorksheetQuestionID =
> ((Literal)dlstScope.Items[i].FindControl("lblWorksheetQuestionID")).Te
> xt;
> string WorksheetID =
> ((Literal)dlstScope.Items[i].FindControl("lblWorksheetID")).Text;
> Action(WorksheetQuestionID, Convert.ToInt32(WorksheetID),
> Convert.ToInt32(Answer));
> }
> }
Is there somewhere that you are using the "new" keyword on dlstScope?
You have to create a new instance of dlstScope at some point.
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[i].FindControl("txtAnswer")).SelectedValue;
string WorksheetQuestionID =
((Literal)dlstScope.Items[i].FindControl("lblWorksheetQuestionID")).Text;
string WorksheetID =
((Literal)dlstScope.Items[i].FindControl("lblWorksheetID")).Text;
Action(WorksheetQuestionID, Convert.ToInt32(WorksheetID),
Convert.ToInt32(Answer));
}
}
"Karl Seguin" wrote:

> 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 th
us
> 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" <Jon@.discussions.microsoft.com> wrote in message
> news:C959676E-037C-446E-94B7-90F48431D802@.microsoft.com...
> all.
>
>

0 comments:

Post a Comment