Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
This is my gridview row bound code

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 lcl_date As String = "d-MMM-yyyy"
If Session("lcl_date") IsNot Nothing Then
lcl_date = Session("lcl_date")
End If
Dim podate As DateTime = DataBinder.Eval(e.Row.DataItem, "podate")
Dim lbl5 As Label = CType(e.Row.FindControl("Label5"), Label)
Dim txpodate As TextBox = CType(e.Row.FindControl("txt_podate"), TextBox)
lbl5.Text = podate.ToString(lcl_date)
txpodate.text = podate.ToString(lcl_date)
End If

End Sub

XML
It was working fine if i do not add

Dim txtpodate as textbox and txtpodate.text

and corresponding asp.net code is as

<asp:TemplateField HeaderText="PO Date">

                               <ItemTemplate>
                                   <asp:Label ID="Label5" runat="server" Text='<%# Bind("podate") %>'></asp:Label>
                               </ItemTemplate>
                               <EditItemTemplate>
                                   <asp:TextBox ID="txt_podate" runat="server" Text='<%# Bind("podate") %>'></asp:TextBox>
                               </EditItemTemplate>
                           </asp:TemplateField>
but after adding this line in the gridview row databound, it displaying error as Object reference not set to instance of the object
Posted

1 solution

Error is coming because you are fetching control from EditItemTemplate.
You can find Control from EditItemTemplate as below.

VB
If ((e.Row.RowState & DataControlRowState.Edit)) Then  
  Dim txpodate As TextBox = CType(e.Row.FindControl("txt_podate"), TextBox)
  Response.Write(TextBox1.Text)
End If  
 
Share this answer
 

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