Solved

ADO.Net Deletes From Dataset but Not Database

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

705 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