Call SetCommands() when you bind the Datagrid.
Call daUpdate whenever you want to update the Datagrid.
All variables prefixed with m_ are class members.
Friend Sub daUpdate(ByVal ds As DataSet, ByVal sTable As String)
Dim tran As SqlTransaction
m_da.ContinueUpdateOnError = True update whatever we can
Try
If m_con.State = ConnectionState.Closed Then m_con.Open()
tran = m_con.BeginTransaction(IsolationLevel.ReadCommitted)
m_da.UpdateCommand.Transaction = tran
m_da.InsertCommand.Transaction = tran
m_da.DeleteCommand.Transaction = tran
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
Try
m_da.Update(ds.Tables(sTable))
tran.Commit()
Catch ex As SqlException
Try
MessageBox.Show(ex.ToString)
tran.Rollback()
Catch RollbackEx As SqlException
MessageBox.Show(RollbackEx.ToString)
End Try
Finally
If m_con.State = ConnectionState.Open Then m_con.Close()
End Try
End Sub
Friend Sub SetCommands()
This routine Sets the Commands for the Data Adapter
Try
Dim cmdInsert As New SqlCommand()
Dim cmdUpdate As New SqlCommand()
Dim cmdDelete As New SqlCommand()
With cmdInsert
.CommandText = "INSERT INTO CLients(FirstName, LastName, Tel, SinClient)" _
& " VALUES (@FirstName, @LastName, @Tel, @SinClient); " _
& "SELECT FirstName, LastName, Tel, SinClient FROM Clients" _
& " WHERE (SinClient = @SinClient)"
.Connection = m_con
.Parameters.Add(New SqlParameter("@FirstName", SqlDbType.NVarChar, 15, "FirstName"))
.Parameters.Add(New SqlParameter("@LastName", SqlDbType.NVarChar, 20, "LastName"))
.Parameters.Add(New SqlParameter("@Tel", SqlDbType.NVarChar, 12, "Tel"))
.Parameters.Add(New SqlParameter("@SinClient", SqlDbType.NVarChar, 9, "SinClient"))
End With
With cmdUpdate
.CommandText = "UPDATE Clients SET " & _
"SinClient = @SinClient, " & _
"FirstName = @FirstName, " & _
"LastName = @LastName, " & _
"Tel = @Tel " & _
"WHERE " & _
"(SinClient = @Original_SinClient) " & _
"AND (FirstName = @Original_FirstName) " & _
"AND (Tel = @Original_Tel) " & _
"AND (LastName = @Original_LastName " & _
"OR @Original_LastName IS NULL AND LastName IS NULL); " & _
"SELECT SinClient, FirstName, LastName, Tel " & _
"FROM Clients " & _
"WHERE (SinClient = @SinClient)"
.Connection = m_con
.Parameters.Add(New SqlParameter("@SinClient", SqlDbType.NVarChar, 9, "SinClient"))
.Parameters.Add(New SqlParameter("@FirstName", SqlDbType.NVarChar, 15, "FirstName"))
.Parameters.Add(New SqlParameter("@LastName", SqlDbType.NVarChar, 20, "LastName"))
.Parameters.Add(New SqlParameter("@Tel", SqlDbType.NVarChar, 12, "Tel"))
.Parameters.Add(New SqlParameter("@Original_SinClient", SqlDbType.NVarChar, 9, _
System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "SinClient", _
System.Data.DataRowVersion.Original, Nothing))
.Parameters.Add(New SqlParameter("@Original_FirstName", SqlDbType.NVarChar, 15, _
System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "FirstName", _
System.Data.DataRowVersion.Original, Nothing))
.Parameters.Add(New SqlParameter("@Original_LastName", SqlDbType.NVarChar, 20, _
System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "LastName", _
System.Data.DataRowVersion.Original, Nothing))
.Parameters.Add(New SqlParameter("@Original_Tel", SqlDbType.NVarChar, 12, _
System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "Tel", _
System.Data.DataRowVersion.Original, Nothing))
End With
With cmdDelete
.CommandText = "DELETE " & _
"FROM CLients " & _
"WHERE ( " & _
"SinClient = @Original_SinClient) " & _
"AND (FirstName = @Original_FirstName) " & _
"AND (Tel = @Original_Tel) " & _
"AND (LastName = @Original_LastName " & _
"OR @Original_LastName IS NULL AND LastName IS NULL)"
.Connection = m_con
.Parameters.Add(New SqlParameter("@Original_SinClient", System.Data.SqlDbType.NVarChar, 9, _
System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "SinClient", _
System.Data.DataRowVersion.Original, Nothing))
.Parameters.Add(New SqlParameter("@Original_FirstName", System.Data.SqlDbType.NVarChar, 15, _
System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "FirstName", _
System.Data.DataRowVersion.Original, Nothing))
.Parameters.Add(New SqlParameter("@Original_LastName", System.Data.SqlDbType.NVarChar, 20, _
System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "LastName", _
System.Data.DataRowVersion.Original, Nothing))
.Parameters.Add(New SqlParameter("@Original_Tel", System.Data.SqlDbType.NVarChar, 12, _
System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "Tel", _
System.Data.DataRowVersion.Original, Nothing))
End With
With m_da
.InsertCommand = cmdInsert
.UpdateCommand = cmdUpdate
.DeleteCommand = cmdDelete
End With
Catch
End Try
End Sub