473,385 Members | 1,402 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Control Error

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

Nov 18 '05 #1
1 1141
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)
--

"Douglas Gage" <ha******@neo.tamu.edu> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
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: }
Nov 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: owen | last post by:
Generally speaking, what does it mean when I see a "button" with red text showing this message instead of the control I've dragged onto the web form in Design View.? (But the page works fine at...
4
by: patrick_a | last post by:
Hello, I am trying to insert multiple instances of a custom user control into a placeholder on an aspx page, based on the records retrieved from the database. I use the datareader to loop through...
1
by: Lauchlan M | last post by:
Hi. I have an ASP.net project that works fine. I copied it to another project using the following steps: << (i) create a new VS.NET ASP.NET project.
4
by: Moe Sizlak | last post by:
Hi There, I am trying to return the value of a listbox control that is included as a user control, I can return the name of the control but I can't access the integer value of the selected item,...
0
by: Ravi Ambros Wallau | last post by:
Hi: I've created a custom control - a grid that uses Infragistics to display some filters, the grid itself, and some buttons. Well, when using this control directly on WebForm, everything works...
8
by: Ravi Ambros Wallau | last post by:
Hey guys: What can I do when an "Error Creating Control" is displayed on the form (instead of the control), and a tooltip indicating the error never is displayed? Is there some log, some hidden...
15
by: Nathan | last post by:
I have an aspx page with a data grid, some textboxes, and an update button. This page also has one html input element with type=file (not inside the data grid and runat=server). The update...
7
by: Chuck Hartman | last post by:
I have a Windows service that requests web pages from a site using an HttpWebRequest object. When I try to request a page from an ASP.NET 2 site, I get a WebException with message "The remote...
2
by: eagle | last post by:
How do I access a control, like a placeholder, from another page, or another user control that may be on the same page. For instance, I have a web page with 2 user controls. One user controls...
0
by: Tony Johansson | last post by:
Hello! I have a very specific question and that is about how to inherit a visual control for example the control System.Windows.Forms.TextBox without causing the environment to delete the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.