DataGridControl not populating

K

Kevin

Hi,

I'm attempting to load raw XML(XML string), parse it and populate a
text box, parse 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 MyXMLDocument As System.Xml.XmlDocument
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

MyXMLDocument = New System.Xml.XmlDocument

MyXMLDocument.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 = MyXMLDocument.SelectNodes(MyXpath)
MyNode = MyXMLDocument.SelectSingleNode(MyXpath)

'Display in textbox
TextBox1.Text = MyNode.InnerXml

MyXpath = "/list/list-element"
MyNodeList = MyXMLDocument.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
 
Z

Zachary Heilman

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()
 
K

Kevin

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:
http://www.extremeexperts.com/Net/Articles/BindingXMLtoDataGrid.aspx

I ended up using an XMLDataDocument 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)("value-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)("value-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)("value-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 XmlDataDocument
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 XmlDataDocument
oXML.LoadXml(strXML)
strXpath = "/DataSet/list-element"
DataGrid1.DataSource = oXML.SelectNodes(strXpath)
DataGrid1.DataBind()

End If

End Sub

End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top