473,473 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Create 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 PopulateControls1()
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.ExecuteReader()

'Get the first line
r.Read()
Me.Label1.Text = "First Name"
Me.TextBox1.Text = r.Item("FirstName")
'Now get the new line
r.Read()
Me.Label2.Text = "First Name"
Me.TextBox2.Text = r.Item("FirstName")
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 3594
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).Rows(0)
Me.Label1.Text = "First Name"
Me.TextBox1.Text = _dr("FirstName").ToString()

_dr = _ds.Tables(0).Rows(1)
Me.Label2.Text = "First Name"
Me.TextBox2.Text = _dr("FirstName").ToString()

or, instead:

Me.Label1.Text = "First Name"

Me.TextBox1.Text = _ds.Tables(0).Rows(0)("FirstName").ToString()

Me.Label2.Text = "First Name"

Me.TextBox2.Text = _ds.Tables(0).Rows(1)("FirstName").ToString()
"SmartbizAustralia" <to*@smartbiz.com.auwrote in message
news:11********************@j44g2000cwa.googlegrou ps.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 PopulateControls1()
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.ExecuteReader()

'Get the first line
r.Read()
Me.Label1.Text = "First Name"
Me.TextBox1.Text = r.Item("FirstName")
'Now get the new line
r.Read()
Me.Label2.Text = "First Name"
Me.TextBox2.Text = r.Item("FirstName")
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(My.Settings.PTConnectionString)
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("Product").Rows
Dim ProductID As Integer = CType(dr.Item("ProductID"), Integer)
Dim ProductName As String = dr.Item("ProductName").ToString
Dim ProductNumber As String = dr.Item("ProductNumber").ToString
'Dim Price As Nullable(Of Decimal)
'If .Item("Price") IsNot DBNull.Value Then
' Price = CType(.Item("Price"), Decimal)
'End If
Dim Description As String = dr.Item("Description").ToString
'Dim ProductType As Integer = CType(.Item("ProductType"), Integer)
'Dim StockType As String = .Item("StockType").ToString
Console.WriteLine(String.Format("ProductID {0}, " & _
"ProductName {1}, {2}ProductNumber {3}, " & _
"Description {4}", ProductID, ProductName, _
ControlChars.CrLf, 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).TableName = "Product"
ds.Tables(1).TableName = "Customers"
Robin S.
---------------------------------------
"SmartbizAustralia" <to*@smartbiz.com.auwrote in message
news:11********************@j44g2000cwa.googlegrou ps.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 PopulateControls1()
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.ExecuteReader()

'Get the first line
r.Read()
Me.Label1.Text = "First Name"
Me.TextBox1.Text = r.Item("FirstName")
'Now get the new line
r.Read()
Me.Label2.Text = "First Name"
Me.TextBox2.Text = r.Item("FirstName")
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
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...
3
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...
4
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...
1
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...
2
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...
6
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...
7
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...
2
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 #...
13
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.