Solved

Write data back to access table from DataGrid control

Posted on 2006-06-22
3
225 Views
Last Modified: 2010-04-23
I have a form with a Data Grid control bound to a table and am trying to write back changes to a table.  I would like the user to click on a button to save the changes. How do I do this.  Here is my code, to fill the grid.

  Dim dbConnection As New OleDbConnection
       Dim da As New OleDbDataAdapter
        Dim dbCommand As New OleDbCommand
        Dim dt As New DataTable
        Dim ds As New DataSet
        Dim dr As DataRow
        Dim i As Integer
        Dim s As String


        dbConnection.ConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source=d:\data\docs\metalogic\test.mdb"


        Try
            dbConnection.Open()

        Catch ex As Exception
            MsgBox("Cannot open Database" & vbCr & ex.Message, MsgBoxStyle.Critical, "Error!")
            Exit Sub
        End Try
        Dim SQLStr As String
        Dim strRate As String


        SQLStr = "select *  from " & tablename



        dbCommand.Connection = dbConnection
        dbCommand.CommandType = CommandType.Text

        dbCommand.CommandText = SQLStr
        da.SelectCommand = dbCommand
        Dim cb As New OleDbCommandBuilder(da)

        da.Fill(dt)
        '===========================================

        '

        grdTable.DataSource = dt


==============================

Thanks,
B Sturge





0
Comment
Question by:bsturge
3 Comments
 
LVL 41

Assisted Solution

by:graye
graye earned 166 total points
ID: 16959342
You'd use the DataAdapter's Update method to perfrom this task...

Take a look at the following articles on how to write VB.Net code to save changes back to the underlying database  http://home.hot.rr.com/graye/Articles/SavingDataADO.htm

0
 
LVL 6

Assisted Solution

by:Nandakumar Sakthivel
Nandakumar Sakthivel earned 167 total points
ID: 16959406
Hi,
Instead of creating the data Adapter manually.you can configure the Data Adapter using the tool in visual studio.
In the Data tab of tool  there are different types of adapters available for different databases.It will generate the
 query automatically when you drag and drop and configure it
you need to use

       DataAdapter.Update(DataSet) command for updating the table

 for more info abt using adapters
    http://www.vb-helper.com/howto_net_bound_datagrid.html
    http://www.developerfusion.co.uk/show/4278/4/


Thanks,
Nanda
 
0
 
LVL 2

Accepted Solution

by:
Bill_PSC earned 167 total points
ID: 16961936
Greetings.

First you need to fill the dataset.

da.Fill(ds,"dt")

then on the button click event.

   Me.BindingContext(DS, "DT").EndCurrentEdit()
                DA.Update(DS, "DT")
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

Suggested Solutions

Title # Comments Views Activity
vb.net class 3 21
Complex SQL statement in VB.NET 7 31
Optimize the query 5 43
Visual studio 2015 1 21
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
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…
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 …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 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