Hi All,
I am not able to read the values from Gridview TemplateField Controls.
basically I craeted a gridview with four colums and finally i added a Template field to it.
The templatefield contains 3 labels and one button while i am clicking on this button. i am getting a error message
Error "Object reference not set to an instance of an object" - .NET VB
Please suggest what should i do for thi problem
this is my Buttons.aspx Page
<%@ Page Language="VB" Debug="true" AutoEventWireup="false" CodeFile="button.aspx.vb" Inherits="button" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Buttons in Gridview</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="gid"
DataSourceID="SqlDataSource1" OnSelectedIndexChanged="GridView1_SelectedIndexCha nged">
<Columns>
<asp:BoundField DataField="fn" HeaderText="First Name" SortExpression="fn" />
<asp:BoundField DataField="ln" HeaderText="Last Name" SortExpression="ln" />
<asp:BoundField DataField="email" HeaderText="Email" SortExpression="email" />
<asp:BoundField DataField="regdate" HeaderText="Registered Date" SortExpression="regdate" />
<asp:TemplateField>
<ItemTemplate>
<asp:Label id="fn" runat="server" Text='<%# Eval("fn") %>'></asp:Label><br>
<asp:Label ID="ln" runat="server" Text='<%# Eval("ln") %>'>'></asp:Label><br>
<asp:Label ID="email" runat="server" Text='<%# Eval("email") %>'></asp:Label><br>
<asp:Label ID="rd" runat="server" Text='<%# Eval("regdate") %>'></asp:Label>
<asp:Button ID="Button1" runat="server" Text="Display" CommandName="disp"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:My_Connection_String %>"
SelectCommand="SELECT [fn], [ln], [email], [regdate], [gid] FROM [GLogin]"></asp:SqlDataSource>
</div>
</form>
</body>
</html>
And This is my code behind file
Imports System.Data.SqlClient
Imports System.Data
Partial Class button
Inherits System.Web.UI.Page
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs ) Handles GridView1.RowCommand
' Dim dr As DataRow
Dim index As Integer
index = Val(e.CommandArgument)
Dim sr As GridViewRow = GridView1.SelectedRow
If e.CommandName = "disp" Then
Dim l As String
MsgBox(sr.Cells(5).Controls(4).ID)
l = CType(sr.FindControl("fn"), Label).Text
MsgBox(e.CommandSource.ToString)
End If
End Sub
Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
End Class
I need a quick solution for this