?
Solved

ADO.Net Deletes From Dataset but Not Database

Posted on 2006-10-29
3
Medium Priority
?
196 Views
Last Modified: 2010-04-23
As mentioned in the title the code that I have to delete a record will delete it from the dataset but not from the database.  The other code that I have to Add, Update, and Select records works fine, but for some reason my delete is not working.  Can anyone offer some suggestions on how to resolve this.  I am fairly new to using ADO.Net but I would like to be able to start using it.

In case anyone is wondering why I am not using the wizards I am trying to create a data class that will be consumed by the actual application.  There is no visual representation here.  I would also like to get familiar with the actual code since it always gives you more control.

Here is the Code:

---------------------------------------------------------------------------------------
Constructor
---------------------------------------------------------------------------------------
        Private Sub New()
            Try
                ' Get Connection and Open Connection
                sqlConn = Functions.GetConnection

                '
                ' Select Command
                '
                With cmdSelect
                    .CommandText = "Select * from tblManufacturerGeneral"
                    .Connection = sqlConn
                End With
               
                '
                ' Insert Command
                '
                With cmdInsert
                    .CommandText = "INSERT INTO tblManufacturerGeneral " & _
                                   "(MBusGUID, Commission, CreationDate, ModifyDate) " & _
                                   "VALUES " & _
                                   "(@MBusGUID, @Commission, @CreationDate, @ModifyDate)"
                    .Connection = sqlConn
                    With .Parameters
                        .Add("@MBusGUID", SqlDbType.UniqueIdentifier, 16, "MBusGUID")
                        .Add("@Commission", SqlDbType.Decimal, 18, "Commission")
                        .Add("@CreationDate", SqlDbType.DateTime, 8, "CreationDate")
                        .Add("@ModifyDate", SqlDbType.DateTime, 8, "ModifyDate")
                    End With
                End With
               
                '
                ' Update Command
                '
                With cmdUpdate
                    .CommandText = "UPDATE [tblManufacturerGeneral] " & _
                                   "SET MBusGUID = @MBusGUID, Commission = @Commission, CreationDate = @CreationDate, ModifyDate = @ModifyDate " & _
                                   "WHERE MBusGUID = @MBusGUID"
                    .Connection = sqlConn
                    With .Parameters
                        .Add("@MBusGUID", SqlDbType.UniqueIdentifier, 16, "MBusGUID")
                        .Add("@Commission", SqlDbType.Decimal, 18, "Commission")
                        .Add("@CreationDate", SqlDbType.DateTime, 8, "CreationDate")
                        .Add("@ModifyDate", SqlDbType.DateTime, 8, "ModifyDate")
                    End With
                End With
               
                '
                ' Delete Command
                '
                With cmdDelete
                    .CommandText = "DELETE " & _
                                   "FROM tblManufacturerGeneral " & _
                                   "WHERE MBusGUID = @MBusGUID"
                    .Connection = sqlConn
                    With .Parameters
                        .Add("@MBusGUID", SqlDbType.UniqueIdentifier, 16, "MBusGUID")
                    End With
                End With

                ' Configure the Data Adapter
                myDataAdapter.SelectCommand = cmdSelect
                myDataAdapter.UpdateCommand = cmdUpdate
                myDataAdapter.DeleteCommand = cmdDelete
                myDataAdapter.InsertCommand = cmdInsert
                myDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey

                ' Fill the Dataset
                myDataAdapter.Fill(myDataset, "tblManufacturerItems")
            Catch ex As Exception
                'TODO Add Error Handler
            End Try
        End Sub
---------------------------------------------------------------------------------------
Delete Procedure
---------------------------------------------------------------------------------------
        Public Sub DeleteRecord(ByVal BusGUID As String)
            Dim drMItems As DataRow

            If Exists(BusGUID) Then
                drMItems = myDataset.Tables(0).Rows.Find(BusGUID)
                myDataset.Tables(0).Rows.Remove(drMItems)
                myDataAdapter.Update(myDataset.Tables(0))
            Else
                'TODO: Add Error Handler
            End If
        End Sub
0
Comment
Question by:kmcbrearty
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 3

Author Comment

by:kmcbrearty
ID: 17831641
If found the solution.

---------------------------------------------------------------------------------------
New Delete Procedure
---------------------------------------------------------------------------------------

Public Sub DeleteRecord(ByVal BusGUID As String)
            Dim drMItems As DataRow
            Dim rowIndex As Long

            If Exists(BusGUID) Then
                drMItems = myDataset.Tables(0).Rows.Find(BusGUID)
                rowIndex = myDataset.Tables(0).Rows.IndexOf(drMItems)

                myDataset.Tables(0).Rows(rowIndex).Delete()
                myDataAdapter.Update(myDataset.Tables(0))
            Else
                'TODO: Add Error Handler
            End If
        End Sub
0
 
LVL 6

Accepted Solution

by:
manch earned 1000 total points
ID: 17832443

Write one more statement after updating the dataset

i.e. After Statement
myDataAdapter.Update(myDataset.Tables(0))

write :
myDataSet.AcceptChanges()

Try it
0
 
LVL 18

Assisted Solution

by:vbturbo
vbturbo earned 1000 total points
ID: 17833412
You could also use this to update all your changes made to the dataset


Public Sub UpdateDatabase()

Dim cb As New OleDb.OleDbCommandBuilder(da)

da.Update(myDataset, "tblManufacturerGeneral")

MsgBox("Data updated")
End Sub

Checkout this tutorial on working with dataset's. Gives you multiple posibilyties with a minimum of code effort and code lines.
I fased out all my old ado concepts after going through this ! (takes about half an hour)

http://www.homeandlearn.co.uk/NET/nets12p9.html

And here from the begining

http://www.homeandlearn.co.uk/NET/nets12p4.html

vbturbo
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question