0

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.

4
Contributors
14
Replies
59
Views
7 Years
Discussion Span
Last Post by anashidayat
0

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.

Edited by finito: n/a

0

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.

0

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.

Edited by Jollyyy100: n/a

0

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")

Edited by Jollyyy100: n/a

0

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()

0

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)
0

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?

0

intnavigate = 0

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

Edited by Jollyyy100: n/a

0

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

Edited by finito: n/a

0

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.

0

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

It would say variable not initialized.

Edited by finito: n/a

0
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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.