By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,910 Members | 1,077 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,910 IT Pros & Developers. It's quick & easy.

Help Deleting from DataBase

P: 10
Can anyone explain why this code doesnt delete from the database. Some times it works and some times it errors i am confused.

Expand|Select|Wrap|Line Numbers
  1. Private Sub DeleteBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteBtn.Click
  2.  
  3.     Dim connStr As String = "Provider = Microsoft.Jet.OLEDB.4.0;" & _
  4.     "Data Source = G2G.mdb"
  5.     Dim oDataTable As New DataTable
  6.     Dim squlStr As String = "SELECT * From Staff"
  7.     Dim oleDbStaffAdapter As New OleDb.OleDbDataAdapter(squlStr, connStr)
  8.  
  9.         oleDbStaffAdapter.Fill(oDataTable)
  10.         Dim StaffDeleteRow As DataRow
  11.  
  12.         oDataTable.Rows.Item(Val(StaffnoTxt.Text)).Delete()
  13.  
  14.         Dim changes As Integer
  15.         Dim commandbuilder As New OleDb.OleDbCommandBuilder(oleDbStaffAdapter)
  16.  
  17.         changes = oleDbStaffAdapter.Update(oDataTable)
  18.  
  19.  
  20.     End Sub
Thanks
Dec 7 '07 #1
Share this Question
Share on Google+
3 Replies


P: 10
Hi i am getting An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in system.data.dll. Some times my code works and some times it doesn't. Its realy wierd. It is erroring on changes = oleDbStaffAdapter.Update(aDataTable) can anyone please help me.

Expand|Select|Wrap|Line Numbers
  1. Private Sub AddBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddBtn.Click
  2.  
  3.     Dim connStr As String = "Provider = Microsoft.Jet.OLEDB.4.0;" & _
  4.     "Data Source = G2G.mdb"
  5.     Dim aDataTable As New DataTable
  6.     Dim squlStr As String = "SELECT * From Staff"
  7.     Dim oleDbStaffAdapter As New OleDb.OleDbDataAdapter(squlStr, connStr)
  8.  
  9.  
  10.       Try
  11.             aDataTable.Clear()
  12.             StaffnoTxt.DataBindings.Clear()
  13.             FnameTxt.DataBindings.Clear()
  14.             LastnameTxt.DataBindings.Clear()
  15.             NoTxt.DataBindings.Clear()
  16.             PcodeTxt.DataBindings.Clear()
  17.             DoornoTxt.DataBindings.Clear()
  18.  
  19.             oleDbStaffAdapter.Fill(aDataTable)
  20.             Dim MyNewRow As DataRow = aDataTable.NewRow
  21.  
  22.             MyNewRow("Staff_No") = StaffnoTxt.Text
  23.             MyNewRow("First_Name") = FnameTxt.Text
  24.             MyNewRow("Surname") = LastnameTxt.Text
  25.             MyNewRow("Tel_No") = NoTxt.Text
  26.             MyNewRow("Post_Code") = PcodeTxt.Text
  27.             MyNewRow("House") = DoornoTxt.Text
  28.  
  29.             aDataTable.Rows.Add(MyNewRow)
  30.  
  31.  
  32.             Dim changes As Integer
  33.             Dim commandbuilder As New OleDb.OleDbCommandBuilder(oleDbStaffAdapter)
  34.             changes = oleDbStaffAdapter.Update(aDataTable)
  35.  
  36.             AddLbl.Text = "New Staff Added"
  37.  
  38.  
  39.         Catch ex As InvalidOperationException
  40.             MsgBox(ex.Message)
  41.         End Try
  42. End Sub
Thanks
Jonathan
Dec 7 '07 #2

kenobewan
Expert 2.5K+
P: 4,871
I thought it would have been commandbuilder.Update(aDataTable), but its late and the screen has become blurred :).
Dec 8 '07 #3

kenobewan
Expert 2.5K+
P: 4,871
I note the same code is coming up multiple question and that you are basically multiposting under the guise of different questions. I also find it difficult to believe that this is a university course, although some unis will offer anything if they can find a student.

Your questions appears to be a Homework Assignment and you should make a proper attempt at assignment/program. This question will now be closed and you will have to post a new question when you're ready. I strongly recommend reading the posting guidelines.

MODERATOR
Dec 8 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.