473,804 Members | 2,959 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Looking for a simple explanation of how to walk through a dataset in .net 2.0

Hi,

This seems to be a neglected bit of info as everyone gets carried away
with data binding examples instead.

Can simply use the datareader as below:

Private Sub PopulateControl s1()
Dim sSql As String
Dim cn As SqlConnection

cn = New SqlConnection(m _sConnection)
cn.Open()
sSql = "Select top 10 * from Person.Contact"
Dim sqlCmd As New SqlCommand(sSql , cn)
Dim r As SqlDataReader = sqlCmd.ExecuteR eader()

'Get the first line
r.Read()
Me.Label1.Text = "First Name"
Me.TextBox1.Tex t = r.Item("FirstNa me")
'Now get the new line
r.Read()
Me.Label2.Text = "First Name"
Me.TextBox2.Tex t = r.Item("FirstNa me")
cn.Close()

End Sub

but would love to do the same with a dataset instead.

But how do you walk through each row and select particular columns in a
dataset?

Nov 29 '06 #1
2 3614
In the DataTable that is inside the DataSet, the rows are just an array of
DataRow and all the columns are available in each 'DataRow'.

Something like this should put you on the right track:

'Given as Dataset _ds having a single Datable

Dim _dr As DataRow = _ds.Tables(0).R ows(0)
Me.Label1.Text = "First Name"
Me.TextBox1.Tex t = _dr("FirstName" ).ToString()

_dr = _ds.Tables(0).R ows(1)
Me.Label2.Text = "First Name"
Me.TextBox2.Tex t = _dr("FirstName" ).ToString()

or, instead:

Me.Label1.Text = "First Name"

Me.TextBox1.Tex t = _ds.Tables(0).R ows(0)("FirstNa me").ToString ()

Me.Label2.Text = "First Name"

Me.TextBox2.Tex t = _ds.Tables(0).R ows(1)("FirstNa me").ToString ()
"SmartbizAustra lia" <to*@smartbiz.c om.auwrote in message
news:11******** ************@j4 4g2000cwa.googl egroups.com...
Hi,

This seems to be a neglected bit of info as everyone gets carried away
with data binding examples instead.

Can simply use the datareader as below:

Private Sub PopulateControl s1()
Dim sSql As String
Dim cn As SqlConnection

cn = New SqlConnection(m _sConnection)
cn.Open()
sSql = "Select top 10 * from Person.Contact"
Dim sqlCmd As New SqlCommand(sSql , cn)
Dim r As SqlDataReader = sqlCmd.ExecuteR eader()

'Get the first line
r.Read()
Me.Label1.Text = "First Name"
Me.TextBox1.Tex t = r.Item("FirstNa me")
'Now get the new line
r.Read()
Me.Label2.Text = "First Name"
Me.TextBox2.Tex t = r.Item("FirstNa me")
cn.Close()

End Sub

but would love to do the same with a dataset instead.

But how do you walk through each row and select particular columns in a
dataset?

Nov 29 '06 #2
This creates a DataSet; you can also use a DataTable.

---------------------------------
Dim ds As DataSet
'open the connection
Using cnn As New SqlConnection(M y.Settings.PTCo nnectionString)
cnn.Open()

'define the command
Dim cmd As New SqlCommand
cmd.Connection = cnn
cmd.CommandText = "SELECT * FROM Product"
'define the data adapter and fill the data table
Dim da As New SqlDataAdapter( cmd)
ds = New DataSet
da.Fill(ds, "Product")
End Using

For Each dr As DataRow In ds.Tables("Prod uct").Rows
Dim ProductID As Integer = CType(dr.Item(" ProductID"), Integer)
Dim ProductName As String = dr.Item("Produc tName").ToStrin g
Dim ProductNumber As String = dr.Item("Produc tNumber").ToStr ing
'Dim Price As Nullable(Of Decimal)
'If .Item("Price") IsNot DBNull.Value Then
' Price = CType(.Item("Pr ice"), Decimal)
'End If
Dim Description As String = dr.Item("Descri ption").ToStrin g
'Dim ProductType As Integer = CType(.Item("Pr oductType"), Integer)
'Dim StockType As String = .Item("StockTyp e").ToString
Console.WriteLi ne(String.Forma t("ProductID {0}, " & _
"ProductNam e {1}, {2}ProductNumbe r {3}, " & _
"Descriptio n {4}", ProductID, ProductName, _
ControlChars.Cr Lf, ProductNumber, Description))
Next
-------------------------------

You can also load a dataset with multiple tables.
To do this, your sql would have multiple sql statements
in yhour commandtext:

SELECT * FROM PRODUCT; SELECT * FROM CUSTOMERS

and then you name the tables like this:

ds.Tables(0).Ta bleName = "Product"
ds.Tables(1).Ta bleName = "Customers"
Robin S.
---------------------------------------
"SmartbizAustra lia" <to*@smartbiz.c om.auwrote in message
news:11******** ************@j4 4g2000cwa.googl egroups.com...
Hi,

This seems to be a neglected bit of info as everyone gets carried away
with data binding examples instead.

Can simply use the datareader as below:

Private Sub PopulateControl s1()
Dim sSql As String
Dim cn As SqlConnection

cn = New SqlConnection(m _sConnection)
cn.Open()
sSql = "Select top 10 * from Person.Contact"
Dim sqlCmd As New SqlCommand(sSql , cn)
Dim r As SqlDataReader = sqlCmd.ExecuteR eader()

'Get the first line
r.Read()
Me.Label1.Text = "First Name"
Me.TextBox1.Tex t = r.Item("FirstNa me")
'Now get the new line
r.Read()
Me.Label2.Text = "First Name"
Me.TextBox2.Tex t = r.Item("FirstNa me")
cn.Close()

End Sub

but would love to do the same with a dataset instead.

But how do you walk through each row and select particular columns in a
dataset?

Nov 29 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
3170
by: Matt | last post by:
Any progammers looking for a killer app to develop? How about a voice enabled forum? One of the most powerful, exciting, and engrossing experiences on the Internet is the Forum. The first great Internet forums were the Usenet newsgroups. Usenet is still a powerful force, but many different types of forums are also very popular (such as message boards like Vbulliten and XMBforum). I love forums. Love em love em love em. My web site...
3
1705
by: for.fun | last post by:
Hi everybody, I am looking for a XML comparison tool (I do not mean a standard char-by-char diff tool but a tool which understand XML syntax) More precisely, I can have serveral XML structures organized differently. The XML nodes can store the same data but be organized differently => in such a case, I would like the diff tool to tell me that both XML files are identicals.
4
1845
by: Wayne Wengert | last post by:
I am looking for pointers to good beginner books, tutorials or other resources to help me understand how to really use XML data. I program mostly in VB (I have several applications in VB6 but am just starting to convert to VB.NET). Most of my database backends are SQL Server 2000 with a few still Access. I want to educate myself so that I can code (and understand) processes like reading an XML file and adding the contained information to...
1
1688
by: jessebasketball | last post by:
i am a very beginner programmer and am stuck on a very simple problem. I am trying to make a game where the user is presented with a scenerio and options. ie you are standing in the parking lot a.run b.go home c.stay there when a option is selected a new option is presented.
2
8489
by: Don Wash | last post by:
Hi All! I've been searching everywhere for a simple sample of producing a bar graph using CrystalReport by specifying SQL Query, and I've found none of it! I find so many complex samples with so many features buried in the sample but all I want to know is a very simple thing, how to create bar charts with CrystalReport in VS.NET using SQL Query. Could anyone provide me a sample showing how to produce a simple bar chart using SQL Query...
6
13956
by: JohnR | last post by:
I have a table with 1 row which is used to hold some application wide items (one item per field, hence I only need 1 row). I want to bind one of the fields to a textbox. After setting up the oledbconnection and dataAdapter and filling the DataSet (ds) I tried this: TextBox1.DataBindings.Add("text", ds.Tables.Item("MyFile"), "MyField") I then put the following code in the SAVE button click event:
7
1098
by: Wayne Wengert | last post by:
I have a VB VSE 2005 project in which I need to rank about 35 columns of numbers. I want to end up with a dataset of ordinals. For example, if I have a dataset with 5 rows in which colA values are 6.6, 6.5, 6.2, 6.6, 6.7 respectively, the resulting ordinals would be 2, 4, 5, 2, 1. I am currntly doing this in a brute force way. I put the values in an array and then walk through each column to calculate the ordinal and place it in a second...
2
2871
by: Joe | last post by:
Hi I have a dataset with 3 tables and 2 relations Is there a way to when I am at 1 row to tell if there is a relation on that row ??? I have the code hardcoded but try to make it work if the # of tables and #relations increase or decrease So I can just pass any dataset and walk thru the rows?? Thanks
13
3118
by: Alan Silver | last post by:
Hello, MSDN (amongst other places) is full of helpful advice on ways to do data access, but they all seem geared to wards enterprise applications. Maybe I'm in a minority, but I don't have those sorts of clients. Mine are all small businesses whose sites will never reach those sorts of scales. I deal with businesses whose sites get maybe a few hundred visitors per day (some not even that much) and get no more than ten orders per day....
0
10318
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10302
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9132
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7608
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6845
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5639
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4277
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3803
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2976
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.