1 Private Sub ModifySelectedCustomerRecord(ByVal dbProvider As DbProviderFactory, ByVal customerID As String, ByVal modifiedCompanyName As String, ByVal modifiedContactName As String, ByVal connectionString As String)
2
3 Dim connection As DbConnection = dbProvider.CreateConnection
4 connection.ConnectionString = connectionString
5
6 Dim modifyCustomersSQL As String = "UPDATE Customers SET CompanyName=@CompanyName, ContactName=@ContactName WHERE CustomerID=@CustomerID"
7
8 'Retrieve a command object from the dbProvider
9 Dim command As DbCommand = dbProvider.CreateCommand
10
11 'Setup the command object to execute the modifyCustomersSQL statement
12 With command
13 .CommandText = modifyCustomersSQL
14 .CommandType = CommandType.Text
15 .Connection = connection
16 End With
17
18 'Create a parameter object from the dbProvider
19 Dim companyNameParameter As DbParameter = dbProvider.CreateParameter
20 companyNameParameter.ParameterName = "CompanyName"
21 companyNameParameter.Value = modifiedCompanyName
22
23 Dim contactNameParameter As DbParameter = dbProvider.CreateParameter
24 contactNameParameter.ParameterName = "ContactName"
25 contactNameParameter.Value = modifiedContactName
26
27 Dim customerIDParameter As DbParameter = dbProvider.CreateParameter
28 customerIDParameter.ParameterName = "CustomerID"
29 customerIDParameter.Value = customerID
30
31
32 'Add the parameters to the Commands parameters collection in the same order as they are defined
33 'in the SQL statement. This will resolve the MS Access issue whereby named parameters are not
34 'supported. It also ensures that the correct fields are updated with the correct values.
35 command.Parameters.Add(companyNameParameter)
36 command.Parameters.Add(contactNameParameter)
37 command.Parameters.Add(customerIDParameter)
38
39 'Execute the command to modify the data
40 connection.Open()
41 command.ExecuteNonQuery()
42 connection.Close()
43
44 connection.Dispose()
45 command.Dispose()
46
47 'Reload the Grid to show the changes
48 LoadCustomersIntoGrid(dbProvider, connectionString)
49
50 End Sub