473,473 Members | 1,954 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Problem getting value from Datagrid

I have the following cod

private void btnUpdate_Click(object sender, System.EventArgs e

TextBox tbMembers = new TextBox()

tbMembers = (TextBox) dlClubs.FindControl("tbMembers")

string sqlUpdate = "UPDATE tblClub SET "

if (tbMembers.Text.Length > 0
sqlUpdate = sqlUpdate + "Membership = " + tbMembers.Text.Replace("'","''") + ", "
els
sqlUpdate = sqlUpdate + "Membership = null,"
sqlUpdate = sqlUpdate + " WHERE (ClubID = " + ddlClubs.SelectedValue + ")"
Response.Write(sqlUpdate)
This code results in a "Object reference not set to an instance of an object". I have checked to ensure the Datagrid contains this object. The following is how it is defined in the datagrid

<asp:datalist id="dlClubs" runat="server"><ItemTemplate><TABLE id="Table13" width="500"><TR><TD vAlign="top"><asp:label id="lblMembers" runat="server" CssClass="lblDetail">Number of Memebers:</asp:label></TD><TD vAlign="top"><asp:textbox id="tbMembers" runat="server" Width="200px" Text='<%# DataBinder.Eval(Container, "DataItem.Membership") %>'></asp:textbox></TD></TR></TABLE></ItemTemplate></asp:datalist

What am I doing wrong? Shouldn't this work? Is there some other way to get the value out of a textbox element on the form

Thanks
CDWaddel
Nov 15 '05 #1
2 5180

Hi CDWaddell,

Thank you for posting in the community!

Based on my understanding, you use DataList control in your webform, and
you want to use FindControl to get the TextBox and do operation on it.
But the FindControl method will always fail and return null reference.

=======================================
Actually, Control.FindControl(id) method will find the control with "id" IN
"Control"'s NAMINGCONTAINER.

For NamingContainer, it is the ID namespace. For all the Repeating
control(DataGrid, DataList or Repeater), each row in them also implements
the INamingContainer interface. And every TextBox is included in a row, so
the TextBox is not in the NamingContainer of the DataList control, it is in
the NamingContainer of each row. So the DataList.FindControl will not find
the TextBox.

The reason why every row implement INamingContainer is that: every row
contains TextBox, which has the same ID "tbMembers", so it will cause ID
conflict in DataList control, so every row acts as a namingcontainer for
child control to separate the conflict.

For more information about the Control Identification mechanism in Asp.net
Web Form, please refer to the article below:
http://msdn.microsoft.com/library/en...mscontrolident
ification.asp

To determine which control implement INamingContainer interface, please
refer to:
http://msdn.microsoft.com/library/de...us/cpref/html/
frlrfSystemWebUIINamingContainerClassTopic.asp

~~~~~~~~~~~~~~~~~~
To resolve your problem, there are 2 ways:

1). Use the NamingContainer of the child control(That is DataListItem):

foreach(DataListItem dli in dlClubs.Items)
{
TextBox tb=(TextBox)dli.FindControl("tbMembers");
this.Response.Write( tb.ID+ " <br>" );
}

2). Loop through the control collection of each row, and find the TextBox:

foreach(DataListItem dli in dlClubs.Items)
{
if(e.Item.ItemType==ListItemType.Item
||e.Item.ItemType==ListItemType.AlternatingItem)
{
foreach(Control c in dli.Controls)
{
if(c is TextBox)
{
this.Response.Write(c.ID+ "<br>");
}
}
}
}

The second solution is convenient when you did not specify the ID for your
child control.

Additionally, for more information about "Referencing Controls in Web Forms
Pages", please refer to:
http://msdn.microsoft.com/library/de...us/vbcon/html/
vbtskreferencingcontrolsinwebformspages.asp

=================================================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.
Have a nice day!!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #2

Sorry, the second solution code should be:

foreach(DataListItem dli in dlClubs.Items)
{
if(dli.ItemType==ListItemType.Item
||dli.ItemType==ListItemType.AlternatingItem)
{
foreach(Control c in dli.Controls)
{
if(c is TextBox)
{
this.Response.Write(c.ID+ "<br>");
}
}
}
}

Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #3

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

Similar topics

1
by: Amadelle | last post by:
Hi all and thanks in advance for your help, I have a problem with capturing the changed value of a text box in a datagrid. The datagrid is populated based on a dataset and I am using template...
5
by: Jeff | last post by:
IDE: VS 2003 :NET OS: XP Pro My app have a form with a tab-control on it. The tab-control have 2 tabpages. One of the tabpages displays a datagrid, and the other tabpage displays details (order...
0
by: Diogo Alves - Software Developer | last post by:
Hi there, I have a big problem here, I am binding a database table to a dataset and then populate a datagrid the problem is that I have a column that is named TOTALMINUTES and type real.... On...
5
by: Jason | last post by:
I've been trying to figure out a good way to do this but haven't had much luck, any input would be greatly appreciated. Basically, after a datagrid is sorted, how can I get the primary key value...
3
by: Stuart | last post by:
Hi there I am using a button column>SELECT and a datagrid.Item_Command to take a value from the datagrid (in this case a customer account number) - store that value in to a session variable and...
1
by: Eustice Scrubb | last post by:
I'm trying to use the Insert.aspx Quickstart and I'm getting a NULL pointer exception. Any help? <code> <script language="VB" runat="server"> Dim myConnection As SqlConnection Sub...
4
by: Reney | last post by:
I have a very weird problem in updating my datagrid. Please help me to solve it. The datagrid is tied to a dataset table with five columns. Three of them are primary key and the other two columns...
3
by: JJ | last post by:
I have a datagrid on my form that displays its data from various tables within a specified dataset. That all works correclty. However, when one cell is selected prior to changed the displayed...
4
by: JJGarcia | last post by:
Hi Everyone, I'll try to explain the process I'm following, I'm new to this so I'm triying the easy way first, probably the lasyest too! I created a new Project, drag in to it a SQLConnection,...
9
by: rn5a | last post by:
A Form has a DataGrid which displays records from a SQL Server 2005 DB table. Users can modify the records using this DataGrid for which I am using EditCommandColumn in the DataGrid. This is the...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.