?
Solved

Save DataGrid Edited data to MS Access Database

Posted on 2008-10-28
4
Medium Priority
?
713 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

752 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