473,394 Members | 1,794 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Working with a database

Hi,

I currently have the following code in my program which loads data into
a data reader and then adds it to a DataGridView. Using the datareader
i cant however delete and modify the data because it is read only. How
can i do the same as what im doing except with a DataSet. Please not in
the query there are some joins which i have not been able to get to
work with a dataset so far.

'Declare variables
Dim cnnRestaurantManager As SqlConnection
Dim traRestaurantManager As SqlTransaction
Dim cmmRestaurantManager As New SqlCommand
Dim drRestaurantManager As SqlDataReader

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Initiate the connection
cnnRestaurantManager = New SqlConnection("Data
Source=.\SQLEXPRESS;AttachDbFilename=|DataDirector y|\RestaurantManager.mdf;Integrated
Security=True;Connect Timeout=30;User Instance=True")
'Open the connection
Try
cnnRestaurantManager.Open()
Catch ex As Exception
MsgBox("Startup failed, could not connect to SQL server")
Exit Sub
End Try

Try
'Start transaction
traRestaurantManager =
cnnRestaurantManager.BeginTransaction("Reservation s")
'Initiate the command
cmmRestaurantManager.Connection = cnnRestaurantManager
cmmRestaurantManager.Transaction = traRestaurantManager
'Perform Query
cmmRestaurantManager.CommandText = "SELECT
reservationsTbl.ReservationID, customerTbl.FirstName,
customerTbl.LastName, reservationsTbl.Time, reservationsTbl.PartySize,
reservationsTbl.Date FROM customerTbl INNER JOIN reservationsTbl ON
customerTbl.CustomerID = reservationsTbl.CustomerID"
drRestaurantManager = cmmRestaurantManager.ExecuteReader()
Catch ex As Exception
MsgBox("An error occurred whilst rying to query the
database")
End Try

'Setup DataGridView1
With ReservationGrid
.ColumnCount = 6
.Columns(0).Name = "ID"
.Columns(0).AutoSizeMode =
DataGridViewAutoSizeColumnMode.AllCells
.Columns(1).Name = "First Name"
.Columns(1).AutoSizeMode =
DataGridViewAutoSizeColumnMode.AllCells
.Columns(2).Name = "Surname"
.Columns(2).AutoSizeMode =
DataGridViewAutoSizeColumnMode.AllCells
.Columns(3).Name = "TOA"
.Columns(3).AutoSizeMode =
DataGridViewAutoSizeColumnMode.AllCells
.Columns(4).Name = "Party Size"
.Columns(4).AutoSizeMode =
DataGridViewAutoSizeColumnMode.AllCells
.Columns(5).Name = "Date"
.Columns(5).AutoSizeMode =
DataGridViewAutoSizeColumnMode.AllCells
End With

'Read data from datareader and output to ReservationGrid
While drRestaurantManager.Read()

ReservationGrid.Rows.Add(drRestaurantManager.GetSq lInt32(0),
drRestaurantManager.GetString(1), drRestaurantManager.GetString(2),
drRestaurantManager.GetString(3), drRestaurantManager.GetString(4),
drRestaurantManager.GetSqlDateTime(5).ToString())
End While
'Close datareader
drRestaurantManager.Close()
'Close Transaction
traRestaurantManager.Commit()
End Sub

Dec 2 '06 #1
2 1542
Jimmy,

Probably are you coming from the ASP side, where the datareader is often
used to populate the grids.

In the windowforms way is for that the datatable, which fullfils that 1:1
and has everything around it in dotNet to do things as by instance updating.

To see a simple sample how to use that, see this page.

http://www.vb-tips.com/dbpages.aspx?...2-b1ed16424252

I hope this helps,

Cor
"jimmy" <ja**************@tiscali.co.ukschreef in bericht
news:11**********************@80g2000cwy.googlegro ups.com...
Hi,

I currently have the following code in my program which loads data into
a data reader and then adds it to a DataGridView. Using the datareader
i cant however delete and modify the data because it is read only. How
can i do the same as what im doing except with a DataSet. Please not in
the query there are some joins which i have not been able to get to
work with a dataset so far.

'Declare variables
Dim cnnRestaurantManager As SqlConnection
Dim traRestaurantManager As SqlTransaction
Dim cmmRestaurantManager As New SqlCommand
Dim drRestaurantManager As SqlDataReader

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Initiate the connection
cnnRestaurantManager = New SqlConnection("Data
Source=.\SQLEXPRESS;AttachDbFilename=|DataDirector y|\RestaurantManager.mdf;Integrated
Security=True;Connect Timeout=30;User Instance=True")
'Open the connection
Try
cnnRestaurantManager.Open()
Catch ex As Exception
MsgBox("Startup failed, could not connect to SQL server")
Exit Sub
End Try

Try
'Start transaction
traRestaurantManager =
cnnRestaurantManager.BeginTransaction("Reservation s")
'Initiate the command
cmmRestaurantManager.Connection = cnnRestaurantManager
cmmRestaurantManager.Transaction = traRestaurantManager
'Perform Query
cmmRestaurantManager.CommandText = "SELECT
reservationsTbl.ReservationID, customerTbl.FirstName,
customerTbl.LastName, reservationsTbl.Time, reservationsTbl.PartySize,
reservationsTbl.Date FROM customerTbl INNER JOIN reservationsTbl ON
customerTbl.CustomerID = reservationsTbl.CustomerID"
drRestaurantManager = cmmRestaurantManager.ExecuteReader()
Catch ex As Exception
MsgBox("An error occurred whilst rying to query the
database")
End Try

'Setup DataGridView1
With ReservationGrid
.ColumnCount = 6
.Columns(0).Name = "ID"
.Columns(0).AutoSizeMode =
DataGridViewAutoSizeColumnMode.AllCells
.Columns(1).Name = "First Name"
.Columns(1).AutoSizeMode =
DataGridViewAutoSizeColumnMode.AllCells
.Columns(2).Name = "Surname"
.Columns(2).AutoSizeMode =
DataGridViewAutoSizeColumnMode.AllCells
.Columns(3).Name = "TOA"
.Columns(3).AutoSizeMode =
DataGridViewAutoSizeColumnMode.AllCells
.Columns(4).Name = "Party Size"
.Columns(4).AutoSizeMode =
DataGridViewAutoSizeColumnMode.AllCells
.Columns(5).Name = "Date"
.Columns(5).AutoSizeMode =
DataGridViewAutoSizeColumnMode.AllCells
End With

'Read data from datareader and output to ReservationGrid
While drRestaurantManager.Read()

ReservationGrid.Rows.Add(drRestaurantManager.GetSq lInt32(0),
drRestaurantManager.GetString(1), drRestaurantManager.GetString(2),
drRestaurantManager.GetString(3), drRestaurantManager.GetString(4),
drRestaurantManager.GetSqlDateTime(5).ToString())
End While
'Close datareader
drRestaurantManager.Close()
'Close Transaction
traRestaurantManager.Commit()
End Sub

Dec 2 '06 #2
Here's an example:

Dim ds As DataSet
'open the connection
Using cnn As New SqlConnection(My.Settings.ProductConnectionString)
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 Description As String = dr.Item("Description").ToString
Console.WriteLine(String.Format("ProductID {0}, " & _
"ProductName {1}, {2}ProductNumber {3}, " & _
"Description {4}", ProductID, ProductName, _
ControlChars.CrLf, ProductNumber, Description))
Next
Robin S.
-----------------------------------
"jimmy" <ja**************@tiscali.co.ukwrote in message
news:11**********************@80g2000cwy.googlegro ups.com...
Hi,

I currently have the following code in my program which loads data into
a data reader and then adds it to a DataGridView. Using the datareader
i cant however delete and modify the data because it is read only. How
can i do the same as what im doing except with a DataSet. Please not in
the query there are some joins which i have not been able to get to
work with a dataset so far.

'Declare variables
Dim cnnRestaurantManager As SqlConnection
Dim traRestaurantManager As SqlTransaction
Dim cmmRestaurantManager As New SqlCommand
Dim drRestaurantManager As SqlDataReader

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Initiate the connection
cnnRestaurantManager = New SqlConnection("Data
Source=.\SQLEXPRESS;AttachDbFilename=|DataDirector y|\RestaurantManager.mdf;Integrated
Security=True;Connect Timeout=30;User Instance=True")
'Open the connection
Try
cnnRestaurantManager.Open()
Catch ex As Exception
MsgBox("Startup failed, could not connect to SQL server")
Exit Sub
End Try

Try
'Start transaction
traRestaurantManager =
cnnRestaurantManager.BeginTransaction("Reservation s")
'Initiate the command
cmmRestaurantManager.Connection = cnnRestaurantManager
cmmRestaurantManager.Transaction = traRestaurantManager
'Perform Query
cmmRestaurantManager.CommandText = "SELECT
reservationsTbl.ReservationID, customerTbl.FirstName,
customerTbl.LastName, reservationsTbl.Time, reservationsTbl.PartySize,
reservationsTbl.Date FROM customerTbl INNER JOIN reservationsTbl ON
customerTbl.CustomerID = reservationsTbl.CustomerID"
drRestaurantManager = cmmRestaurantManager.ExecuteReader()
Catch ex As Exception
MsgBox("An error occurred whilst rying to query the
database")
End Try

'Setup DataGridView1
With ReservationGrid
.ColumnCount = 6
.Columns(0).Name = "ID"
.Columns(0).AutoSizeMode =
DataGridViewAutoSizeColumnMode.AllCells
.Columns(1).Name = "First Name"
.Columns(1).AutoSizeMode =
DataGridViewAutoSizeColumnMode.AllCells
.Columns(2).Name = "Surname"
.Columns(2).AutoSizeMode =
DataGridViewAutoSizeColumnMode.AllCells
.Columns(3).Name = "TOA"
.Columns(3).AutoSizeMode =
DataGridViewAutoSizeColumnMode.AllCells
.Columns(4).Name = "Party Size"
.Columns(4).AutoSizeMode =
DataGridViewAutoSizeColumnMode.AllCells
.Columns(5).Name = "Date"
.Columns(5).AutoSizeMode =
DataGridViewAutoSizeColumnMode.AllCells
End With

'Read data from datareader and output to ReservationGrid
While drRestaurantManager.Read()

ReservationGrid.Rows.Add(drRestaurantManager.GetSq lInt32(0),
drRestaurantManager.GetString(1), drRestaurantManager.GetString(2),
drRestaurantManager.GetString(3), drRestaurantManager.GetString(4),
drRestaurantManager.GetSqlDateTime(5).ToString())
End While
'Close datareader
drRestaurantManager.Close()
'Close Transaction
traRestaurantManager.Commit()
End Sub

Dec 2 '06 #3

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

Similar topics

3
by: Chris Tilley - HPC:Factor | last post by:
Hi, I'm utterly confounded by this one. There must be some sort of rule that I don't know of. I'd consider myself a Newbie+1, so be gentle. I have a database connection (working A-Ok) and a...
5
by: Andrew DeFaria | last post by:
I created the following .sql file to demonstrate a problem I'm having. According to the manual: If |ON DELETE CASCADE| is specified, and a row in the parent table is deleted, then InnoDB...
2
by: RC | last post by:
I am updating/improving a working database for a non-profit organization. I am thinking of making a copy of the database at the office, bringing the copy to my house, making the changes and then...
0
by: Viorel | last post by:
Working as a beginner with data objects in Visual Studio 2003 and C#, I use the "Generate Dataset" command in order to generate automatically the dataset objects based on data adapters. Generated...
2
by: Nikhil Patel | last post by:
Hi all, I installed an asp.net application on a client's machine on Friday. It uses a Sql Server database as backend. Database server and Web server are on different machines but same domain. The...
14
by: Mark B | last post by:
Our webhost (www.usbusinessweb.net) had a W2K IIS5 server crash after a scheduled hard-boot occurred during a ms-security patch install overnight. They couldn't get the server working again so they...
3
by: Jason Huang | last post by:
Hi, In our C# Windows Form application, we are using the SQL Server 2000 as the database server. The Database table MyTable has a field RegistrationDate which represents the Date a client comes...
7
by: vamichael | last post by:
When I try to run the Website Administration Tool from my published application using http://localhost/myapp/webadmin.axd I am getting a "resource not found" error message. I can use the...
22
by: Bob and Sharon Hiller | last post by:
I have an ASP page that was done in VBScript It is setup to read an Access database and I need to change it to read a Sql 2005 Database. The code that is used to open the Access Database: Set...
2
by: Sheikko | last post by:
ADO.NET - Working with DataSet ========================= HI, I want to create an application that interact with a DB. I wont to use SQL statement, but I want to use DataSet. - I have created my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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...

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.