Hello. I'm new to vb.net. Please bear with me. I would like to learn how to insert, update, delete data in a Datagridview. So far I learned the best approach to this is to bind the DatagridView to a Datatable? The requirement is to use stored procedures. I'm not allowed to directly access database tables.
My public variables:
Code:
Public intDisbursementID As Long
Dim CS As String = ConfigurationManager.ConnectionStrings("SimpleAccounting.My.MySettings.SimpleAcctgConnectionString").ConnectionString
Dim cb As SqlCommandBuilder = Nothing
Dim da As SqlDataAdapter = Nothing
Dim ds As DataSet = Nothing
Dim dv As DataView
Dim dt As DataTable
Dim bs As BindingSource
Dim isDataLoaded As Boolean
my code below for From Load:
Code:
Private Sub LoadDetailsData(ByVal mDisbursementID As Long)
Using con As SqlConnection = New SqlConnection(CS)
Try
da = New SqlDataAdapter("sp_NET_tblDisbursementDetails_CompanyID_DisbursementID", CS)
da.SelectCommand.CommandType = CommandType.StoredProcedure
da.SelectCommand.Parameters.AddWithValue("@CompanyID", CInt(ConfigurationManager.AppSettings("CompanyID")))
da.SelectCommand.Parameters.AddWithValue("@DisbursementID", CLng(mDisbursementID))
cb = New SqlCommandBuilder(da)
''======== I have no idea how to make this work ===============
'da.InsertCommand = SqlCommand.GetInsertCommand()
'da.UpdateCommand = SqlCommand.GetUpdateCommand()
'da.DeleteCommand = SqlCommand.GetDeleteCommand()
'==============================================================
dt = New DataTable
bs = New BindingSource
da.Fill(dt)
bs.DataSource = dt
dgvDisbursementDetails.DataSource = bs
'dgvDisbursementDetails.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Using
End Sub
My Button Save Code:
Question: I can't figure out a way how to make the sqlcommandbuilder to work. Is there a way for me to override the sqlcommandbuilder and use my existing insert, update, delete storedprocedures ?