Solved

Save DataGrid Edited data to MS Access Database

Posted on 2008-10-28
4
703 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Awesome.. not exactly right but close enough for me to get it..
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
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 …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now