Solved

Save DataGrid Edited data to MS Access Database

Posted on 2008-10-28
4
709 Views
Last Modified: 2012-06-27
Visual Studio 2005
Visual Basic.net

I have a DataGrid that has displayed some data to a user. (it reads an XML file)
The User has the ability to edit the individual cells on the datagrid.

Once they are done I want them to be able to click a button and write all the datagrid data to a MS Access database.

Database is called: "OnlineOrders.mdb"
Table is called "Orders"

There are 5 Fields: OrderID, Product, Quantity, Cost, Description
4 of which correspond to the displayed DataGrid colums (Product, Quantity, Cost, Description)  
OrderID is stored asa variable by itself.

Each row will need to be inserted into the database
I know what the SQL should be:
"Insert  into Orders (OrderID, Product, Quantity, Cost, Description) Values (''+OrderID+', '"+Product+"', '"+Quantity+"', '"+Cost+"', '"+Description+"')"

But I do not know how to setup the database code in VB.net orhow to loop through the rows.
I need help with the data setup and then looping through the datagrid contents.
0
Comment
Question by:EGormly
  • 3
4 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 22829427
Hi,
You can write code similar to the following.


Dim dbcon as new oledbconnection("connectionstring")
dbcon.open
dim dbcmd as new oledbcommand
dbcmd.connection=dbcon
for i as integer = 0 to dgrid.rows.count - 1
    dbcmd.commandtext="Insert Into Orders(OrderID, Product, Quantity, Cost, Description) Values(" & i+1 & ", '" & dgrid.rows.item(i).item(0) & "', " & dgrid.rows.item(i).item(1) & ", " & dgrid.rows.item(i).item(2) & ", '" & dgrid.rows.item(i).item(3) & "')"
dbcmd.executenonquery()
next
dbcmd.dispose
dbcon.close
dbcon.dispose

Open in new window

0
 

Author Comment

by:EGormly
ID: 22830128
I get:
Error 2 'item' is not a member of 'System.Windows.Forms.DataGridViewRow'.
Refering to this part: dgrid.rows.item(i).item(0)

Testing it out when I type "dgrid.rows.item(i)." the code suggest box that pops up does not give "item" as an option in the list
0
 

Author Comment

by:EGormly
ID: 22830212
I figured it out thank you..

it was to use it this way:
dgrid.Rows.Item(i).Cells(0).Value
0
 

Author Closing Comment

by:EGormly
ID: 31510761
Awesome.. not exactly right but close enough for me to get it..
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question