How to insert,delete,update datagrids to MS Access
Hey guys how is everyone today?
I have a small issue with datagrids so far I'm able to retrieve data from MS access and populate the grid. The user let say puts in an id number and the grids
gets populated...now I want the user to be able to edit certain attributes from grid itself for example (lastname ,firstname, data of birth etc)
I am able to actually edit in the grid, but I don't know how to rebind it back once I press a submit button or better yet how am i able to reconnect all the new
fresh data that has been either inserted,deleted,update in the grid itself.
There are much more fields in this grid, but I decided to keep it nice and simple by using only 4 fields .
Well look at my noobish code and tell me what you think guys?
////////////////
Dim connString As String = "Provider = Microsoft.JET.OLEDB.4.0; Data Source = X:\members.mdb; "
Dim myConnection As OleDbConnection = New OleDbConnection
Dim insertCmd As OleDbCommand = New OleDbCommand
myConnection.ConnectionString = connString
myConnection.Open()
With insertCmd
.Connection = myConnection
.CommandText = " Select CreditApp_ID,FirstName,MiddleName,LastName,DOB From CreditApplication " & _
"Where CreditApp_ID=@CreditApp_ID"
.CommandType = CommandType.Text
.Parameters.Add(New OleDbParameter("@CreditApp_ID", OleDbType.Char, 10))
.Parameters.Add(New OleDbParameter("@FirstName", OleDbType.Char, 30))
.Parameters.Add(New OleDbParameter("@MiddleName", OleDbType.Char, 1))
.Parameters.Add(New OleDbParameter("@LastName", OleDbType.Char, 30))
.Parameters.Add(New OleDbParameter("@DOB", OleDbType.Date))
.Parameters("@CreditApp_ID").Value = CreditID
.Parameters("@FirstName").Value = CreditID
.Parameters("@MiddleName").Value = CreditID
.Parameters("@LastName").Value = CreditID
End With
'Now to declare a OLEDBDataAdapter object
Dim Adapter As New OleDbDataAdapter
'Now to declare a dataset
Dim DataSet As New DataSet
'Now to apply my Command to the DataAdapter
Adapter.SelectCommand = insertCmd
Try
Adapter.Fill(DataSet)
'To bind the Dataset to the DataGrid :)
grdResults.DataSource = DataSet
'Tell the DataGrid which table in the Dataset to use
grdResults.DataMember = DataSet.Tables(0).TableName
'Setting the Alternating Colors property to the Grid
grdResults.AlternatingBackColor = Color.WhiteSmoke
'Set the GridLineStyle Property :)
grdResults.GridLineStyle = DataGridLineStyle.None
'Set the SelectionBackColor and the Selection ForeColor Properties
grdResults.SelectionBackColor = Color.LightGray
grdResults.SelectionForeColor = Color.Black
Catch OleDbExceptionErr As OleDbException
Debug.WriteLine(OleDbExceptionErr.Message)
Catch InvalidOperationExceptionErr As InvalidOperationException
Debug.WriteLine(InvalidOperationExceptionErr.Messa ge)
End Try
'Cleaning up the Mess
insertCmd.Dispose()
insertCmd = Nothing
Adapter.Dispose()
Adapter = Nothing
DataSet.Dispose()
DataSet = Nothing
myConnection.Dispose()
myConnection = Nothing
///////////////////////////////
Keep in mind guys the code works well so there is no coding trouble to troubleshoot here....more like solution problem lol
Again objective is to have another button rebind the grid after user is done updating,deleting,inserting etc.
Im a noob at this whole binding stuff and would like to learn the best way to do this stuff.
Thankyou in advance as always :)
|