|
Question : Getting a First Chance Exception when trying to add a record to access with VB.NET
|
|
Hello all! I am getting the following error in my Immediate window:
A first chance exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll A first chance exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll A first chance exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll A first chance exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll A first chance exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll A first chance exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll
That's not repeated - I get the error 6 times every time i run the program.
My Code is as follows:
Public Function AddToDB(ByRef Table As String, ByRef DBColumnName As String, ByVal ValueToWrite As String) As String
Try
Dim con As New OleDb.OleDbConnection Dim ds As New DataSet Dim da As OleDb.OleDbDataAdapter Dim sql As String Dim dsNewRow As DataRow Dim ex As OleDb.OleDbException
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=C:\saw.mdb;Jet OLEDB:Database Password=hello;"
sql = "SELECT * FROM " & Table
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, Table)
Dim cb As New OleDb.OleDbCommandBuilder(da)
dsNewRow = ds.Tables(Table).NewRow()
dsNewRow.Item("Date_Time") = DateTime.Now() dsNewRow.Item(DBColumnName) = ValueToWrite
ds.Tables(Table).Rows.Add(dsNewRow)
da.Update(ds, Table)
con.Close()
con = Nothing da = Nothing ds = Nothing dsNewRow = Nothing sql = Nothing da = Nothing
AddToDB = ValueToWrite & ": Written to DB"
Catch ex As Exception
'Error Handeling AddToDB = "Database Write Error" & ex.ToString()
End Try
The correct data is entered in the correct table every time, I just don't know why this error is being thrown. I have been searching for while on how to resolve this with no luck. Thanks!
|
|
Answer : Getting a First Chance Exception when trying to add a record to access with VB.NET
|
|
You're DataAdapter has no UpdateCommand, hence nothing to do when you call da.Update().
|
|
|
|