Results 1 to 6 of 6

Thread: How to Save table to database

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2004
    Posts
    252

    How to Save table to database

    Hi all Gurus,
    I am new to .net & I have very simple problem.

    to save changes from dataset to database, I have written following code:


    Private Sub updateDataSource()

    'stop any current edits.
    Me.BindingContext(DsInventory1, "Vendor").EndCurrentEdit()
    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


    It seems that code runs fine but no changes are made in database,
    can you please help me out earlier.

    Thank you

  2. #2
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    Not really sure, but I don't think you want to call the AcceptChanges method.

    In addition, you may want to consider creating a new data set with the GetChanges method, this will return a data set that has only your changes (updates, inserts, deletes).

    Here's some code, it's C#, it seems to work fine.

    Client calls a web service to update changes:
    PHP Code:
            private void btnUpdate_Click(object senderSystem.EventArgs e)
            {
                
    statusBar1.Text "Updating...";
                
    DataSet changes dsThisTable.GetChanges();
                if (
    changes == null)
                {
                    
    statusBar1.Text "Nothing to update";
                    
    MessageBox.Show("There are no changes to update.""Nothing to Update"MessageBoxButtons.OKMessageBoxIcon.Information);
                }
                else
                {
                    
    PatriotWebService.PatriotClientWebService ws = new PatriotWebService.PatriotClientWebService();
                    
    string result "0";
                    try
                    {
                        
    result ws.UpdateTable(dgSelectedTable.CaptionTextchanges);
                    }
                    catch (
    SoapException sex)
                    {
                        
    MessageBox.Show(sex.Message"Error"MessageBoxButtons.OKMessageBoxIcon.Error);
                    }
                    
    statusBar1.Text result " row(s) affected";
                    
    RefreshData(dgSelectedTable.CaptionText);
                }
            } 
    Web service method:
    PHP Code:
            [WebMethod]
            public 
    string UpdateTable(string tableNameDataSet ds)
            {
                
    SqlConnection myConnection = new SqlConnection();
                
    myConnection.ConnectionString myConnectionString;
                
    SqlDataAdapter myAdapter = new SqlDataAdapter();
                
    string sql "SELECT * FROM " tableName;
                
    myAdapter.SelectCommand = new SqlCommand(sqlmyConnection);
                
    // The Update method would fail unless specifying my own
                // insert, update and delete commands, or just using the
                // automatic ones as the next line does.
                
    SqlCommandBuilder custCB = new SqlCommandBuilder(myAdapter);
                
    int i myAdapter.Fill(new DataSet(), tableName);
                
    int j myAdapter.Update(dstableName);
                
    string returnString j.ToString();
                return 
    returnString;
            } 
    HTH,
    Mike

  3. #3
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    i remember seeing a 'hasChanges()' member. It actually will create the insert/update sql commands for you and everything. I tried to find that article but I have so many in my favorites, it was a lost cause. I'll still look for it and post it if I find it.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jan 2004
    Posts
    252

    save data

    Can you please give me code in VB.NET to save data in database.

    Thank you,

  5. #5
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    are you wanting to know how to save data to a table or how to update a dataset that has changed?

    those are two different problems.

  6. #6
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416
    to save in the database
    VB Code:
    1. if ds.haschanges then
    2. dim cb as Sqlcommandbuilder
    3. 'da is an sqldataadapter
    4. cb=new Sqlcommandbuilder(da)
    5. da.update()
    6.  
    7. 'if you want to get only the changes in your table
    8.  
    9. Dim TempTbl As DataTable
    10. TempTbl = ds.Tables(0).GetChanges
    11. Dim cb As New SqlCommandBuilder()
    12. cb = New SqlCommandBuilder(da)
    13. da.Update(TempTbl)
    14. end if

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