C# with datagrid

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: }
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi Douglas,

Two things:

1- Are you binding the grid? you need to do this first of all.

2- in the foreach, you need to change this line:

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

to:

string str = ((TextBox)dataGridItem.FindControl("answerbox")).Text;


Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

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
 
D

Douglas Gage

Thank, no error now but i can't get any text out of the answers boxes

I want to retrieve those and update in database

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
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Are you binding the content?
if so, how are you doing so, if you post the code both of your aspx and code
behind we can help you, if you are declaring the grid dinamically in the
code you need to do a databind each time a postback is done.

Cheers,
 

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

Top