Solved

Having problems with syntax for looping through datarows in a datatable

Posted on 2006-06-19
6
349 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
Industry Leaders: 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!

 
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

756 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