Results 1 to 12 of 12

Thread: DataAdapter and using .Update to save dataset

  1. #1

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    DataAdapter and using .Update to save dataset

    VB Code:
    1. Public Sub Update(ByVal CommandText As String, ByRef MyData As DataSet)
    2.         Dim Comm As SqlCommand
    3.         If _Transaction Is Nothing Then
    4.             Comm = New SqlCommand(CommandText, Connection)
    5.         Else
    6.             Comm = New SqlCommand(CommandText, Connection, _Transaction)
    7.         End If
    8.         Dim da As New SqlDataAdapter(Comm)
    9.         da.UpdateCommand = Comm
    10.         da.Update(MyData)
    11.     End Sub
    That's what I have.
    It doesn't error, but it also doesn't update my DB.
    I use (and I know * is bad, but it's testing) the following as the CommandText:
    Code:
    SELECT * FROM Customers
    So I fetch a dataset using the above sql, and then I use the above update statement to save it. I know I am doing something REALLY daft.

    Woka

  2. #2
    Addicted Member techwizz's Avatar
    Join Date
    Apr 2005
    Location
    U.S.A.
    Posts
    246

    Re: DataAdapter and using .Update to save dataset

    Will you attatch the file? Ill look at if u want?

  3. #3

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: DataAdapter and using .Update to save dataset

    What is the CommandText used for the update command? Also, are you committing the transaction if and when one is used?

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: DataAdapter and using .Update to save dataset

    You're using a SELECT statement in your update command?

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: DataAdapter and using .Update to save dataset

    w00f!!! Doh!
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: DataAdapter and using .Update to save dataset

    Quote Originally Posted by RobDog888
    w00f!!! Doh!

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: DataAdapter and using .Update to save dataset

    Quote Originally Posted by mendhak
    You're using a SELECT statement in your update command?
    I don't think he is, although that's what I first thought from the wording. Using a SELECT statement in an UpdateCommand would raise an exception, would it not? I think it's because the transaction is not being committed. If you don't call Commit on a transaction, my guess is that it would get rolled back by default. You would receive no notification of this, as is happening. I'm pretty sure that has happened to me in the past, although it was obviously a long time ago

    Edit:
    That's assuming that the route that uses a transaction is being followed. If not, forget everything I just said. In fact, I was never here!

  9. #9

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: DataAdapter and using .Update to save dataset

    I am using "Select * from Customers Where ID = 5", the same sql I use to get the dataset.
    It's not the transaction.
    I try this with or without starting a transaction, and the same thing happens.
    I do no get an exception either.

    I am using 2005 Beta 2, but I am assuming the ADO.NET is still the same as 2003.

    Woka

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: DataAdapter and using .Update to save dataset

    Then mendhak was right. You need to use a SELECT statement to retrieve rows, a DELETE statement to delete rows, an INSERT statement to add new rows and an UPDATE statement to edit rows. You need to use something like:
    Code:
    UPDATE Customers SET Name = @Name WHERE ID = @ID
    and then add some parameters to the SqlCommand object to substitute the actual values for the placeholders (@Name and @ID).

  11. #11

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: DataAdapter and using .Update to save dataset

    Hmmm...I am sure I have done this is the past where the dataset has remembered how to ties into the DB and auto updates the correct field.

    Will give that a go though.
    Cheers.

    Woka

    PS Mendhack was right coz he read my 1st post correctly hahaa

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: DataAdapter and using .Update to save dataset

    If you want to generate non-query commands automatically, you can use the "Configure Data Adapter..." function at the bottom of the properties window, assuming you have created the adapter in the designer, or you can use an SqlCommandBuilder. The command builder is frowned on in some parts and does have limitations. I never use it myself, but it is quick and easy. The Data Adapter Configuration Wizard is a liitle more involved, so gives you more control, but is still relatively easy to use.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width