[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 203
  • Last Modified:

datagrid update problem

I am having difficulty getting my datagrid to update and retain the changes in the underlying table.
I found the following code off the web but its not working for me.

I have the connection and adapter defined in the form - attempting to do a dynamic command builder off of them.
The grid will load; and you can change the data but as soon as you run the following code ; then reload the grid your changes aren't saved.

Thanks for any help.

company_d1 is my dataset

------------------------
Dim command_builder As OleDb.OleDbCommandBuilder
command_builder = New OleDb.OleDbCommandBuilder(Me.OleDbDataAdapter1)
command_builder.GetInsertCommand()
command_builder.GetDeleteCommand()
command_builder.GetUpdateCommand()
        ' Save the changes.

        Me.OleDbDataAdapter1.Update(Me.Company_d1)

-----------------------
I'm loading the dataset with the following:
dataset_company = New DataSet
        Me.OleDbDataAdapter1.Fill(dataset_company)
        Grid_Company.SetDataBinding(dataset_company, "company")
0
sny23vpb
Asked:
sny23vpb
  • 5
  • 4
1 Solution
 
Bob LearnedCommented:
Me.OleDbDataAdapter1.Fill(dataset_company, "company")
Grid_Company.DataSource = dataset_company.Tables("company")

Me.OleDbDataAdapter1.Update(dataset_company, "company")

Bob
0
 
sny23vpbAuthor Commented:
Thank you. I'm having trouble getting the following command to build out

Me.OleDbDataAdapter1.Update(dataset_company, "company")

when i click the update button I get
An unhandled exception of type 'System.InvalidOperationException' occurred in system.data.dll

Additional information: Update unable to find TableMapping['company'] or DataTable 'company'.

perhaps I'm not defining my dataset properly.

I'm defining dataset_company
   Public dataset_company As DataSet


For the update button I have
   Dim dataset_company = New DataSet
me.OleDbDataAdapter1.Update(dataset_company, "company")

For the load button I have
  Dim dataset_company = New DataSet
        Me.OleDbDataAdapter1.Fill(dataset_company, "company")
        Grid_Company.SetDataBinding(dataset_company, "company")


Thanks for your help.
0
 
maralansCommented:
At the class level:

Dim cm As CurrencyManager
Dim ds As New DataSet
Dim da As New Oledb.OledbDataAdapter

At your load event:

        da.Fill(ds, "Company")
        Grid_Company.DataSource = ds.Tables("Company")
        cm = CType(BindingContext(ds.DataSource), CurrencyManager)
        Dim cb As Oledb.OledbCommandBuilder = New Oledb.OledbCommandBuilder(ds)
     

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        da.Update(ds, "Company")
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDelete.Click
        cm.EndCurrentEdit()
        cm.RemoveAt(cm.Position)
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        cm.EndCurrentEdit()
        cm.AddNew()
    End Sub

End Class
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
maralansCommented:
Oops,

Dim da As New Oledb.OledbDataAdapter(yourSelectString, yourConnection)
0
 
sny23vpbAuthor Commented:
Thanks.

Under this event:
 Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
        Dim cb As OleDb.OleDbCommandBuilder = New OleDb.OleDbCommandBuilder(ds)
        da.Fill(ds, "Company")
        Grid_Company.DataSource = ds.Tables("Company")
        cm = CType(BindingContext(ds.DataSource), CurrencyManager)
    End Sub


On the command
Dim cb As OleDb.OleDbCommandBuilder = New OleDb.OleDbCommandBuilder(ds)

It is saying dataset cannot be converted to oleadapter

with the following variables defined in the class
Public cm As CurrencyManager
    Public ds As New DataSet
    Public da As New OleDb.OleDbDataAdapter
    Public dataset_company = New DataSet

On   cm = CType(BindingContext(ds.DataSource), CurrencyManager) - gives "datasource is not a member of dataset" during build

------
Am I not referencing an object correctly or should I change something running a lower version of ado - using Visual Studio 2003.
Thanks for any help

0
 
sny23vpbAuthor Commented:
Sorry; went back and changed to

Public sConnection As String = "Provider=Microsoft.jet.oledb.4.0; Data Source=c:\pos\pos.mdb"
    Public da As New OleDb.OleDbDataAdapter("select * from customer_transaction", sConnection)

But doesn't seem to have made a difference in the build error above.

Thanks.
0
 
maralansCommented:
Public ds= New DataSet
0
 
maralansCommented:
Public Class Form1

  Dim cs As String = "provider=microsoft.jet.oledb.4.0;data source = c:\pos\pos.mdb"
  Dim cn As New OleDb.OleDbConnection(cs)
  Dim da As New OleDb.OleDbDataAdapter("select * from customer_transaction", cn)
  Dim ds As New DataSet
  Dim cm As CurrencyManager

 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        da.Fill(ds)
        dg.DataSource = ds.Tables(0)

        cm = CType(BindingContext(dg.DataSource), CurrencyManager)
        Dim cb As OleDb.OleDbCommandBuilder = New OleDb.OleDbCommandBuilder(da)

    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        cm.EndCurrentEdit()
        cm.AddNew()
    End Sub

    Private Sub BtnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDelete.Click
        cm.EndCurrentEdit()
        cm.RemoveAt(cm.Position)
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        da.Update(ds)
    End Sub

    End Class
0
 
maralansCommented:
or, if you need:

Public cs As String = "provider=microsoft.jet.oledb.4.0;data source = c:\pos\pos.mdb"
Public cn As New OleDb.OleDbConnection(cs)
Public da As New OleDb.OleDbDataAdapter("select * from customer_transaction", cn)
Public ds As New DataSet
Public cm As CurrencyManager
0
 
sny23vpbAuthor Commented:
Thank you very much. I had to create a new form with no adapters or anything; but this worked. Appreciate your time.
Brad.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now