How to delete a row (code included)

  • Thread starter Thread starter Markus
  • Start date Start date
M

Markus

My code at btnDelete doesn't work. Can you help?

And I'm new to all this programming stuff but 'm
learning :-)



Public Class frmSuradnici
Inherits System.Windows.Forms.Form
Dim objConnection As New OleDb.OleDbConnection( _
"Provider=Microsoft.Jet.OLEDB.4.0; Data
Source=C:\Surgery.mdb")
Dim objOwnerDA As New OleDb.OleDbDataAdapter("Select *
from Owners", _
objConnection)
Dim objOwnerCB As New OleDb.OleDbCommandBuilder
(objOwnerDA)
Dim objDataSet As New DataSet()

Windows Form Generated Code

Private Sub btnRetrieve_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btnRetrieve.Click
objDataSet.Clear()
objOwnerDA.FillSchema(objDataSet,
SchemaType.Source, "Owners")
objOwnerDA.Fill(objDataSet, "Owners")
cboOwners.Items.Clear()
Dim i As Integer, strCurrentID As String
For i = 1 To objDataSet.Tables("Owners").Rows.Count
strCurrentID = objDataSet.Tables("Owners").Rows
(i - 1).Item("OwnerID")
cboOwners.Items.Add(strCurrentID)
Next
cboOwners.SelectedIndex = 0
FillOwnerDetails()
End Sub

Public Sub FillOwnerDetails()
Dim objRow As DataRow
objRow = objDataSet.Tables("Owners").Rows.Find
(cboOwners.SelectedItem.ToString)
lblOwnerID.Text = objRow.Item("OwnerID")
txtName.Text = objRow.Item("Name")
txtAddress.Text = objRow.Item("Address")
End Sub

Public Sub StoreOwnerDetails()
Dim objRow As DataRow
If lblOwnerID.Text = "" Then Exit Sub

objRow = objDataSet.Tables("Owners").Rows.Find
(lblOwnerID.Text)
objRow.Item("Name") = txtName.Text
objRow.Item("Address") = txtAddress.Text
End Sub

Private Sub cboOwners_SelectedIndexChanged(ByVal
sender As Object, ByVal e As System.EventArgs) Handles
cboOwners.SelectedIndexChanged
StoreOwnerDetails()
FillOwnerDetails()
End Sub

Private Sub btnSave_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btnSave.Click
StoreOwnerDetails()
objOwnerDA.Update(objDataSet, "Owners")
End Sub

Private Sub frmSuradnici_Load(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
MyBase.Load
Dim toolTip1 As New ToolTip
toolTip1.AutoPopDelay = 5000
toolTip1.InitialDelay = 1000
toolTip1.ReshowDelay = 500
toolTip1.ShowAlways = True

toolTip1.SetToolTip(Me.cboOwners, "Izaberite
suradnički broj kako bi dobili njegove podatke.")
toolTip1.SetToolTip(Me.lblOwnerID, "Suradnički
broj")
toolTip1.SetToolTip(Me.txtName, "Pogledajte /
Upisite ime i prezime suradnika.")
toolTip1.SetToolTip(Me.txtAddress, "Pogledajte /
Upisite adresu suradnika.")
toolTip1.SetToolTip(Me.btnRetrieve, "Učitajte
podatke o suradnicima iz baze.")
toolTip1.SetToolTip(Me.btnSave, "Pohranite
promjene u bazu.")
toolTip1.SetToolTip(Me.Label1, "Programer:
Tomislav Ivančić.")
toolTip1.SetToolTip(Me, "FIDELITAS d.o.o.")


End Sub

Private Sub btnDelete_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btnDelete.Click
Dim objRow As DataRow
objRow = objDataSet.Tables("Owners").Rows.Find
("OwnerID")
objRow.Delete()
End Sub
End Class
 
Get a reference to the row you want to delete

Dim myrow as datarow= mytable.rows.item(1)
or use find function.

If not myrow is nothing then myrow.delete

Actually, the row is just mark as deleted but is still in DB.

To make delete permanent.

Mydataset.UPDATE ' you should already have a delete command ( with
commandbuilder)

mydataset.ACCEPTCHANGES
 
Hi,

you should use for loop in dataset.table to search
specific row to delete rather than use datarow.

the rowstate will be deleted,

Hope it will help you.

KWOK.
 
I need to delete the row that is curently loaded in my
form.

In this book I'm working on authoers says that I have to
use

objRow.Delete()

but I can't fuigure it out.
 
How do I get reference to the row that is currently loaded
in my form

objRow = objDataSet.Tables("Owners").Rows.Find(WHAT HERE
to find the row currently loaded in my form?)
 
---- PROBLEM -----
Private Sub btnDelete_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btnDelete.Click
Dim objRow As DataRow
objRow = objDataSet.Tables("Owners").Rows.Find
("OwnerID")
objRow.Delete()
End Sub


When I first wrote the code (above) I haven't updated the
database. I'm talking about database not dataset.

------- SOLUTION ------

Private Sub btnDelete_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btnDelete.Click
objRow = objDataSet.Tables("Owners").Rows.Find
(lblOwnerID.Text)
objRow.Delete()
objOwnerDA.Update(objDataSet, "Owners")
txtSearch.Text = "broj suradnika"
btnSave.Enabled = False
lblOwnerID.Text = ""
txtName.Text = ""
txtAddress.Text = ""
 
Back
Top