I am having a big program trying to delete a record from my SQL Servers Database. I originally created this code to work with Microsoft Access, however I have decided to convert it to use MS SQL 2008.
Now this is what I have:
Dim dbConnection As SqlConnection
Dim dbCommand As SqlCommand
Dim strSQL As String
Dim dgvSelectRow As Integer 'Represents the selected row in the DataGridView
Public Sub SQLConnect()
dbConnection = New SqlConnection("Data Source=w7gbnewmfg2143\sqlexpress;Initial Catalog=master_plan;Integrated Security=True")
dbConnection.Open()
End Sub
Public Sub SQLCommand()
dbCommand = New SqlCommand(strSQL, dbConnection)
End Sub
Public Sub SQLDisconnect()
dbConnection.Close()
End Sub
Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
If dgvLine1.RowCount = 0 Then
MsgBox("No Rows to Delete")
Else
dgvSelectRow = dgvLine1.CurrentRow.Index
If MsgBox("Are you sure to remove this job?", MsgBoxStyle.Critical + MsgBoxStyle.YesNo, "Remove Job") = MsgBoxResult.Yes Then
SQLConnect()
strSQL = "DELETE FROM [LinePlan] WHERE DateTime = '" & dgvLine1.Item(8, dgvSelectRow).Value & "'" 'Removes selected row from DataGridView at position column 8
SQLCommand()
dbCommand.ExecuteNonQuery()
SQLDisconnect()
tmrUpdate.Enabled = True
tmrUpdate.Start()
Else
End If
End If
End Sub
Basically I am trying to delete a record from a selected row in a DataGridView. In the row it deletes the DateTime value at column at in my DGV. Now when I created this query in MS Access I had no problems at all, it worked perfectly. Now that I have migrated it to MS SQL, I am having all sorts of problems. I was getting alot of syntax errors which I have now resolved (i think). I have been using Catch ex As Exception to try and determine any errors. I have now cleared all errors. Now when I click the remove button, I receive no errors, however the record does not delete and it still remains in the database. The program does not crash or display any errors, and seems like it has done what it needs to do, but it does not delete the record. I have tried using different methods of connection strings but to no avail. I have tried it by not using the variable and just putting in the data manually and still no luck.
Am I missing something straight forward? Because I cannot figure out what the issue could be. It seems a very straight forward query and didnt think much could really go wrong, but how wrong am I :).
Any help or guidence would be greatly appreciated. Thanks for your time!
Regards, James