Professional Web Applications Themes

DataGridControl not populating - ASP.NET Data Grid Control

Hi, I'm attempting to load raw XML(XML string), p it and populate a text box, p it further into an ArrayList, then set the ArrayList as the DataSource of a DataGrid. I had no problem getting this to work in a VB.NET Windows App. But I can't seem to populate the DataGrid in an VB.NET Web App. When I run the Windows App, textbox is loaded and datagrid is populated. When I run the Web App, textbox is loaded, but I can't seem to figure out why the datagrid doesn't populate. Any help would be appreciated. This is my first ...

  1. #1

    Default DataGridControl not populating

    Hi,

    I'm attempting to load raw XML(XML string), p it and populate a
    text box, p it further into an ArrayList, then set the ArrayList
    as the DataSource of a DataGrid.

    I had no problem getting this to work in a VB.NET Windows App. But I
    can't seem to populate the DataGrid in an VB.NET Web App.

    When I run the Windows App, textbox is loaded and datagrid is
    populated.

    When I run the Web App, textbox is loaded, but I can't seem to figure
    out why the datagrid doesn't populate.

    Any help would be appreciated. This is my first .NET programming
    attempt. Any suggestions on overall programming technique is also
    appreciated.

    Thanks, Kev.

    Here are my two aspx and aspx.vb pages with their respetive code.

    **************
    WebForm1.aspx:
    **************
    <% Page Language="vb" AutoEventWireup="false"
    Codebehind="WebForm1.aspx.vb" Inherits="WebApplication2.WebForm1"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <title>WebForm1</title>
    <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema"
    content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body MS_POSITIONING="GridLayout">
    <form runat="server">
    <asp:TextBox id="TextBox1" style="Z-INDEX: 101; LEFT: 24px;
    POSITION: absolute; TOP: 24px" runat="server"
    Width="272px" Height="32px" Enabled="true"></asp:TextBox>
    <asp:DataGrid id="DataGrid1" style="Z-INDEX: 102; LEFT: 24px;
    POSITION: absolute; TOP: 64px" runat="server"
    Height="208px" Width="312px"></asp:DataGrid>
    </form>
    </body>
    </HTML>

    *****************
    WebForm1.aspx.vb:
    *****************
    Public Class WebForm1
    Inherits System.Web.UI.Page

    #Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub
    InitializeComponent()

    End Sub
    Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
    Protected WithEvents DataGrid1 As
    System.Web.UI.WebControls.DataGrid

    'NOTE: The following placeholder declaration is required by the
    Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Init
    'CODEGEN: This method call is required by the Web Form
    Designer
    'Do not modify it using the code editor.
    InitializeComponent()
    End Sub

    #End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    'Put user code to initialize the page here
    Dim MyXMLDoent As System.Xml.XmlDoent
    Dim MyXpath As String
    Dim MyNodeList As System.Xml.XmlNodeList
    Dim MyNode As System.Xml.XmlNode
    Dim x As Integer
    Dim y As Integer
    Dim myAL As New ArrayList
    Dim myTest As New clsTest

    DataGrid1.DataSource = Nothing

    MyXMLDoent = New System.Xml.XmlDoent

    MyXMLDoent.LoadXml("<list user-id='10.100.100.112'
    list-id='15357' list-description='Import from GIS'
    query-id='tnfhkxzhunwrhoah4nt5pc45-LIST' list-type='UWI'
    list-status='start' list-title='' list-datasource='3'
    value-1-type='varchar2' value-2-type='not-used'
    value-3-type='not-used' list-count='4'><list-element
    value-1='100072204721W400' value-2='' value-3=''/><list-element
    value-1='1W0131504721W400' value-2='' value-3=''/><list-element
    value-1='100151604721W400' value-2='' value-3=''/><list-element
    value-1='100091604721W400' value-2='' value-3=''/></list>")

    MyXpath = "/list/list-type"
    MyNodeList = MyXMLDoent.SelectNodes(MyXpath)
    MyNode = MyXMLDoent.SelectSingleNode(MyXpath)

    'Display in textbox
    TextBox1.Text = MyNode.InnerXml

    MyXpath = "/list/list-element"
    MyNodeList = MyXMLDoent.SelectNodes(MyXpath)

    For x = 0 To MyNodeList.Count - 1
    myTest = New clsTest
    myTest.A = MyNodeList.Item(x).Attributes(0).InnerXml
    myTest.B = MyNodeList.Item(x).Attributes(1).InnerXml
    myTest.C = MyNodeList.Item(x).Attributes(2).InnerXml
    myAL.Add(myTest)
    Next


    'Set Datasource for DataGrid
    DataGrid1.DataSource = myAL

    End Sub

    End Class

    Public Class clsTest

    Private m_strA As String
    Private m_strB As String
    Private m_strC As String

    Public Property A()

    Get

    Return m_strA

    End Get

    Set(ByVal Value)

    m_strA = Value

    End Set

    End Property

    Public Property B()

    Get

    Return m_strB

    End Get

    Set(ByVal Value)

    m_strB = Value

    End Set

    End Property

    Public Property C()

    Get

    Return m_strC

    End Get

    Set(ByVal Value)

    m_strC = Value

    End Set

    End Property

    End Class
    Kevin Guest

  2. #2

    Default Re: DataGridControl not populating

    You can check out this article,
    [url]http://www.extremeexperts.com/Net/Articles/BindingXMLtoDataGrid.aspx[/url]

    --
    Saravana
    Microsoft MVP - ASP.NET
    [url]www.extremeexperts.com[/url]



    "Kevin" <kevincyaletech.com> wrote in message
    news:7eb8328.0310221507.1998ab43posting.google.co m...
    > Hi,
    >
    > I'm attempting to load raw XML(XML string), p it and populate a
    > text box, p it further into an ArrayList, then set the ArrayList
    > as the DataSource of a DataGrid.
    >
    > I had no problem getting this to work in a VB.NET Windows App. But I
    > can't seem to populate the DataGrid in an VB.NET Web App.
    >
    > When I run the Windows App, textbox is loaded and datagrid is
    > populated.
    >
    > When I run the Web App, textbox is loaded, but I can't seem to figure
    > out why the datagrid doesn't populate.
    >
    > Any help would be appreciated. This is my first .NET programming
    > attempt. Any suggestions on overall programming technique is also
    > appreciated.
    >
    > Thanks, Kev.
    >
    > Here are my two aspx and aspx.vb pages with their respetive code.
    >
    > **************
    > WebForm1.aspx:
    > **************
    > <% Page Language="vb" AutoEventWireup="false"
    > Codebehind="WebForm1.aspx.vb" Inherits="WebApplication2.WebForm1"%>
    > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    > <HTML>
    > <HEAD>
    > <title>WebForm1</title>
    > <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
    > <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
    > <meta name="vs_defaultClientScript" content="JavaScript">
    > <meta name="vs_targetSchema"
    > content="http://schemas.microsoft.com/intellisense/ie5">
    > </HEAD>
    > <body MS_POSITIONING="GridLayout">
    > <form runat="server">
    > <asp:TextBox id="TextBox1" style="Z-INDEX: 101; LEFT: 24px;
    > POSITION: absolute; TOP: 24px" runat="server"
    > Width="272px" Height="32px" Enabled="true"></asp:TextBox>
    > <asp:DataGrid id="DataGrid1" style="Z-INDEX: 102; LEFT: 24px;
    > POSITION: absolute; TOP: 64px" runat="server"
    > Height="208px" Width="312px"></asp:DataGrid>
    > </form>
    > </body>
    > </HTML>
    >
    > *****************
    > WebForm1.aspx.vb:
    > *****************
    > Public Class WebForm1
    > Inherits System.Web.UI.Page
    >
    > #Region " Web Form Designer Generated Code "
    >
    > 'This call is required by the Web Form Designer.
    > <System.Diagnostics.DebuggerStepThrough()> Private Sub
    > InitializeComponent()
    >
    > End Sub
    > Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
    > Protected WithEvents DataGrid1 As
    > System.Web.UI.WebControls.DataGrid
    >
    > 'NOTE: The following placeholder declaration is required by the
    > Web Form Designer.
    > 'Do not delete or move it.
    > Private designerPlaceholderDeclaration As System.Object
    >
    > Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles MyBase.Init
    > 'CODEGEN: This method call is required by the Web Form
    > Designer
    > 'Do not modify it using the code editor.
    > InitializeComponent()
    > End Sub
    >
    > #End Region
    >
    > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles MyBase.Load
    > 'Put user code to initialize the page here
    > Dim MyXMLDoent As System.Xml.XmlDoent
    > Dim MyXpath As String
    > Dim MyNodeList As System.Xml.XmlNodeList
    > Dim MyNode As System.Xml.XmlNode
    > Dim x As Integer
    > Dim y As Integer
    > Dim myAL As New ArrayList
    > Dim myTest As New clsTest
    >
    > DataGrid1.DataSource = Nothing
    >
    > MyXMLDoent = New System.Xml.XmlDoent
    >
    > MyXMLDoent.LoadXml("<list user-id='10.100.100.112'
    > list-id='15357' list-description='Import from GIS'
    > query-id='tnfhkxzhunwrhoah4nt5pc45-LIST' list-type='UWI'
    > list-status='start' list-title='' list-datasource='3'
    > value-1-type='varchar2' value-2-type='not-used'
    > value-3-type='not-used' list-count='4'><list-element
    > value-1='100072204721W400' value-2='' value-3=''/><list-element
    > value-1='1W0131504721W400' value-2='' value-3=''/><list-element
    > value-1='100151604721W400' value-2='' value-3=''/><list-element
    > value-1='100091604721W400' value-2='' value-3=''/></list>")
    >
    > MyXpath = "/list/list-type"
    > MyNodeList = MyXMLDoent.SelectNodes(MyXpath)
    > MyNode = MyXMLDoent.SelectSingleNode(MyXpath)
    >
    > 'Display in textbox
    > TextBox1.Text = MyNode.InnerXml
    >
    > MyXpath = "/list/list-element"
    > MyNodeList = MyXMLDoent.SelectNodes(MyXpath)
    >
    > For x = 0 To MyNodeList.Count - 1
    > myTest = New clsTest
    > myTest.A = MyNodeList.Item(x).Attributes(0).InnerXml
    > myTest.B = MyNodeList.Item(x).Attributes(1).InnerXml
    > myTest.C = MyNodeList.Item(x).Attributes(2).InnerXml
    > myAL.Add(myTest)
    > Next
    >
    >
    > 'Set Datasource for DataGrid
    > DataGrid1.DataSource = myAL
    >
    > End Sub
    >
    > End Class
    >
    > Public Class clsTest
    >
    > Private m_strA As String
    > Private m_strB As String
    > Private m_strC As String
    >
    > Public Property A()
    >
    > Get
    >
    > Return m_strA
    >
    > End Get
    >
    > Set(ByVal Value)
    >
    > m_strA = Value
    >
    > End Set
    >
    > End Property
    >
    > Public Property B()
    >
    > Get
    >
    > Return m_strB
    >
    > End Get
    >
    > Set(ByVal Value)
    >
    > m_strB = Value
    >
    > End Set
    >
    > End Property
    >
    > Public Property C()
    >
    > Get
    >
    > Return m_strC
    >
    > End Get
    >
    > Set(ByVal Value)
    >
    > m_strC = Value
    >
    > End Set
    >
    > End Property
    >
    > End Class

    Saravana [MVP] Guest

  3. #3

    Default Re: DataGridControl not populating

    You have to Bind the data before the page is rendered. This allows
    you to control when the data is actually attached to the object. In
    this case, you've just told it where it's DataSource is, but you
    haven't actually bound the data to the datagrid object yet.
    Try:
    DataGrid1.DataSource = myAL
    DataGrid1.DataBind()

    [email]kevincyaletech.com[/email] (Kevin) wrote in message news:<7eb8328.0310221507.1998ab43posting.google.c om>...
    >
    > 'Set Datasource for DataGrid
    > DataGrid1.DataSource = myAL
    >
    > End Sub
    Zachary Heilman Guest

  4. #4

    Default Re: DataGridControl not populating

    I thought that the '.DataBind()' was what was missing too. However,
    when I added that line of code and ran the application, I would
    receive the following error message:

    "DataGrid with id 'DataGrid1' could not automatically generate any
    columns from the selected data source"

    Basically, I saw my problems as being the inability of myself to load
    XML Node Attribute values into a DataGrid, the inability of myself to
    load a ListArray into a DataGrid, as well as, not configuring the
    links between the aspx and aspx.vb pages.

    saravana's sample code URL helped alot:
    [url]http://www.extremeexperts.com/Net/Articles/BindingXMLtoDataGrid.aspx[/url]

    I ended up using an XMLDataDoent and re-created a XML doc using the
    respective Attribute values in the aspx.vb page. And I implemented
    custom Template Columns and explicity referenced data from the
    datagrid in the aspx page.

    Thanks for everyone's help.

    Here's my updated code:

    *************
    WebForm1.aspx
    *************
    <% Page Language="vb" AutoEventWireup="false"
    Codebehind="WebForm1.aspx.vb" Inherits="WebApplication2.WebForm1"%>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <title>WebForm1</title>
    <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema"
    content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body MS_POSITIONING="GridLayout">
    <form runat="server">
    <asp:TextBox id="TextBox1" style="Z-INDEX: 101; LEFT: 24px;
    POSITION: absolute; TOP: 24px" runat="server"
    Width="272px" Height="32px" Enabled="true"></asp:TextBox>
    <asp:DataGrid id="DataGrid1" style="Z-INDEX: 102; LEFT: 24px;
    POSITION: absolute; TOP: 64px" runat="server"
    Height="208px" Width="312px" AutoGenerateColumns="false">
    <Columns>
    <asp:TemplateColumn headertext="Value1" >
    <ItemTemplate >
    <asp:TextBox style= "width:200px;"
    id="Value1" runat= "server"
    Text='<%#
    CType(Container.DataItem,System.XML.XMLNode)("valu e-1").InnerText%>' >
    </asp:TextBox>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn headertext="Value2">
    <ItemTemplate>
    <asp:TextBox style="width:200px;"
    id="Value2" runat="server"
    Text='<%#
    CType(Container.DataItem,System.XML.XMLNode)("valu e-2").InnerText%>' >
    </asp:TextBox>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn headertext="Value3">
    <ItemTemplate>
    <asp:TextBox style="width:200px;"
    id="Value3" runat="server"
    Text='<%#
    CType(Container.DataItem,System.XML.XMLNode)("valu e-3").InnerText%>' >
    </asp:TextBox>
    </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    </asp:datagrid>

    </form>
    </body>
    </HTML>


    ****************
    WebForm1.aspx.vb
    ****************
    Imports System
    Imports System.XML

    Public Class WebForm1
    Inherits System.Web.UI.Page


    #Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub
    InitializeComponent()

    End Sub
    Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
    Protected WithEvents DataGrid1 As
    System.Web.UI.WebControls.DataGrid

    'NOTE: The following placeholder declaration is required by the
    Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Init
    'CODEGEN: This method call is required by the Web Form
    Designer
    'Do not modify it using the code editor.
    InitializeComponent()
    End Sub

    #End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load

    If Not IsPostBack Then
    Dim oXML As New XmlDataDoent
    Dim strXpath As String
    Dim oNodeList As XmlNodeList
    Dim oNode As XmlNode
    Dim strXML As String
    Dim i As Integer

    oXML.LoadXml("<list user-id='10.101.24.112'
    list-id='15357' list-description='Import from NexenGIS'
    query-id='tnfhkxzhunwrhoah4nt5pc45-LIST' list-type='UWI'
    list-status='start' list-title='' list-datasource='3'
    value-1-type='varchar2' value-2-type='not-used'
    value-3-type='not-used' list-count='4'><list-element
    value-1='100072204721W400' value-2='' value-3=''/><list-element
    value-1='1W0131504721W400' value-2='' value-3=''/><list-element
    value-1='100151604721W400' value-2='' value-3=''/><list-element
    value-1='100091604721W400' value-2='' value-3=''/></list>")
    strXpath = "/list/list-type"
    oNodeList = oXML.SelectNodes(strXpath)
    oNode = oXML.SelectSingleNode(strXpath)

    'Display in textbox
    TextBox1.Text = oNode.InnerXml

    strXpath = "/list/list-element"
    oNodeList = oXML.SelectNodes(strXpath)

    'Create the new XML string to load into the DataGrid
    strXML = "<?xml version='1.0' standalone='yes'?>
    <DataSet>"
    For i = 0 To oNodeList.Count - 1
    strXML = strXML & "<list-element> <value-1>" &
    oNodeList.Item(i).Attributes(0).InnerXml & "</value-1>"
    strXML = strXML & "<value-2>" &
    oNodeList.Item(i).Attributes(1).InnerXml & "</value-2>"
    strXML = strXML & "<value-3>" &
    oNodeList.Item(i).Attributes(2).InnerXml & "</value-3>
    </list-element>"
    Next
    strXML = strXML & "</DataSet>"

    oXML = Nothing
    oXML = New XmlDataDoent
    oXML.LoadXml(strXML)
    strXpath = "/DataSet/list-element"
    DataGrid1.DataSource = oXML.SelectNodes(strXpath)
    DataGrid1.DataBind()

    End If

    End Sub

    End Class
    Kevin Guest

Similar Threads

  1. Populating a <mx:List> with XML
    By dzlmbq in forum Macromedia Flex General Discussion
    Replies: 2
    Last Post: March 5th, 10:41 AM
  2. Populating a DG with a Dir Listing
    By rottmanja in forum Macromedia Flex General Discussion
    Replies: 0
    Last Post: January 20th, 04:35 PM
  3. Populating an SQL query
    By Dave the brewer in forum Macromedia Dynamic HTML
    Replies: 0
    Last Post: October 8th, 04:23 PM
  4. populating a datagrid
    By viktor9990 in forum ASP.NET Data Grid Control
    Replies: 5
    Last Post: March 30th, 03:51 PM
  5. populating DB
    By Black_Prince in forum Macromedia Flash Actionscript
    Replies: 0
    Last Post: February 5th, 09:20 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139