Hi, i'm using vb.net and i have connected a database to it. As per my interface i should add, delete,go back,go next and save my records. All the buttons work apart from delete.

The problem is i do not know the codes for deleting a particular record during the run time, can anyone help me out with the codes and steps of deleting a particular record.

Recommended Answers

All 14 Replies

Well, it is similar to UPDATE.

Just do this

DELETE FROM table WHERE condition

If you show me some of your work, I may better understand what you want.

Can you show me your select statement with propagation.

What part of my work should i show you. Sorry i'm yet a student so i do not know what you asking for, just let me know what part you want to see. Thank you so much for your help till know.

You can show your update statement or update button event...

Show me your whole code, or show me your Select, update, or Insert statement.

I get an error on this line "ds1.Tables("Employees").Rows(intnavigate).Delete()", the error says "NullReferenceException was unhandled"
Can you provide me with a solution.
Sorry about the format of pasting my codes, it might be wrong or right i do not know, but thats what i have written on my code page and it gives me that error.

If MessageBox.Show("Delete Record", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
        Else
        End If
        Dim cd1 As New OleDb.OleDbCommandBuilder(da1)
        ds1.Tables("Employees").Rows(intnavigate).Delete()
        intmaxrows = intmaxrows - 1
        intnavigate = 0
        DisplayRecords()
        EmployeeIDTextBox.Text = ds1.Tables("PPDatabase").Rows(intnavigate).Item(0)
        DepartmentNameTextBox.Text = ds1.Tables("PPDatabase").Rows(intnavigate).Item(1)
        EmployeeNumberTextBox.Text = ds1.Tables("PPDatabase").Rows(intnavigate).Item(2)
        FirstNameTextBox.Text = ds1.Tables("PPDatabase").Rows(intnavigate).Item(3)
        LastNameTextBox.Text = ds1.Tables("PPDatabase").Rows(intnavigate).Item(4)
        Address_BoxNumberTextBox.Text = ds1.Tables("PPDatabase").Rows(intnavigate).Item(5)
        Country_RegionTextBox.Text = ds1.Tables("PPDatabase").Rows(intnavigate).Item(6)
        HomePhoneTextBox.Text = ds1.Tables("PPDatabase").Rows(intnavigate).Item(7)
        DepartmentIDTextBox.Text = ds1.Tables("PPDatabase").Rows(intnavigate).Item(8)
        BirthdateDateTimePicker.Text = ds1.Tables("PPDatabase").Rows(intnavigate).Item(9)
        DateHiredDateTimePicker.Text = ds1.Tables("PPDatabase").Rows(intnavigate).Item(10)
        SalaryTextBox.Text = ds1.Tables("PPDatabase").Rows(intnavigate).Item(11)
        DeductionsTextBox.Text = ds1.Tables("PPDatabase").Rows(intnavigate).Item(12)
        NotesTextBox.Text = ds1.Tables("PPDatabase").Rows(intnavigate).Item(13)
        da1.Update(ds1, "PPDatabase")

Show me your whole code, or show me your Select, update, or Insert statement.

I get an error on this line "ds1.Tables("Employees").Rows(intnavigate).Delete()", the error says "NullReferenceException was unhandled"
Can you provide me with a solution.
Sorry about the format of pasting my codes, it might be wrong or right i do not know, but thats what i have written on my code page and it gives me that error.

If MessageBox.Show("Delete Record", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
        Else
        End If
        Dim cd1 As New OleDb.OleDbCommandBuilder(da1)
        ds1.Tables("Employees").Rows(intnavigate).Delete()
        intmaxrows = intmaxrows - 1
        intnavigate = 0
        DisplayRecords()
        EmployeeIDTextBox.Text = ds1.Tables("PPDatabase").Rows(intnavigate).Item(0)
        DepartmentNameTextBox.Text = ds1.Tables("PPDatabase").Rows(intnavigate).Item(1)
        EmployeeNumberTextBox.Text = ds1.Tables("PPDatabase").Rows(intnavigate).Item(2)
        FirstNameTextBox.Text = ds1.Tables("PPDatabase").Rows(intnavigate).Item(3)
        LastNameTextBox.Text = ds1.Tables("PPDatabase").Rows(intnavigate).Item(4)
        Address_BoxNumberTextBox.Text = ds1.Tables("PPDatabase").Rows(intnavigate).Item(5)
        Country_RegionTextBox.Text = ds1.Tables("PPDatabase").Rows(intnavigate).Item(6)
        HomePhoneTextBox.Text = ds1.Tables("PPDatabase").Rows(intnavigate).Item(7)
        DepartmentIDTextBox.Text = ds1.Tables("PPDatabase").Rows(intnavigate).Item(8)
        BirthdateDateTimePicker.Text = ds1.Tables("PPDatabase").Rows(intnavigate).Item(9)
        DateHiredDateTimePicker.Text = ds1.Tables("PPDatabase").Rows(intnavigate).Item(10)
        SalaryTextBox.Text = ds1.Tables("PPDatabase").Rows(intnavigate).Item(11)
        DeductionsTextBox.Text = ds1.Tables("PPDatabase").Rows(intnavigate).Item(12)
        NotesTextBox.Text = ds1.Tables("PPDatabase").Rows(intnavigate).Item(13)
        da1.Update(ds1, "PPDatabase")

Umm, can you explain your code.

What are you doing?

"NullReferenceException was unhandled" can mean you are deleting nothing as in The record you are pulling has Nulls.

where is your Select statement. Where you are pulling data from the Database.

Show me DisplayRecords()

My display records:

EmployeeIDTextBox.Text = ds1.Tables("PPDatabase").Rows(intnavigate).Item(0)
        DepartmentNameTextBox.Text = ds1.Tables("PPDatabase").Rows(intnavigate).Item(1)
        EmployeeNumberTextBox.Text = ds1.Tables("PPDatabase").Rows(intnavigate).Item(2)
        FirstNameTextBox.Text = ds1.Tables("PPDatabase").Rows(intnavigate).Item(3)
        LastNameTextBox.Text = ds1.Tables("PPDatabase").Rows(intnavigate).Item(4)
        Address_BoxNumberTextBox.Text = ds1.Tables("PPDatabase").Rows(intnavigate).Item(5)
        Country_RegionTextBox.Text = ds1.Tables("PPDatabase").Rows(intnavigate).Item(6)
        HomePhoneTextBox.Text = ds1.Tables("PPDatabase").Rows(intnavigate).Item(7)
        DepartmentIDTextBox.Text = ds1.Tables("PPDatabase").Rows(intnavigate).Item(8)
        BirthdateDateTimePicker.Text = ds1.Tables("PPDatabase").Rows(intnavigate).Item(9)
        DateHiredDateTimePicker.Text = ds1.Tables("PPDatabase").Rows(intnavigate).Item(10)
        SalaryTextBox.Text = ds1.Tables("PPDatabase").Rows(intnavigate).Item(11)
        DeductionsTextBox.Text = ds1.Tables("PPDatabase").Rows(intnavigate).Item(12)
        NotesTextBox.Text = ds1.Tables("PPDatabase").Rows(intnavigate).Item(13)

Ok the error you are getting is that the row intnavigate is Null.

what is intnavigate equal to? is there a row at that index?

intnavigate = 0

Is there any other code which can help me delete my records

Assuming you are using OleDb.

YourCon.Open()
Dim command As OleDbCommand = New OleDbCommand("DELETE FROM Employees WHERE condition", YourCon).ExecuteNonQuery()
YourCon.Close()

condition is for example EmployeeID = 123
so it would look like

YourCon.Open()
Dim command As OleDbCommand = New OleDbCommand("DELETE FROM Employees WHERE EmployeeID = 123", YourCon).ExecuteNonQuery()
YourCon.Close()

Follow this link for some more examples

You must initialize your variable intnavigate before you use it.
I get some codes of yours:

ds1.Tables("Employees").Rows(intnavigate).Delete()
intmaxrows = intmaxrows - 1
 intnavigate = 0

the red colored text must be declare first before you put this code:

ds1.Tables("Employees").Rows(intnavigate).Delete()

Please, correct me if you have other value of intnavigate.

leahrose87 if that was true the compiler wouldn't compile.

It would say variable not initialized.

if not myBindingSource.Current is Nothing then

If MsgBox("Are you sure to delete record", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then

                GridView1.ClearColumnsFilter()

                Dim fbtrans As FbTransaction = Fbconn.BeginTransaction(System.Data.IsolationLevel.ReadCommitted)
                Me.myAdapter.Adapter.DeleteCommand.Transaction = fbtrans
                Try
                    myBindingSource.RemoveCurrent()
                    Me.myAdapter.Adapter.Update(myTable)
                    fbtrans.Commit()
                   myTable.AcceptChanges()

                Catch ex As Exception
                    fbtrans.Rollback()
                    MsgBox(ex.Message, MsgBoxStyle.Critical)
                    MyTable.Rejectchanges()
                    MyBindingSource.CurrencyManager.Refresh()
                End Try
            End If
end if

It will delete your current record. If error, rollingback and the record shows again. Using firebird driver. You can replace fbtrans and fbcon according to your driver

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.