Results 1 to 13 of 13

Thread: How to Save tables(dataset) to database

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2004
    Posts
    252

    How to Save tables(dataset) to database

    Hi all GURUs,
    I am new in .NET.

    Can you please tell me how to:

    Save updated tables(dataset) to database.
    I my code dosent work, its as follows:

    'Caeate new dataset to hold changes from main dataset.
    Dim dschanges As DataSet = DsInventory1.GetChanges()
    'stop any current edits.
    Me.BindingContext(DsInventory1, "Vendor").EndCurrentEdit()
    'Get the changes that have been made to main dataset.
    dsChanges = CType(DsInventory1.GetChanges, dsInventory)
    'Check to see if any changes have been made.
    If (Not (dsChanges) Is Nothing) Then
    Try
    Me.ConnINVENTORY.Open()
    'Attempt to update data Source.
    DsInventory1.Tables("Vendor").AcceptChanges()
    DataAdINVENTORY.Update(dsChanges)
    DataAdINVENTORY.Update(DsInventory1, "Vendor")
    MsgBox("Record saved Successfully")
    Catch updateExcepatoin As System.Exception
    MsgBox("Can not save to data Source")
    Finally
    Me.ConnINVENTORY.Close()
    End Try
    DsInventory1.Merge(dsChanges)
    DsInventory1.AcceptChanges()
    End If

  2. #2
    Addicted Member
    Join Date
    Apr 2004
    Location
    Lagos, Nigeria
    Posts
    215
    In your code 'dschanges' holds changes made to 'DsInventory1'. You only need to update 'dschanges'.

    You need to call the Update method of the dataadaptor before calling the AcceptChanges method. Bear in mind you need the Update command for modified rows, DeleteCommand for deleted rows, InsertCommand for newly added rows.

    Try something like:

    VB Code:
    1. Try
    2.     Me.ConnINVENTORY.Open()
    3.     'Attempt to update data Source.
    4.     DataAdINVENTORY.Update(dsChanges)
    5.     DsInventory1.Tables("Vendor").AcceptChanges()
    6.     MsgBox("Record saved Successfully")
    7.  
    8. Catch updateExcepatoin As System.Exception
    9.     MsgBox("Can not save to data Source")
    10.  
    11. Finally
    12.     Me.ConnINVENTORY.Close()
    13.  
    14. End Try

    You do not need to merge after AcceptChanges method has been called -- remember, the dataset is a reference and not a value Type

    Goodluck!

  3. #3
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416
    try this one:
    VB Code:
    1. 'ds is a dataset
    2. 'da is a Sqldataadapter
    3. if ds.HasChanges then
    4.    dim cb as SqlCommandBuilder
    5.    da.SelectCommand = New Sqlcommand(cmdtext,cn)
    6.    cb = new Sqlcommandbuilder(da)
    7.    da.Update(ds,"Customer")
    8. end if

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jan 2004
    Posts
    252
    I tried but its not working, In dataADAPTER.UPDATE methoe it gives error.

    so, what could be the problem.

    I am awaiting for your prompt reply.
    thank you.

  5. #5
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416
    what is the error?

    will you pls. show us a little code in your project.

    so that we can figure that out..

  6. #6
    Junior Member
    Join Date
    Jul 2002
    Posts
    16

    try this

    Dim CONNMYDATABASE As New OleDb.OleDbConnection("PROVIDER=MICROSOFT.JET.OLEDB.4.0;DATA_ SOURCE=C:\MYDATABASE.MDB")
    Dim ADOWNERS As New OleDb.OleDbDataAdapter("SELECT * FROM OWNERS", CONNMYDATABASE)
    Dim CBOWNERS As New OleDb.OleDbCommandBuilder(ADOWNERS)
    Dim DSOWNERS As New DataSet
    Dim OBJROW As DataRow

    OBJROW = DSOWNERS.Tables("OWNERS").NewRow
    OBJROW.Item("OWNERID") = TXTOWNERID.Text
    OBJROW.Item("NAME") = TXTNAME.Text
    OBJROW.Item("ADDRESS") = TXTADDRESS.Text
    DSOWNERS.Tables("OWNERS").Rows.Add(OBJROW)
    ADOWNERS.Update(DSOWNERS, "OWNERS")


    PROBABLY YOUR ERROR IS IN THE .UPDATE YOU HAVE TO ADD THE NAME OF YOUR TABLE TOO.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jan 2004
    Posts
    252
    I tried

    DataAdINVENTORY.Update(DsInventory1, "Vendor")

    still it gives error in UPDATE method..

    let me clear myself. to add multiple tables [around 20 tables]
    I need one oledbDataAdapter
    one oledbDataSet
    one oledbConnection
    AM I RIGHT ?

    If I am configuring all above from code, then I dont need to configure dataAdapter or dataConnection through wizard window [graphical configuration].

    I CAN partly use code and partly use wizard window [graphical configuration]

    AM I RIGHT ?

    what could be the problem
    Last edited by NANC; Jul 27th, 2004 at 10:56 AM.

  8. #8
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416
    yes your right..
    try to use this one
    VB Code:
    1. DataAdINVENTORY.Update(DsInventory1.Tables( "Vendor"))

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jan 2004
    Posts
    252
    my hardLuck,

    I tried,

    DataAdINVENTORY.Update(DsInventory1.Tables( "Vendor"))

    but still, error comes



  10. #10
    Addicted Member
    Join Date
    Apr 2004
    Location
    Lagos, Nigeria
    Posts
    215
    but still, error comes
    What's the exact error message.

  11. #11
    Lively Member
    Join Date
    Jun 2004
    Location
    Philippines
    Posts
    125
    Originally posted by mar_zim
    try this one:
    VB Code:
    1. 'ds is a dataset
    2. 'da is a Sqldataadapter
    3. if ds.HasChanges then
    4.    dim cb as SqlCommandBuilder
    5.    da.SelectCommand = New Sqlcommand(cmdtext,cn)
    6.    cb = new Sqlcommandbuilder(da)
    7.    da.Update(ds,"Customer")
    8. end if
    I tried your code, it only updated my dataset but not my database...also, it doen't update comboboxes? but if I typed the value in my combobox, it then updates my dataset (mind you, only the dataset) I don't understand why this happens, it just happens...

    Hope you could get back to me on this...thanks a lot!

  12. #12
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    If you won't post the error message, as several people have asked you, you can't get much useful help.

  13. #13
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    NANC,

    Are you managing more than one table in your dataset?
    If so, you will not get this to work.
    Additionally, if your datatable is created from a select statement that retrieves data from more than one table, then using the update method will also not work.

    In either of these scenarios, you will have to update your data source using commands.

    And Please, Please, post the error message
    Whadayamean it doesn't work....
    It works fine on my machine!

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