Solved

Having problems with syntax for looping through datarows in a datatable

Posted on 2006-06-19
6
350 Views
Last Modified: 2008-02-01
I for some reason am having a small problem with the syntax for looping through datarows in a datatable.

say the dataset is "mydataset" and the table in the dataset is called "mydatatable"

My code looks like the following:

     for each dr as datarow in mydataset.mydatatable

However vs2005 keeps automatically making it look like:

     for each dr as datarow in mydataset.mydatatableRow

It keeps adding that "Row" to the end of it. And I am new to asp.net but I am pretty sure that "mydatatable" is setup as a datatable.

The way I set this up is by adding a new dataset under the app_code folder in my site, and then used the tableadapter configuration wizard to make a datatable. At least that is my understanding of what I have done. And I know that the tableadapter works correctly with my connection string because I can use the same datatable (or tableadapter... I am confused on any difference if any) with an objectdatasource and fill a gridview.

Thanks
Kristopher
0
Comment
Question by:mynameiskristopher
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 9

Accepted Solution

by:
tolgaong earned 300 total points
ID: 16936997
I think you should try the syntax:

for each dr as datarow in mydataset.Tables("mydatatable")
0
 

Author Comment

by:mynameiskristopher
ID: 16937123
This should work... however I get a Error "Reference to a non-shared member requires an object reference." Does this mean I have to reference somewhere on the page the dataset "mydataset"?

Or is for some reason the dataset is not shared!?
0
 

Author Comment

by:mynameiskristopher
ID: 16937919
Okay... to be precise... this is what I am doing:

    Public Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ' Create a new file in C:\\ dir
        Dim textWriter As XmlTextWriter = New XmlTextWriter("C:\code\myXmlFile.xml", Nothing)

        ' Opens the document
        textWriter.WriteStartDocument()

        'Write RSS version
        textWriter.WriteStartElement("rss")
        textWriter.WriteAttributeString("version", "2.0")

        textWriter.WriteStartElement("channel")
        textWriter.WriteElementString("title", "MyTitle")
        textWriter.WriteElementString("link", "MyLink")
        textWriter.WriteElementString("description", "MyDescription")

        'Start Cabin element
        textWriter.WriteStartElement("item")
        textWriter.WriteElementString("title", "itemTitle")
        textWriter.WriteElementString("link", "itemLink")
        textWriter.WriteElementString("description", "itemDesc")

        Dim dr As DataRow
        For Each dr In MyDataSet.Tables("MyDataTable")
            textWriter.WriteStartElement("image_link")
            textWriter.WriteAttributeString("http://www.domain.com/" & dr.Item(0))
        Next


        ' Ends the document.
        textWriter.WriteEndDocument()
        ' close writer
        textWriter.Close()
    End Sub


And for some reason the intellisense is highlighting: MyDataSet.Tables and says "Reference to a non-shared member requires an object reference." But it doesn't make sense to me... since I thought setting up the dataset in the app_code folder will make it available from anywhere in my project.

Or is my syntax still wrong for this?
And the whole page works and will write the xml file if the for loop is taken out.

This is killing me... I am searching everything on here, and googling and everything I can not find a solution... I can usually always find a solution within 10 minutes... and this is taking me hours and hours.
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 14

Expert Comment

by:jjardine
ID: 16938144
Where are you create   MyDataSet by saying    Dim MyDataSet as new DataSet?  It doesn't look like the dataset is instantiated and that is why you are not getting access to the Tables. Method.
0
 
LVL 11

Assisted Solution

by:anyoneis
anyoneis earned 200 total points
ID: 16938172
MyDataTable does not have an iterator. Try  

for each dr as datarow in mydataset.mydatatable.Rows

David
0
 

Author Comment

by:mynameiskristopher
ID: 16938400
Thanks tolgaong and jjardine both of you answered my question about the correct syntax for looping through the datarows of the table in the dataset.

My problems lie elswhere for the error "Reference to a non-shared member requires an object reference."

So I will just post another question about that tomorrow.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question