Save changes to database after loading .
-
My problem is that in the program that I am writing I cannot save the changes to database after I load the tables from it. Dim connstr As String = "provider=microsoft.jet.oledb.4.0;" & _ " DATA SOURCE = megas.mdb" Dim sqlstr As String = " select * from megas " Dim dtb As New DataTable ‘-------------------------------------------------------------- Private Sub btnload_Click(…) Handles btnload.Click dtb.Clear() Dim dataadapter As New OleDb.OleDbDataAdapter(sqlstr, connstr) dataadapter.Fill(dtb) dataadapter.Dispose() dgdisplay1.DataSource = dtb End Sub ‘----------------------------------------------------------------- Private Sub btnsave_Click (…) Handles btnsave.Click Dim changes As Integer Dim dataadapter As New OleDb.OleDbDataAdapter(sqlstr, connstr) Dim commandbuilder As New OleDb.OleDbCommandBuilder(dataadapter) changes = dataadapter.Update(dtb) dataadapter.Dispose() If changes > 0 Then MsgBox("SAVED ") Else MsgBox("not saved") End If End Sub I load the table form the database and nothing goes wrong with this part, but when I want to save the changes and I click the save btn , that is what I get “An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in system.data.dll “ What am I doing wrong? And why should I be getting this? Thank you. • Thanks for the previous help from Dave Kreskowiak and Naji Kotob +mackay, it really helped, especially that article. |-|■~F~|-|■
-
My problem is that in the program that I am writing I cannot save the changes to database after I load the tables from it. Dim connstr As String = "provider=microsoft.jet.oledb.4.0;" & _ " DATA SOURCE = megas.mdb" Dim sqlstr As String = " select * from megas " Dim dtb As New DataTable ‘-------------------------------------------------------------- Private Sub btnload_Click(…) Handles btnload.Click dtb.Clear() Dim dataadapter As New OleDb.OleDbDataAdapter(sqlstr, connstr) dataadapter.Fill(dtb) dataadapter.Dispose() dgdisplay1.DataSource = dtb End Sub ‘----------------------------------------------------------------- Private Sub btnsave_Click (…) Handles btnsave.Click Dim changes As Integer Dim dataadapter As New OleDb.OleDbDataAdapter(sqlstr, connstr) Dim commandbuilder As New OleDb.OleDbCommandBuilder(dataadapter) changes = dataadapter.Update(dtb) dataadapter.Dispose() If changes > 0 Then MsgBox("SAVED ") Else MsgBox("not saved") End If End Sub I load the table form the database and nothing goes wrong with this part, but when I want to save the changes and I click the save btn , that is what I get “An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in system.data.dll “ What am I doing wrong? And why should I be getting this? Thank you. • Thanks for the previous help from Dave Kreskowiak and Naji Kotob +mackay, it really helped, especially that article. |-|■~F~|-|■
The OleDb.OleDbException class is created whenever the .NET Framework Data Provider for OLE DB encounters an error generated from the server. (Client side errors are thrown as standard common language runtime exceptions.) OleDbException always contains at least one instance of OleDbError. -an explanation from msdn --the most common cause of this is because of a missing data source as what is written on your code: Dim connstr As String = "provider=microsoft.jet.oledb.4.0;" & _ " DATA SOURCE = megas.mdb" the "DATA SOURCE" should be replaced by "DataSource"(no spaces) and make sure that megas.mdb is properly installed on the odbc connection.
-
My problem is that in the program that I am writing I cannot save the changes to database after I load the tables from it. Dim connstr As String = "provider=microsoft.jet.oledb.4.0;" & _ " DATA SOURCE = megas.mdb" Dim sqlstr As String = " select * from megas " Dim dtb As New DataTable ‘-------------------------------------------------------------- Private Sub btnload_Click(…) Handles btnload.Click dtb.Clear() Dim dataadapter As New OleDb.OleDbDataAdapter(sqlstr, connstr) dataadapter.Fill(dtb) dataadapter.Dispose() dgdisplay1.DataSource = dtb End Sub ‘----------------------------------------------------------------- Private Sub btnsave_Click (…) Handles btnsave.Click Dim changes As Integer Dim dataadapter As New OleDb.OleDbDataAdapter(sqlstr, connstr) Dim commandbuilder As New OleDb.OleDbCommandBuilder(dataadapter) changes = dataadapter.Update(dtb) dataadapter.Dispose() If changes > 0 Then MsgBox("SAVED ") Else MsgBox("not saved") End If End Sub I load the table form the database and nothing goes wrong with this part, but when I want to save the changes and I click the save btn , that is what I get “An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in system.data.dll “ What am I doing wrong? And why should I be getting this? Thank you. • Thanks for the previous help from Dave Kreskowiak and Naji Kotob +mackay, it really helped, especially that article. |-|■~F~|-|■
Hi, general exceptions (I guess OleDbException qualifies) often contain an "internal exception". You wont see it if you just look at Exception.Message, but you would see it when looking at Exception.ToString() so I recommend everyone to always log or display Exception.ToString(); it is longer by the fact that it provides more information. I am not sure but expect this to apply to your situation too. :)
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }