Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi,

I am getting object reference error when i use Hidden field in gridview.

I bound the value in Hidden Controls. if value is nothing i am getting error.

I have checking Nothing also. But, still getting error in the code.

see my code

ASP.NET
<asp:TemplateField HeaderText="Budget ManHours"  ItemStyle-Width="65%">
       <ItemTemplate>
              <asp:HiddenField ID="HdnSal" runat="server" Value='<%# Eval("Sal") %>' />
       </ItemTemplate>
       <ItemTemplate>
             <asp:TextBox ID="txtSal" runat="server" ForeColor="Blue" Text='<%# Eval("Sal") %>' Width="75%" ReadOnly="true" AutoPostBack="true" OnTextChanged="txtTot_TextChanged" MaxLength="7"></asp:TextBox>
       </ItemTemplate>
       <FooterTemplate>
              <asp:Label ID="lblTotal" runat="server" ForeColor="Green"></asp:Label>
       </FooterTemplate>
       <ItemStyle HorizontalAlign="Center"/>
</asp:TemplateField>


ASPX.VB Code
------------
VB
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound

       If e.Row.RowType = DataControlRowType.DataRow Then
           Dim chkSelect As CheckBox = TryCast(e.Row.FindControl("chkSelect"), CheckBox)
           Dim HdnSal As HiddenField = TryCast(e.Row.FindControl("HdnSal"), HiddenField)

           If HdnSal.Value = Nothing Then   //Error getting : Object reference if HdnSal is Nothing
               chkSelect.Checked = False
           Else
               chkSelect.Checked = True
           End If
       End If
Posted
Updated 22-Dec-11 22:13pm
v2

1 solution

Instead of checking the Value of the field, check the field itself:

VB
If HdnSal.Value = Nothing Then
Becomes:
VB
If HdnSal Is Nothing OrElse HdnSal.Value Is Nothing Then
 
Share this answer
 
Comments
gani7787 23-Dec-11 4:24am    
Thanks Griff..
OriginalGriff 23-Dec-11 4:31am    
You're welcome!
koolprasad2003 23-Dec-11 4:27am    
Yes. Nothing should be checked with 'IS' keyword. 5.
Nice answer OriginalGriff

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