[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?
:ehh::confused:
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.
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?
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.
Re: Saving Items From Datagridview to Database?