VB.NET School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 300,451 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,576 people online right now. Registration is fast and FREE... Join Now!




VB.NET and ADO.NET - Deleting Rows

 

VB.NET and ADO.NET - Deleting Rows

matto

21 Jun, 2009 - 04:27 AM
Post #1

New D.I.C Head
*

Joined: 18 Apr, 2009
Posts: 40

Hi,

I have a button which loads a table from an MSACCESS file into a listview. Here is my code:

CODE
Private Function LoadDataBaseToListview()
        imgIcons.Images.Add("account", My.Resources.user) 'Add user icon to imagelist
        ' Load the data.
        ' Open the database.
        Dim conn As OleDbConnection = GetDbConnection()

        ' Select records.
        Dim cmd As New OleDbCommand("SELECT * FROM Accounts", conn) 'Select the Accounts table
        Dim data_reader As OleDbDataReader = cmd.ExecuteReader()
        Do While data_reader.Read()
            Dim account As New ListViewItem(data_reader.Item("UserID").ToString) 'Sets the username
            account.SubItems.Add(data_reader.Item("PassID").ToString) 'Sets the password
            lstAccounts.Items.Add(account)
            Me.lstAccounts.Items.Item(lstAccounts.Items.Count - 1).ImageKey = "account" 'Adds the image key "account" to the item "Username"
        Loop
        ' Close the connection.
        conn.Close()
    End Function


When the button is clicked, it runs this function, populating the table into the listview. Now, I would like to add the function where a user can delete rows by right-clicking a row in the listview and selecting delete. I have added a context-menu, and I can delete listview items, however I don't know how to delete the row in the MSACCESS database. Any ideas? Thanks

CODE
'To delete a listview entry
For i As Integer = lstAccounts.Items.Count - 1 To 0 Step -1
            If lstAccounts.Items(i).Selected Then lstAccounts.Items.RemoveAt(i)
        Next


User is offlineProfile CardPM
+Quote Post


AlienCoder

RE: VB.NET And ADO.NET - Deleting Rows

22 Jun, 2009 - 12:22 PM
Post #2

New D.I.C Head
*

Joined: 5 May, 2009
Posts: 31



Thanked: 3 times
My Contributions
CODE

If MessageBox.Show("Do you really want to Delete this Record?", _
              "Delete", MessageBoxButtons.YesNo, _
               MessageBoxIcon.Warning) = DialogResult.No Then

            MsgBox("Operation Cancelled")
            Exit Sub
Else

      Dim cb As New OleDb.OleDbCommandBuilder(MyDataAdapter)

      MyDataSet.Tables(Accounts).Rows('Specify the row to delete).Delete()
          
      MyDataAdapter.Update(MyDataSet)

End If



It was a quick reply didnt get tim to check it out properly and didnt use comments but it should be easy to digest smile.gif


User is offlineProfile CardPM
+Quote Post

matto

RE: VB.NET And ADO.NET - Deleting Rows

23 Jun, 2009 - 09:12 AM
Post #3

New D.I.C Head
*

Joined: 18 Apr, 2009
Posts: 40

I tried implementing your code. I changed the line

CODE
MyDataAdapter.Update(MyDataSet)


to


CODE
MyDataAdapter.Update(MyDataSet, MyTable)


But I got this error:

InvalidOperationException was unhandled
Dynamic SQL generation for the DeleteCommand is not supported against a SelectCommand that does not return any key column information.
User is offlineProfile CardPM
+Quote Post

AlienCoder

RE: VB.NET And ADO.NET - Deleting Rows

24 Jun, 2009 - 02:12 PM
Post #4

New D.I.C Head
*

Joined: 5 May, 2009
Posts: 31



Thanked: 3 times
My Contributions
QUOTE(matto @ 23 Jun, 2009 - 09:12 AM) *

I tried implementing your code. I changed the line

CODE
MyDataAdapter.Update(MyDataSet)


to


CODE
MyDataAdapter.Update(MyDataSet, MyTable)


But I got this error:

InvalidOperationException was unhandled
Dynamic SQL generation for the DeleteCommand is not supported against a SelectCommand that does not return any key column information.


Thanks for that correction. But replace MyTable with the proper name of your table (Accounts). And make sure it is the same name that you have used to fill your dataset and everywhere else like in your sql statement.

User is offlineProfile CardPM
+Quote Post

matto

RE: VB.NET And ADO.NET - Deleting Rows

25 Jun, 2009 - 12:52 PM
Post #5

New D.I.C Head
*

Joined: 18 Apr, 2009
Posts: 40

I did, I put "Accounts" in place of MyTable. Should I not use quotation marks?
User is offlineProfile CardPM
+Quote Post

AlienCoder

RE: VB.NET And ADO.NET - Deleting Rows

25 Jun, 2009 - 02:50 PM
Post #6

New D.I.C Head
*

Joined: 5 May, 2009
Posts: 31



Thanked: 3 times
My Contributions
QUOTE(matto @ 25 Jun, 2009 - 12:52 PM) *

I did, I put "Accounts" in place of MyTable. Should I not use quotation marks?


Yes you must use quotation marks everywhere. If it still gives an error then try posting me more of your code especially the sub\function that sets up your connection componets and so on.

User is offlineProfile CardPM
+Quote Post

matto

RE: VB.NET And ADO.NET - Deleting Rows

26 Jun, 2009 - 08:09 AM
Post #7

New D.I.C Head
*

Joined: 18 Apr, 2009
Posts: 40

Right, well I've encountered another problem now. I have a listview which is populated with a table from an access database. I want to be able to delete a listview item which deletes the corresponding row in the table. I think it would be easier to delete the listview items and on form.close populate the database with the listview items. What do you suggest?
User is offlineProfile CardPM
+Quote Post

matto

RE: VB.NET And ADO.NET - Deleting Rows

3 Jul, 2009 - 02:26 PM
Post #8

New D.I.C Head
*

Joined: 18 Apr, 2009
Posts: 40

Any ideas? Desprately need help as there is hardly any information available
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 01:52AM

Live VB.NET Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month