Hi all,
I was having a huge problem,i mean i spent days trying to fix it but i get the same exception everytime when i run the app.
here's the thing i have a record that is loaded from the database using the listview.it works perfect for the insertion part.but when i try to delete a certain row it gives me the previous exception.
so please if one of you could give me a precious solution
thankyou all

Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Dim str As String
        Dim conn As OleDbConnection = GetDbConnection()
        Dim cmd As OleDbCommand
        Dim selectedItem As Integer = ListView1.SelectedIndices(0)
        str = "DELETE FROM AdigratT WHERE Bridge_Number = '" & ListView1.Items(selectedItem).SubItems(0).Text & "'"
        cmd = New OleDbCommand(str, conn)
        cmd.ExecuteNonQuery()
        MsgBox("Record Removed Successfully")
        conn.Close()
    End Sub

Try :
str = "DELETE FROM AdigratT WHERE Bridge_Number = '" & ListView1.Items(x).SubItems(0).Text & "'"

It still does not work Jx.it's like replacing the declared variable selectedItem with x right?

when i try to delete a certain row it gives me the previous exception

What previous exception?

Is Bridge_Number a Text field? If not, get rid of the single qotes around it in the query. What column index does Bridge_Number represent in the listview? Your query indicates that it is the first column.

What exception is being thrown?

You have never stated it.

It still does not work Jx.it's like replacing the declared variable selectedItem with x right?

Ah..I didn't realized it but the code works fine to me.
What errors is came up?

Also you can try change his :
Dim selectedItem As Integer = ListView1.SelectedItems(0).Index
or
Dim selectedItem As Integer = ListView1.SelectedIndices.Item(0)

'No Value given for one or more required parameters' exception is being thrown.
and also i've tried it with the following code,still doesn't work.

Dim bridgenumber As String
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Dim conn As OleDbConnection = GetDbConnection()
        Dim cmd As OleDbCommand
        bridgenumber = TextBox1.Text.ToString()
        cmd = New OleDbCommand("DELETE FROM AdigratT WHERE Bridge Number = '" & bridgenumber & "'", conn)
        cmd.ExecuteNonQuery()
        MsgBox("Record Removed Successfully")
        conn.Close()
    End Sub

Maybe if it's possible ii can give you the source code and have a look at it

I suspect the "Bridge Number" part of your query.

You may have to wrap your field name with [ brakets.

For example:

cmd = New OleDBCommand("DELETE FROM Adigrat WHERE [Bridge Number]=@BridgeNumber",conn)
cmd.Parameters.AddWithValue("@BridgeNumber",bridgenumber)

cmd.ExecuteNonQuery()

Hey Begginnerdev,it definitely worked.I really really appreciate your work,you got a magic fingers

commented: Congrats! +8

No problem friend!

Please do not forget to mark the question as solved.

Hi again Begginerde,
Can i use the same code for the update scenario,i had the same problem with the update code
i've posted the code on the beginning of the discussion

An update statement would look something like this:

cmd = New OleDBCommand("UPDATE Adigrat SET [Bridge Number]=@Value",con)
cmd.Parameters.AddWithValue("@Value",ValueToUpdate)

cmd.ExecuteNonQuery()
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.