Solved

Save DataGrid Edited data to MS Access Database

Posted on 2008-10-28
4
708 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with Syntax 9 36
Exit the loop 4 42
Run a batch file when a Crystal Report is opened 5 27
cannot connect to sqlserver 8 25
This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

786 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