Solved

Having problems with syntax for looping through datarows in a datatable

Posted on 2006-06-19
6
344 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
This video discusses moving either the default database or any database to a new volume.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now