Solved

ADO.Net Deletes From Dataset but Not Database

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

706 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

16 Experts available now in Live!

Get 1:1 Help Now