Click here to Skip to main content
15,914,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I get the following exceptions at Button18_click:

An exception of type 'System.NullReferenceException' occurred in App_Web_cdebfrfo.dll but was not handled in user code

Additional information: Object reference not set to an instance of an object


My code for textbox inside a datalist:
XML
<asp:TextBox Id="TB2" runat="server" Text="" Width="24" OnTextChanged="TB2_TextChanged" MaxLength="6"
Height="12"     BorderStyle="Groove">
</asp:TextBox>

<asp:Button ID="Button18" runat="server" ForeColor="White"  onclick="Button18_Click"
         BackColor="Teal" Font-Size="small"
 style="z-index: 1;font-family:Sans-Serif; height:22px;  font-weight:900; "
 Text="Add to cart" />


C#
protected void Button18_Click(object sender, EventArgs e)
   {
       string ss = Convert.ToString(Datalist1.FindControl("TB2").ToString());
  }
Posted
Updated 21-Sep-13 6:08am
v2

1 solution

It can't find the TB2 control. I would recommend putting breakpoint and debug it. Perhaps you have an additional control that is the parent of the textbox. FindControl does not go all the way down the tree. So, you may need to do Datalist1.Controls[0].FindControl("TB2") for example. Debug it and you should be able to find it.
 
Share this answer
 
Comments
S.Rajendran from Coimbatore 21-Sep-13 23:49pm    
I entered '5' with 'TB2'.
With a break point to verify at string 'ss' I used like this:
string ss=Convert.ToString(Datalist1.Controls[0].FindControl("TB2"));
The result for string 'ss' is "" instead of '5'.
ZurdoDev 21-Sep-13 23:53pm    
You'll need to examine Datalist1.Controls collection to see what is what and where your control is.
S.Rajendran from Coimbatore 21-Sep-13 23:59pm    
Hereunder is code for my datalist1:

<asp:DataList ID="Datalist1" runat="server" DataKeyField="item_code" RepeatDirection="Horizontal"
RepeatLayout="Table">
<HeaderTemplate>
</HeaderTemplate>
<itemtemplate>
Item code:
<asp:Label Id="Itmcodelbl" runat="server" Text='<%#Eval("item_code") %>'>

<br />
Item Name:
<asp:Label Id="Itmnamelbl" runat="server" Text='<%#Eval("item_name") %>'>

<br />
Particulars:
<br />
<asp:TextBox Id="TB1" runat="server" Text='<%#Eval("item_desc") %>'
Height="94" ReadOnly="true" TextMode="MultiLine" BorderStyle="None">

<br />

<asp:Image ID="imgEmp" runat="server" Width="100px" Height="120px" ImageUrl='<%# Bind("Photopath","~/photo/{0}") %>' style="padding-left:40px; border-color:Green; border-left-style:groove; border-width:thin"/><br />
Buy :
<asp:TextBox Id="TB2" runat="server" Text="" Width="24" OnTextChanged="TB2_TextChanged" MaxLength="6" AutoPostBack="true"
Height="12" BorderStyle="Groove">


<asp:Button ID="Button18" runat="server" ForeColor="White" onclick="Button18_Click"
BackColor="Teal" Font-Size="small"
style="z-index: 1;font-family:Sans-Serif; height:22px; font-weight:900; "
Text="Add to cart" />



ZurdoDev 24-Sep-13 21:03pm    
Where's your item template?
pamod singh 25-Jul-14 6:12am    
<asp:DataList ID="Datalist1" runat="server" DataKeyField="item_code" RepeatDirection="Horizontal"
RepeatLayout="Table">
<HeaderTemplate>
</HeaderTemplate>

Item code:
<asp:Label Id="Itmcodelbl" runat="server" Text='<%#Eval("item_code") %>'>

<br />
Item Name:
<asp:Label Id="Itmnamelbl" runat="server" Text='<%#Eval("item_name") %>'>

<br />
Particulars:
<br />
<asp:TextBox Id="TB1" runat="server" Text='<%#Eval("item_desc") %>'
Height="94" ReadOnly="true" TextMode="MultiLine" BorderStyle="None">

<br />

<asp:Image ID="imgEmp" runat="server" Width="100px" Height="120px" ImageUrl='<%# Bind("Photopath","~/photo/{0}") %>' style="padding-left:40px; border-color:Green; border-left-style:groove; border-width:thin"/><br />
Buy :
<asp:TextBox Id="TB2" runat="server" Text="" Width="24" OnTextChanged="TB2_TextChanged" MaxLength="6" AutoPostBack="true"
Height="12" BorderStyle="Groove">


<asp:Button ID="Button18" runat="server" ForeColor="White" onclick="Button18_Click"
BackColor="Teal" Font-Size="small"
style="z-index: 1;font-family:Sans-Serif; height:22px; font-weight:900; "
Text="Add to cart" />

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900