Solved

Having problems with syntax for looping through datarows in a datatable

Posted on 2006-06-19
6
347 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
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 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