Solved

ADO.Net Deletes From Dataset but Not Database

Posted on 2006-10-29
3
185 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
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 250 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 250 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now