Datagrid problems

  • Thread starter Thread starter Douglas Gage
  • Start date Start date
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: }
 
Douglas,
Looks like you need to check the ListItemType before
your loop. The DateGrid.Items collection holds the Header
and Footer items as well as the data that you are interest
in testing. The code is blowing up on the
ListItemType.Header and will do the same when it gets down
to the ListItemType.Footer because those datagrid rows
don't contain the textbox controls.


Barry.
-----Original Message-----
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"> said:
<HeaderStyle Font-Bold="True" ForeColor="#E7E7FF"
BackColor="#4A3C8C"></HeaderStyle>
<FooterStyle ForeColor="Black"
 
You are right, I have it fixed Now i want to store all the ansers users type
in those boxes and store it somewhere. I could not get those answers

Thank for your help

Douglas Gage said:
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
 
Back
Top