Results 1 to 2 of 2

Thread: [2005] Deleting a selected item from a ListView and Access database

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2006
    Posts
    58

    [2005] Deleting a selected item from a ListView and Access database

    here r my codes
    wat shud i do if i want the selected item in the listview to be deleted in both the listview and the database?? can someone help me with the codeS???



    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim strcon As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source =C:\temp\db1.mdb"
    Dim con As OleDb.OleDbConnection
    ' Dim dr As OleDb.OleDbDataReader
    Dim cmd As New OleDb.OleDbCommand
    Try

    Dim strselect As String = "Delete from item where DateDeleted ='" & Format(DateTimePicker1.Text) & "'"

    'create a new connection
    con = New OleDb.OleDbConnection(strcon)
    con.Open()
    cmd.Connection = con
    cmd.CommandText = strselect
    cmd.ExecuteNonQuery()
    con.Close()

    'If dr.Read Then
    'TextBox2.Text = dr("ItemId")
    'TextBox1.Text = dr("ReferenceNo")
    'cbxtype.Text = dr("ItemCategory")
    'DateTimePicker1.Text = dr("DateDeleted")

    'End If
    'dr.Close()

    Catch ex As Exception
    MessageBox.Show("The record is deleted")
    End Try

    ' Dim alvw As ListView.SelectedIndexCollection = lvfine.SelectedIndices

    ' For Each lvw As Integer In alvw
    'lvfine.Items.RemoveAt(lvw) ' Remove them from the list view.
    ' Next

    For i As Integer = Me.lvfine.SelectedItems.Count - 1 To 0 Step -1
    Me.lvfine.Items.Remove(Me.lvfine.SelectedItems(i))
    Next i

    For i As Integer = Me.lvfine.SelectedIndices.Count - 1 To 0 Step -1
    Me.lvfine.Items.RemoveAt(Me.lvfine.SelectedIndices(i))
    Next i

    While Me.lvfine.SelectedItems.Count > 0
    Me.lvfine.Items.Remove(Me.lvfine.SelectedItems(0))
    End While

    ' Try
    'Dim strselect As String = "Insert into item(DateDeleted) values('" & Format(DateTimePicker1.Value) & "')"

    'create a new connection
    'con = New OleDb.OleDbConnection(strcon)
    'con.Open()
    'cmd.Connection = con
    'cmd.CommandText = strselect
    'dr = cmd.ExecuteReader

    'If dr.Read Then
    'TextBox2.Text = dr("ItemId")
    'TextBox1.Text = dr("ReferenceNo")
    'cbxtype.Text = dr("ItemCategory")
    ' DateTimePicker1.Text = dr("DateDeleted")

    'End If
    'dr.Close()

    'Catch ex As Exception
    ' MessageBox.Show("The record is deleted")
    ' End Try

    End Sub

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Deleting a selected item from a ListView and Access database

    This is your third thread asking the same question. Start one thread for each topic and if you need to add or ask for more information then add a post. Don't keep creating thread after thread for the same topic.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width