Results 1 to 5 of 5

Thread: [RESOLVED] Saving Items From Datagridview to Database?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2012
    Location
    California
    Posts
    133

    Resolved [RESOLVED] Saving Items From Datagridview to Database?

    I have a datagridview and items get populated to it from a MySQL Database.

    My Goal is to creating a receipt and saving it to the database

    So all the items in the datagridview need to get saved to the database.

    or I don't care where I save it , as long as the vb.net application can retrieve the data. IE: Like for returns, or update the receipt.


    So, How or what is a good way to do this?


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

    Re: Saving Items From Datagridview to Database?

    If you're using a MySQL then, if you haven't already, you should first install Connector/Net, which is a freely downloadable, MySQL-specific ADO.NET provider. You can then reference the MySql.Data.dll assembly in your application project.

    In code, use a MySqlDataAdapter to Fill a DataTable with the data from the database and bind that table to your DataGridView via a BindingSource. Once you've made your changes, use the same adapter to Update the database with the changes from the table. For a code example of the data access part, follow the CodeBank link in my signature and check out my thread on Retrieving & Saving Data.
    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

    Thread Starter
    Addicted Member
    Join Date
    Apr 2012
    Location
    California
    Posts
    133

    Re: Saving Items From Datagridview to Database?

    Thanks for the reply.

    I do have the MySQL Connector/Net setup with my database. I know How to Update, Insert, Save, and Delete.

    It is just, like this scenario:

    When I click Save Receipt, the Application Generates a receipt Number

    So, Receipt # 1 has 50 items on it,

    How would 50 items on the receipt get saved to the database?

    Would I create a Table for each receipt # created in the Database, and the rows are the items?

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

    Re: Saving Items From Datagridview to Database?

    You would have one table for the receipt records and one table for the items on the receipts. There are numerous examples around like this, including the Order and OrderDetail tables in the Northwind and AdventureWorks example databases from Microsoft. For instance, you might have a Receipt table with a ReceiptID column as primary key. You might also have a ReceiptItem table with a ReceiptItemID column as primary key and a ReceiptID column as a foreign key from the Receipt table. If you save a Receipt record with a ReceiptID of 1 then you save all the items on that receipt to the ReceiptItem table with a ReceiptID of 1. You can then get all the items on a receipt by specifying the appropriate foreign key value to filter the data.
    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

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Apr 2012
    Location
    California
    Posts
    133

    Re: Saving Items From Datagridview to Database?

    Got It Thanks

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