I want to delete an item from list view and at the same I want delete the item from the Releavant table
when I click the okay button in the ** search form** i want to delete the item from listview and as well as to update it to Sell table

  • listview is in the** form7**

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim Productid As String = ComboBox2.Text

        myCommand = New SqlCommand(" DELETE FROM [Sells] WHERE P_Id = ' " & ComboBox2.Text & "' AND Bill_No='" & Val(TextBox1.Text) & "'", myConnection)
        myCommand.ExecuteNonQuery()
        MsgBox("Update successfully")
    
    
        listview()
    
        Me.Hide()
    End Sub
    
    Public Sub listview()
    
        Try
            Try
    
    
    
                If Form7.ListView1.Items.Count = 0 Then
                    MsgBox("No Data to Delete", MsgBoxStyle.Information)
                    Exit Sub
                End If
                Dim intindex As Integer
                For Each lstdata As ListViewItem In Form7.ListView1.SelectedItems
                    intindex = lstdata.Index
    
    
    
                Next
                Form7.ListView1.Items.RemoveAt(intindex)
    
                ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
            Catch err As Exception
                MsgBox(err.Message, MsgBoxStyle.Exclamation)
            End Try
    
        Catch err As Exception
            MsgBox(err.Message, MsgBoxStyle.Exclamation)
        End Try
        '''''''
    
        myConnection.Close()
    End Sub
    

in here the data won't neither delete from listview nor from table though i get the message No Data to Delete and Update successfully
this the coding in the remove item in the form7

        Dim intResponse As Integer

        intResponse = MsgBox("Please Select the Product Id", MsgBoxStyle.OkCancel)

        If intResponse = vbOK Then
            varibles.billno = Val(TextBox14.Text)
            Search.Show()

            ' code to end the app, such as "Me.Close" would go here

        End If

    End Sub

daliy sales -form7
search from- form

What I like to do when using listviews, is to load the table's identity column into the first column of the listview. (Then make the column width 0 to hide it from the user)

Then to reference it, I use:

Dim myUnique As String 'Your data type here

If IsNothing(ListView1.SelectedItems) = False Then
    For Each Itm as ListViewItem in ListView1.SelectedItems
        myUnique = Itm.Text
    Next
Else
    MsgBox("Please select an item first!")
End If

You have just referenced the identity that is stored in the first (hidden or not) column of the row.

You can now use this to manipulate data/the list view.

Delete command is only not working..............

You are referencing the index of the listview only.

This will ONLY work on the very first delete (Assuming you have an identity column that matches the exact indexes of the listview(Extremely unlikely))

You will need to reference a unique value from the list view item, otherwise, you can only remove it form the listview - not the database.

For example:

(HideThisColumn|Col1|Col2|Col3)
(             3|Name|Date|Info)
(             5|Name|Date|Info)
(             6|Name|Date|Info)
(             9|Name|Date|Info)

If you stored the database indentity column in the first column of the list view, but hid the column, the user will only see col1, col2 and col3.
So when the user clicks the column " 5|Name|Date|Info " You know that that corresponds to the database entry with an identity of 5.

By using the index of the item ( What you are doing now ) you will try to execute a delete statement on the index of 1 (2nd item in the list) which will produce undesired outcomes.

I'm deleting the item from the compsite primary key which is for product id and Bill no in here i have n't hide any columns...................

If you are using a dataset to fill your listview, and the records are added by dataset row, you can do this.

 MyDataSet.Tables("myTable").Rows(indexFromListView).Delete()
 MyDataAdapter.Update(MyDataSet.Tables("myTable"))

thank you

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.