|
-
Nov 14th, 2002, 08:14 AM
#1
Thread Starter
Member
Delete a record from database
Hello everyone,
I'm trying to delete a record from database. I delete within the code and display the results on a grid.
The problem is the record is deleted from the dataset, and I can see it in the grid and in the number of records of the dataset, but not from the database.
Note I get no exception.
If anybody could help me I would very appriciate it.
thanks,
Shuly.
Here is the code:
Dim objConn As OleDb.OleDbConnection
Dim objDS As DataSet
Dim objDeleteCmd As New OleDb.OleDbCommand()
Dim sDeleteSQL As String
Dim objParam As OleDb.OleDbParameter
Dim objDataAdapter As OleDb.OleDbDataAdapter
Private Sub Form1_Load (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Const sConnection As String = "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=E:\VbNet_Projects\Add\Grid\DatabaseTry\LocalJournal.mdb;" _
& "Persist Security Info=False"
Dim sSQL As String
sSQL = "SELECT EventId From Journal"
objConn = New OleDb.OleDbConnection(sConnection)
objDataAdapter = New OleDb.OleDbDataAdapter(sSQL, objConn)
objDS = New DataSet()
Dim objDV As DataView
Try
objConn.Open()
Catch myException As System.Exception
Windows.Forms.MessageBox.Show(myException.Message)
End Try
If objConn.State = ConnectionState.Open Then
Try
objDataAdapter.Fill(objDS, "Journal")
objConn.Close()
Catch myexception As Exception
Windows.Forms.MessageBox.Show(myException.Message)
End Try
End If
DataGrid1.SetDataBinding(objDS, "Journal")
TextBox1.Text = CStr(objDS.Tables("Journal").Rows.Count).ToString
sDeleteSQL = "DELETE FROM Journal WHERE EventId = ?"
objDeleteCmd.Connection = objConn
objDeleteCmd.CommandText = sDeleteSQL
objParam = objDeleteCmd.Parameters.Add("@EventId", OleDb.OleDbType.BigInt)
objParam.SourceColumn = "EventId"
objParam.SourceVersion = DataRowVersion.Current
objDataAdapter.DeleteCommand = objDeleteCmd
End Sub
Private Sub btnDelete_Click _Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
Dim minEvent As Long
Try
minEvent = 1
Dim cRow() As DataRow = objDS.Tables("Journal").Select("[EventId] = " & minEvent)
objDS.Tables("Journal").Rows.Remove(cRow(0))
objDataAdapter.Update(objDS, "Journal")
TextBox1.Text = CStr(objDS.Tables("Journal").Rows.Count).ToString
Catch ex As Exception
Windows.Forms.MessageBox.Show(ex.Message)
End Try
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|