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

Delete row from database using OleDb

maylortaylor
P: 72
I have this function working half right. The part that is working right is where I can select a row on the DataGridView, call this function using a "Delete Row" button, and then it will delete the row from the DataGridView....However, it does not delete the row on the database.

Can anyone help me with deleting the row from the DB using OleDb?

Expand|Select|Wrap|Line Numbers
  1. Function DeleteTableRow()
  2.         Dim TaxConnStr As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ConfigurationManager.AppSettings("Database")
  3.         Dim dbConnection = New OleDbConnection(TaxConnStr)
  4.  
  5.         Try
  6.             Dim dbCommand As OleDbCommand = New OleDbCommand
  7.             Dim rdr2 As OleDbDataReader
  8.  
  9.             Dim selectedRow = DataGridView1.SelectedRows
  10.  
  11.             dbCommand.CommandText = "DELETE FROM UserCriteria WHERE RowID =" & selectedRow
  12.             If dbConnection.State = ConnectionState.Closed Then
  13.                 dbConnection.Open()
  14.             End If
  15.  
  16.             dbCommand.Connection = dbConnection
  17.             rdr2 = dbCommand.ExecuteReader
  18.             dbCommand.ExecuteNonQuery()
  19.  
  20.  
  21.             rdr2.Close()
  22.  
  23.                        '''allows you to select on cell in the row to delete entire row
  24.             For Each oneCell As DataGridViewCell In DataGridView1.SelectedCells
  25.                 If oneCell.Selected Then
  26.                     DataGridView1.Rows.RemoveAt(oneCell.RowIndex)
  27.                 End If
  28.             Next
  29.  
  30.  
  31.  
  32.         Catch ex As Exception
  33.             MsgBox(ex.Message)
  34.         Finally
  35.             dbConnection.Close()
  36.         End Try
  37.     End Function
Mar 27 '13 #1
Share this Question
Share on Google+
4 Replies


Rabbit
Expert Mod 10K+
P: 12,383
Your database table has a field called RowID that is populated the same way as the DataGridView?
Mar 27 '13 #2

maylortaylor
P: 72
Well the row in the database is called "ID" and that particular row on the DataGridView is set to hidden.
Mar 27 '13 #3

Rabbit
Expert Mod 10K+
P: 12,383
The row? You mean the field in the table in the database? If it's named ID, then you need to use ID in your SQL, not RowID.
Mar 27 '13 #4

P: 1
check this datagridview tutorial

http://csharp.net-informations.com/d...w-tutorial.htm

pringston
May 10 '13 #5

Post your reply

Sign in to post your reply or Sign up for a free account.