Solved

Having problems with syntax for looping through datarows in a datatable

Posted on 2006-06-19
6
348 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 …
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

856 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