Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.1
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 25th, 2003, 10:04 AM
Registered User
 
Join Date: Jul 2003
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default .net simple data binding to label

I want to bind an entry in an xml file to a label using vb.net. However, I can't seem to get it to work and there are few resources on the net that cover this, most just cover the data grid. Here is my code at this point, any pointers would be appreciated. Thanks.

<script language="VB" runat="Server">

Sub Page_Load(Sender As Object, E As EventArgs)

Dim ObjDataSet As New DataSet()

objDataSet.ReadXML(Server.MapPath("abstracts.xml") )
lblNameTwo.text = objDataSet.Tables(0).Rows(0)("abstractAuthor")
lblNameTwo.DataBind()

End Sub

</script>

 
Old July 25th, 2003, 11:01 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

If you are setting the Text property of the Label directly, there is no need to use DataBind.

This should work as well:

Code:
lblNameTwo.Text = objDataSet.Tables(0).Rows(0)("abstractAuthor").ToString()
(assuming that objDataSet contains at least one valid DataRow.

You can also get the first row of the DataSet, cast it to a DataRow and use that for simple binding. IMO, this makes your code a bit easier to read:

Code:
Dim objDataRow As DataRow = CType(objDataSet.Tables("MyTable").Rows(0), DataRow)
lblNameTwo.Text = objDataRow("MyTable").ToString()
HtH

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old July 28th, 2003, 08:37 AM
Registered User
 
Join Date: Jul 2003
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Neither of those seem to work. In the first suggestion:

"lblNameTwo.Text = objDataSet.Tables(0).Rows(0)("abstractAuthor").ToS tring()"

I get a server error, "Invalid byte was found at byte index 253."

In the second suggestion:

"Dim objDataRow As DataRow = CType(objDataSet.Tables("MyTable").Rows(0), DataRow)
lblNameTwo.Text = objDataRow("MyTable").ToString()"

I get an application error, "Object reference not set to an instance of an object."

These are similar to some of the errors I was getting last week, but I will keep hacking away at it. Thanks.

 
Old July 28th, 2003, 11:07 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 158
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to NotNowJohn
Default

Both code samples sent by Imar are OK (the second too, you have to change "MyTable" to "abstractAuthor"). It seems that you have the problem here with XML encoding. Can u send us abstracts.xml?

...but the Soon is eclipsed by the Moon
 
Old July 29th, 2003, 09:08 AM
Registered User
 
Join Date: Jul 2003
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sure, but there isn't much to it right now.

<abstract>
 <student>
  <abstractAuthor>Author Name</abstractAuthor>
  <abstractTitle>Paper Title</abstractTitle>
  <abstractContent>Paper Content
  </abstractContent>
 </student>
</abstract>







Similar Threads
Thread Thread Starter Forum Replies Last Post
Binding SQLDataSource To A Label Control sg225551 ASP.NET 2.0 Basics 1 January 16th, 2008 10:36 AM
ch02 - Binding to a Simple XML Document Tophat BOOK: Beginning ASP.NET 1.0 7 August 17th, 2007 05:09 AM
Binding Label Inside GridView to different soruce tna55 ASP.NET 2.0 Basics 50 February 19th, 2007 06:46 PM
BINDING DATA WITH SIMPLE CONTROLS saraswathyrajaram ASP.NET 1.0 and 1.1 Basics 13 August 29th, 2006 03:09 PM
Using ado.net and data binding Brettvan1 VB.NET 2002/2003 Basics 18 February 6th, 2006 01:39 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.