Solved

loop through data set to create Web service items

Posted on 2009-05-12
7
443 Views
Last Modified: 2012-06-21
I would like to Loop through a data set to create web service items.  

Below is an example of how I hard code items to be sent to the web service.  

I have a data set created from an SQL query, how can II loop through that data set and take the value of the first column in every data row and set that to the PERNR for a new item?

'Dim item(1) As TestWS.DataItem

        'item(0) = New TestWS.DataItem

        'item(0).PERNR = "00012191"

        'item(1) = New TestWS.DataItem

        'item(1).PERNR = "00011382"
 

        'Dim ws As New TestWS.DataService

        'ws.Credentials = New Net.NetworkCredential("UserName", "Password")

        'ws.Data(item)

Open in new window

0
Comment
Question by:DotNetNooby
  • 3
  • 2
  • 2
7 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
Comment Utility
Try the following code
Dim Items(dSet.Tables(0).Rows.Count) as New TestWS.DataItem
 

For i as integer = 0 to dSet.Tables(0).Rows.Count - 1

    Items(i) = dSet.Tables(0).Rows(i).Item(0)

Next
 

Dim ws As New TestWS.DataService

ws.Credentials = New Net.NetworkCredential("UserName", "Password")

ws.Data(items)

Open in new window

0
 
LVL 4

Expert Comment

by:rdoran0928
Comment Utility
You could do something like the following:

        Dim queryConnString As String = ConfigurationManager.ConnectionStrings("yourConnectionString").ConnectionString
        Dim queryConn As SqlConnection = New SqlConnection(queryConnString)
        Dim querySelect = SELECT pernrColumn FROM table
        Dim queryCmd As SqlCommand = New SqlCommand(querySelect, queryConn)
        Dim queryAdapter As SqlDataAdapter = New SqlDataAdapter(queryCmd)
        Dim queryDataSet As DataSet = New DataSet

        Try
            queryConn.Open()
            queryAdapter.Fill(queryDataSet, "QueryTable")

        Catch ex As Exception
             Put your exception handling code here

        Finally
            queryConn.Close()

        End Try

        ' traverse records and set PERNR in the item
        Dim queryRows As DataRowCollection = queryDataSet.Tables("QueryTable").Rows
        Dim i As Integer
        Dim curRow As DataRow

        For i = 0 To queryRows.Count - 1

            curRow = queryRows(i)

            item(i).PERNR = curRow("pernrColumn")

        Next


Best of luck!
0
 
LVL 83

Expert Comment

by:CodeCruiser
Comment Utility
@rdoran0928
It is inefficient code. Why do you need to use a curRow variable? Why not

For i = 0 To queryRows.Count - 1

            item(i).PERNR = queryRows(i)("pernrColumn")

Next
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 4

Expert Comment

by:rdoran0928
Comment Utility
Just for clarity, assuming a potentially more complex solution involving multiple columns.
0
 

Author Comment

by:DotNetNooby
Comment Utility
When I run this it throws an exception: Unable to cast object of type 'System.String' to type 'TestWS.DataItem'
0
 

Author Closing Comment

by:DotNetNooby
Comment Utility
worked great, Thanks!
0
 
LVL 83

Expert Comment

by:CodeCruiser
Comment Utility
Yeah because the following statement
Items(i) = dSet.Tables(0).Rows(i).Item(0)

is asssigning a string to DataItem. Do you know the structure of TestWS.DataItem? May be you should assign value to a property inside it like
Items(i).Text = dSet.Tables(0).Rows(i).Item(0)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

728 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

10 Experts available now in Live!

Get 1:1 Help Now