Control Error

D

Douglas Gage

I have two columns

Question Answers
Q1
Q2
Q3
Q4

Submit

I have the following code to collect the answers and update them in the database but I got the error. I don't know what to do, please tell me if you can

Thanks







public void updateQuestion(object sender,System.EventArgs e)

{

ArrayList myAnsList = new ArrayList();

foreach(DataGridItem dataGridItem in grid.Items)

{

string str = ((TextBox)grid.FindControl("answerbox")).Text; ->error

myAnsList.Add(str);

}

//update

loadProDataTalbe(myAnsList);

}

<asp:datagrid id="grid" runat="server" >
<SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#9471DE"></SelectedItemStyle>
<ItemStyle ForeColor="Black" BackColor="#DEDFDE"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#E7E7FF" BackColor="#4A3C8C"></HeaderStyle>
<FooterStyle ForeColor="Black" BackColor="#C6C3C6"></FooterStyle>
<PagerStyle HorizontalAlign="Right" ForeColor="Black" BackColor="#C6C3C6"></PagerStyle>
<Columns>
<asp:BoundColumn DataField="Q_Question" HeaderText="Questions"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Answer">
<ItemTemplate>
<asp:TextBox Runat="server" ID="answerbox"></asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>




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 100: foreach(DataGridItem dataGridItem in grid.Items)
Line 101: {
Line 102: string str = ((TextBox)grid.FindControl("answerbox")).Text;
Line 103: myAnsList.Add(str);
Line 104: }
 
C

Chris Jackson

The first thing I notice is that you iterate through each dataGridItem, but
then only call FindControl on the grid itself. If it returns anything, then
it will always return the same thing. Of course, you are finding that it
doesn't control anything, which is because the immediate container (the
grid) doesn't contain a server control with this ID. Rather, you have to dig
down in the hierarchy, navigating to the specific object that you want. (It
doesn't recursively search the control tree - it just searches the immediate
container.) If you dig into the Cells, you should find that one of the Cells
is the container that holds the answerbox object.

--
Chris Jackson
Software Engineer
Microsoft MVP - Windows Client
Windows XP Associate Expert
--
More people read the newsgroups than read my email.
Reply to the newsgroup for a faster response.
(Control-G using Outlook Express)
--

I have two columns

Question Answers
Q1
Q2
Q3
Q4

Submit

I have the following code to collect the answers and update them in the
database but I got the error. I don't know what to do, please tell me if you
can

Thanks







public void updateQuestion(object sender,System.EventArgs e)

{

ArrayList myAnsList = new ArrayList();

foreach(DataGridItem dataGridItem in grid.Items)

{

string str = ((TextBox)grid.FindControl("answerbox")).Text; ->error

myAnsList.Add(str);

}

//update

loadProDataTalbe(myAnsList);

}

<asp:datagrid id="grid" runat="server" >
<SelectedItemStyle Font-Bold="True" ForeColor="White"
BackColor="#9471DE"></SelectedItemStyle>
<ItemStyle ForeColor="Black" BackColor="#DEDFDE"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#E7E7FF"
BackColor="#4A3C8C"></HeaderStyle>
<FooterStyle ForeColor="Black" BackColor="#C6C3C6"></FooterStyle>
<PagerStyle HorizontalAlign="Right" ForeColor="Black"
BackColor="#C6C3C6"></PagerStyle>
<Columns>
<asp:BoundColumn DataField="Q_Question"
HeaderText="Questions"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Answer">
<ItemTemplate>
<asp:TextBox Runat="server" ID="answerbox"></asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>




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 100: foreach(DataGridItem dataGridItem in grid.Items)
Line 101: {
Line 102: string str = ((TextBox)grid.FindControl("answerbox")).Text;
Line 103: myAnsList.Add(str);
Line 104: }
 

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
473,773
Messages
2,569,594
Members
45,121
Latest member
LowellMcGu
Top