Results 1 to 5 of 5

Thread: updating a dataset using a datagrid [*Resolved*]

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Resolved updating a dataset using a datagrid [*Resolved*]

    Hello

    I have a datagrid that is filled using the dataadapter. The datagrid that can be edited. Once the customer has completed the editing l want to update the new contents of the datagrid. This is what l have so far:

    Code:
    da.SelectCommand = cmd;
    da.Fill(ds,"Product");
    
    this.grdCustomerPurchase.SetDataBinding(ds,"Product");
    
    //How would l update the dataset once the customer has edited the contents of the datagrid
    Many thanks in advance,

    Steve
    Last edited by steve_rm; Dec 9th, 2005 at 12:18 AM.
    steve

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

    Re: updating a dataset using a datagrid

    You are Filling a DataTable using a DataAdapter and to get the deletions, insertions and updates back to the database you need to Update the same DataTable with, generally, the same DataAdapter. You have created the SelectCommand for your DataAdapter as evidenced by your code. You now have to create the corresponding DeleteCommand and/or InsertCommand and/or UpdateCommand to do the obvious and call Update on the DataAdapter. I suggest that you take a look at the help/MSDN topics for those properties for code examples. Also, all the tutorial links in my signature have a section on ADO.NET that will give you information and examples. The 101 samples also contain examples of data access operations.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Addicted Member
    Join Date
    Apr 2003
    Location
    Australia
    Posts
    252

    Re: updating a dataset using a datagrid [*Resolved*]

    Hi Guys,

    I have looked everywhere, and I am almost there, but still cannot figure out how to properly update the Database. I following a SAMS C# book word to word (attached) and it doesn't have a suitable UpdateCommand --- which is very strange.

    In my sample application, urlwizard.mdb is my database. tURL is the table. UserName, Password, Domain and Port are the four fields.

    What I would like to know is a suitable UpdateCommand based on this information.

    VB Code:
    1. private void btnSave_Click(object sender, System.EventArgs e)
    2.         {
    3.             // If there is existing data, update it.
    4.             if (m_dtUsers.Rows.Count != 0)
    5.             {
    6.                 m_dtUsers.Rows[m_rowPosition]["UserName"] = txtUserName.Text;
    7.                 m_dtUsers.Rows[m_rowPosition]["Password"] = txtPassword.Text;              
    8.                 m_daDataAdapter.Update(m_dtUsers);
    9.             }
    10.  
    11.         }

    What would be the UpdateCommand for this database.

    Thanks,
    McoreD
    Attached Files Attached Files

  4. #4
    Addicted Member
    Join Date
    Apr 2003
    Location
    Australia
    Posts
    252

    Re: updating a dataset using a datagrid [*Resolved*]

    This is so weird. You cannot have "Domain" as a column in an Access datbase, which is why I have been failing to Load and Update.

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

    Re: updating a dataset using a datagrid [*Resolved*]

    Quote Originally Posted by ~*McoreD*~
    This is so weird. You cannot have "Domain" as a column in an Access datbase, which is why I have been failing to Load and Update.
    It's not weird at all. You can use whatever name you want for columns but if you choose an Access or SQL reserved word then you need to enclose it in square brackets when you include it in commands. Otherwise it is interpreted as its reserved meaning and will almost certainly create a syntax error or, in very rare cases, execute but do something other than what you intended.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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