Solved

ADO.Net Deletes From Dataset but Not Database

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Passing data between Forms 3 28
vb.net, web page, SQL holding the selected data in an accessible variable 7 34
ModalPopup  question 22 39
Copying from a network share 3 26
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
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 …

726 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