Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 615
  • Last Modified:

Datakeys fetching Null values

Hi all,
I have posting this question from past week. i hope to get Answers today. i have had a good discussion with one of masters...but still hanging low..Refer the link below for the question
http://www.experts-exchange.com/Programming/Languages/Q_23886558.html#a22935029
Please help!!!!
0
SaraDob
Asked:
SaraDob
  • 2
1 Solution
 
madhevan_pillaiCommented:
Hi

try this sample code

aspx

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="DataGrid.aspx.vb" Inherits="Data_DataGrid" %>

<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:GridView ID="GrdLocation" runat="server"
DataKeyNames="LocId,Location"
autogeneratecolumns="false" AutoGenerateSelectButton="True"
onselectedindexchanging="GrdLocation_SelectedIndexChanging" AllowPaging="True"
AllowSorting="True">
<columns>
<asp:commandfield showselectbutton="true"
headertext="Select Location"/>
<asp:boundfield datafield="LocId"
headertext="LocId"/>
<asp:boundfield datafield="Location"
headertext="Location"/>
<asp:boundfield datafield="City"
headertext="City"/>
<asp:boundfield datafield="State"
headertext="State"/>
<asp:boundfield datafield="Country"
headertext="Country"/>
</columns>
 
</asp:GridView>
        <asp:TextBox ID="TextBox1" runat="server" Width="489px"></asp:TextBox>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    </form>
</body>
</html>


code behind


Partial Class Data_DataGrid
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If (Not Me.IsPostBack) Then
            Me.GrdLocation.DataSource = clsData.GetDataTable
            Me.GrdLocation.DataBind()
        End If
    End Sub

    Protected Sub GrdLocation_SelectedIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSelectEventArgs) Handles GrdLocation.SelectedIndexChanging
        Dim gid As Guid = CType(Me.GrdLocation.DataKeys(e.NewSelectedIndex)("LocId"), Guid)
        Me.TextBox1.Text = gid.ToString
        Me.TextBox2.Text = Me.GrdLocation.DataKeys(e.NewSelectedIndex)("Location").ToString


    End Sub
End Class



app_code


Imports System.Data

Public Class clsData
    Public Shared Function GetDataTable() As DataTable
        Dim dt As New DataTable
        dt.Columns.Add("LocId", GetType(Guid))
        dt.Columns.Add("Location")
        dt.Columns.Add("City")
        dt.Columns.Add("State")
        dt.Columns.Add("Country")

        dt.Rows.Add(New Object() {System.Guid.NewGuid, "aaaaa", "aaaaaaaaa", "aaaaaaaaaaaa", "aaaaaaaaaaaa"})
        dt.Rows.Add(New Object() {System.Guid.NewGuid, "bbbbb", "bbbbbbbbb", "bbbbbbbbbbbb", "bbbbbbbbbbbb"})
        dt.Rows.Add(New Object() {System.Guid.NewGuid, "ccccc", "ccccccccc", "cccccccccccc", "cccccccccccc"})
        dt.Rows.Add(New Object() {System.Guid.NewGuid, "ddddd", "ddddddddd", "dddddddddddd", "dddddddddddd"})

        Return dt
    End Function


    Public Shared Function GetDataTable1() As DataTable
        Dim dt As New DataTable
        dt.Columns.Add("LocId")
        dt.Columns.Add("Location")
        dt.Columns.Add("City")
        dt.Columns.Add("State")
        dt.Columns.Add("Country")

        dt.Rows.Add(New Object() {1, "aaaaa", "aaaaaaaaa", "aaaaaaaaaaaa", "aaaaaaaaaaaa"})
        dt.Rows.Add(New Object() {2, "bbbbb", "bbbbbbbbb", "bbbbbbbbbbbb", "bbbbbbbbbbbb"})
        dt.Rows.Add(New Object() {3, "ccccc", "ccccccccc", "cccccccccccc", "cccccccccccc"})
        dt.Rows.Add(New Object() {4, "ddddd", "ddddddddd", "dddddddddddd", "dddddddddddd"})

        Return dt
    End Function

End Class
0
 
madhevan_pillaiCommented:
please see this in ur code

    Dim LocId As Guid = CType(Me.GrdLocation.DataKeys(e.NewSelectedIndex)("LocID"),

 <asp:boundfield datafield="LocId"  headertext="LocId"/>

see the LocID case with LocId
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now