I am trying to insert a new row of data into an Access database. Here is what I am using:
Dim fNm As String
Dim lNm As String
Dim rowsAffected As Integer
fNm = Me.tbFirstNm.Text()
lNm = Me.tblastNm.Text()
Dim cnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\Auto_Mate.mdb;"
Dim cn As New OleDbConnection(cnString)
'try to open connection
Try
cn.Open()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Dim sql As String = "Insert into DRIVER (DRIVER_FIRST_NM, DRIVER_LAST_NM)"
sql = sql & "VALUES (" & fNm & ", " & lNm & ")"
Dim cmd As New OleDbCommand(sql, cn)
rowsAffected = cmd.ExecuteNonQuery()
Debug.Write(fNm & lNm)
End Sub
Everything works until I get to 'rowsAffected = cmd.ExecuteNonQuery()' I get the following error:
An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in system.data.dll
I do not want to have to use a dataset to insert, but I will if that is the only way. Am I on the right track or should I be using something like SQLCommand?
Dim fNm As String
Dim lNm As String
Dim rowsAffected As Integer
fNm = Me.tbFirstNm.Text()
lNm = Me.tblastNm.Text()
Dim cnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\Auto_Mate.mdb;"
Dim cn As New OleDbConnection(cnString)
'try to open connection
Try
cn.Open()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Dim sql As String = "Insert into DRIVER (DRIVER_FIRST_NM, DRIVER_LAST_NM)"
sql = sql & "VALUES (" & fNm & ", " & lNm & ")"
Dim cmd As New OleDbCommand(sql, cn)
rowsAffected = cmd.ExecuteNonQuery()
Debug.Write(fNm & lNm)
End Sub
Everything works until I get to 'rowsAffected = cmd.ExecuteNonQuery()' I get the following error:
An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in system.data.dll
I do not want to have to use a dataset to insert, but I will if that is the only way. Am I on the right track or should I be using something like SQLCommand?