Solved

Save DataGrid Edited data to MS Access Database

Posted on 2008-10-28
4
710 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 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

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…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

737 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