Solved

Write data back to access table from DataGrid control

Posted on 2006-06-22
3
204 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

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

Suggested Solutions

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
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.

758 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

11 Experts available now in Live!

Get 1:1 Help Now