J
jimmy
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=|DataDirectory|\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("Reservations")
'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.GetSqlInt32(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
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=|DataDirectory|\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("Reservations")
'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.GetSqlInt32(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