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 cnnRestaurantMa nager As SqlConnection
Dim traRestaurantMa nager As SqlTransaction
Dim cmmRestaurantMa nager As New SqlCommand
Dim drRestaurantMan ager As SqlDataReader
Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
'Initiate the connection
cnnRestaurantMa nager = New SqlConnection(" Data
Source=.\SQLEXP RESS;AttachDbFi lename=|DataDir ectory|\Restaur antManager.mdf; Integrated
Security=True;C onnect Timeout=30;User Instance=True")
'Open the connection
Try
cnnRestaurantMa nager.Open()
Catch ex As Exception
MsgBox("Startup failed, could not connect to SQL server")
Exit Sub
End Try
Try
'Start transaction
traRestaurantMa nager =
cnnRestaurantMa nager.BeginTran saction("Reserv ations")
'Initiate the command
cmmRestaurantMa nager.Connectio n = cnnRestaurantMa nager
cmmRestaurantMa nager.Transacti on = traRestaurantMa nager
'Perform Query
cmmRestaurantMa nager.CommandTe xt = "SELECT
reservationsTbl .ReservationID, customerTbl.Fir stName,
customerTbl.Las tName, reservationsTbl .Time, reservationsTbl .PartySize,
reservationsTbl .Date FROM customerTbl INNER JOIN reservationsTbl ON
customerTbl.Cus tomerID = reservationsTbl .CustomerID"
drRestaurantMan ager = cmmRestaurantMa nager.ExecuteRe ader()
Catch ex As Exception
MsgBox("An error occurred whilst rying to query the
database")
End Try
'Setup DataGridView1
With ReservationGrid
.ColumnCount = 6
.Columns(0).Nam e = "ID"
.Columns(0).Aut oSizeMode =
DataGridViewAut oSizeColumnMode .AllCells
.Columns(1).Nam e = "First Name"
.Columns(1).Aut oSizeMode =
DataGridViewAut oSizeColumnMode .AllCells
.Columns(2).Nam e = "Surname"
.Columns(2).Aut oSizeMode =
DataGridViewAut oSizeColumnMode .AllCells
.Columns(3).Nam e = "TOA"
.Columns(3).Aut oSizeMode =
DataGridViewAut oSizeColumnMode .AllCells
.Columns(4).Nam e = "Party Size"
.Columns(4).Aut oSizeMode =
DataGridViewAut oSizeColumnMode .AllCells
.Columns(5).Nam e = "Date"
.Columns(5).Aut oSizeMode =
DataGridViewAut oSizeColumnMode .AllCells
End With
'Read data from datareader and output to ReservationGrid
While drRestaurantMan ager.Read()
ReservationGrid .Rows.Add(drRes taurantManager. GetSqlInt32(0),
drRestaurantMan ager.GetString( 1), drRestaurantMan ager.GetString( 2),
drRestaurantMan ager.GetString( 3), drRestaurantMan ager.GetString( 4),
drRestaurantMan ager.GetSqlDate Time(5).ToStrin g())
End While
'Close datareader
drRestaurantMan ager.Close()
'Close Transaction
traRestaurantMa nager.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 cnnRestaurantMa nager As SqlConnection
Dim traRestaurantMa nager As SqlTransaction
Dim cmmRestaurantMa nager As New SqlCommand
Dim drRestaurantMan ager As SqlDataReader
Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
'Initiate the connection
cnnRestaurantMa nager = New SqlConnection(" Data
Source=.\SQLEXP RESS;AttachDbFi lename=|DataDir ectory|\Restaur antManager.mdf; Integrated
Security=True;C onnect Timeout=30;User Instance=True")
'Open the connection
Try
cnnRestaurantMa nager.Open()
Catch ex As Exception
MsgBox("Startup failed, could not connect to SQL server")
Exit Sub
End Try
Try
'Start transaction
traRestaurantMa nager =
cnnRestaurantMa nager.BeginTran saction("Reserv ations")
'Initiate the command
cmmRestaurantMa nager.Connectio n = cnnRestaurantMa nager
cmmRestaurantMa nager.Transacti on = traRestaurantMa nager
'Perform Query
cmmRestaurantMa nager.CommandTe xt = "SELECT
reservationsTbl .ReservationID, customerTbl.Fir stName,
customerTbl.Las tName, reservationsTbl .Time, reservationsTbl .PartySize,
reservationsTbl .Date FROM customerTbl INNER JOIN reservationsTbl ON
customerTbl.Cus tomerID = reservationsTbl .CustomerID"
drRestaurantMan ager = cmmRestaurantMa nager.ExecuteRe ader()
Catch ex As Exception
MsgBox("An error occurred whilst rying to query the
database")
End Try
'Setup DataGridView1
With ReservationGrid
.ColumnCount = 6
.Columns(0).Nam e = "ID"
.Columns(0).Aut oSizeMode =
DataGridViewAut oSizeColumnMode .AllCells
.Columns(1).Nam e = "First Name"
.Columns(1).Aut oSizeMode =
DataGridViewAut oSizeColumnMode .AllCells
.Columns(2).Nam e = "Surname"
.Columns(2).Aut oSizeMode =
DataGridViewAut oSizeColumnMode .AllCells
.Columns(3).Nam e = "TOA"
.Columns(3).Aut oSizeMode =
DataGridViewAut oSizeColumnMode .AllCells
.Columns(4).Nam e = "Party Size"
.Columns(4).Aut oSizeMode =
DataGridViewAut oSizeColumnMode .AllCells
.Columns(5).Nam e = "Date"
.Columns(5).Aut oSizeMode =
DataGridViewAut oSizeColumnMode .AllCells
End With
'Read data from datareader and output to ReservationGrid
While drRestaurantMan ager.Read()
ReservationGrid .Rows.Add(drRes taurantManager. GetSqlInt32(0),
drRestaurantMan ager.GetString( 1), drRestaurantMan ager.GetString( 2),
drRestaurantMan ager.GetString( 3), drRestaurantMan ager.GetString( 4),
drRestaurantMan ager.GetSqlDate Time(5).ToStrin g())
End While
'Close datareader
drRestaurantMan ager.Close()
'Close Transaction
traRestaurantMa nager.Commit()
End Sub
Comment