Re: Updating a database via oledb or odbc adapters.

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Val Mazur (group51a_at_hotmail.com)
Date: 04/24/04


Date: Fri, 23 Apr 2004 22:34:43 -0400

Hi,

Move AcceptChanges after you call Update method. When you call
AcceptChanges, DataSet resets state of all the rows in a DataTable to
unchanged. When you call Update method of DataAdapter, then it checks that
no rows changed and does nothing. Your code should look like

adp.Update(ds, "OrderForm")
dt.AcceptChanges()

-- 
Val Mazur
Microsoft MVP
"dreed" <anonymous@discussions.microsoft.com> wrote in message 
news:1B5023B1-9A78-481F-9D96-6131F9607104@microsoft.com...
>I am talking to an Access datatbase (MDB/MDE versions).  I can access the 
>data, and make local changes - but I cannot get the 
>changes/deletions/additions back into the database file.  I have enclosed 
>both ODBC and OLEDB examples of what I've tried.  These are representative 
>of many variations I have tried.  Everything works as expected, except the 
>UPDATE method on the datadapter doesn't actually send the data back to the 
>database.
>
> Help would be appreciated!
>
> Here is a sample with ODBC:
>
>    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As 
> System.EventArgs) Handles MyBase.Load
>        Dim cn As Odbc.OdbcConnection = New 
> Odbc.OdbcConnection("DSN=drihm2004")
>        Dim cmd As Odbc.OdbcCommand = New Odbc.OdbcCommand("Select * from 
> VarWork", cn)
>        cmd.CommandType = CommandType.Text
>        Dim da As Odbc.OdbcDataAdapter = New Odbc.OdbcDataAdapter(cmd)
>        da.SelectCommand = cmd
>        Dim cb As Odbc.OdbcCommandBuilder = New Odbc.OdbcCommandBuilder(da)
>        Dim ds As DataSet = New DataSet("DS1")
>        cn.Open()
>        da.Fill(ds, "VarWork")
>        Dim dt As DataTable
>        Dim dr As DataRow
>        dt = ds.Tables.Item(0)
>        dr = dt.NewRow
>        dr("VarWorkStr") = "1234"
>        dt.Rows.Add(dr)
>        dr.AcceptChanges()
>        dt.AcceptChanges()
>        da.Update(ds, dt.TableName.ToString)
>        da.Fill(ds, "VarWork")
>        cn.Close()
>
>    End Sub
>
> And here is one with OLEDB:
>
>    Dim CurrentDB As OleDb.OleDbConnection = New 
> OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data 
> Source=c:\drihm2004\fromdw\drihm2004.mdb")
>
> #Region " Windows Form Designer generated code "
>
>    Public Sub New()
>        MyBase.New()
>
>        'This call is required by the Windows Form Designer.
>        InitializeComponent()
>
>        'Add any initialization after the InitializeComponent() call
>
>    End Sub
>
>    'Form overrides dispose to clean up the component list.
>    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
>        If disposing Then
>            If Not (components Is Nothing) Then
>                components.Dispose()
>            End If
>        End If
>        MyBase.Dispose(disposing)
>    End Sub
>
>    'Required by the Windows Form Designer
>    Private components As System.ComponentModel.IContainer
>
>    'NOTE: The following procedure is required by the Windows Form Designer
>    'It can be modified using the Windows Form Designer.
>    'Do not modify it using the code editor.
>    Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
>    Friend WithEvents OleDbConnection1 As System.Data.OleDb.OleDbConnection
>    <System.Diagnostics.DebuggerStepThrough()> Private Sub 
> InitializeComponent()
>        Me.ListBox1 = New System.Windows.Forms.ListBox
>        Me.OleDbConnection1 = New System.Data.OleDb.OleDbConnection
>        Me.SuspendLayout()
>        '
>        'ListBox1
>        '
>        Me.ListBox1.Location = New System.Drawing.Point(152, 16)
>        Me.ListBox1.Name = "ListBox1"
>        Me.ListBox1.Size = New System.Drawing.Size(496, 550)
>        Me.ListBox1.TabIndex = 0
>        '
>        'OleDbConnection1
>        '
>        Me.OleDbConnection1.ConnectionString = "Jet OLEDB:Global Partial 
> Bulk Ops=2;Jet OLEDB:Registry Path=;Jet OLEDB:Database L" & _
>        "ocking Mode=1;Jet OLEDB:Database Password=;Data 
> Source=""c:\proto\drihm2004.mde"";" & _
>        "Password=;Jet OLEDB:Engine Type=5;Jet OLEDB:Global Bulk 
> Transactions=1;Provider=" & _
>        """Microsoft.Jet.OLEDB.4.0"";Jet OLEDB:System database=;Jet 
> OLEDB:SFP=False;Extende" & _
>        "d Properties=;Mode=""ReadWrite|Share Deny None"";Jet OLEDB:New 
> Database Password=;" & _
>        "Jet OLEDB:Create System Database=False;Jet OLEDB:Don't Copy Locale 
> on Compact=Fa" & _
>        "lse;Jet OLEDB:Compact Without Replica Repair=False;User 
> ID=Admin;Jet OLEDB:Encry" & _
>        "pt Database=False"
>        '
>        'Form1
>        '
>        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
>        Me.ClientSize = New System.Drawing.Size(648, 582)
>        Me.Controls.Add(Me.ListBox1)
>        Me.Name = "Form1"
>        Me.Text = "Form1"
>        Me.ResumeLayout(False)
>
>    End Sub
>
> #End Region
>
>    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As 
> System.EventArgs) Handles MyBase.Load
>        Dim Com As OleDb.OleDbCommand = New OleDb.OleDbCommand("Select * 
> from OrderForm")
>        Dim adp As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(Com)
>        Dim cb As OleDb.OleDbCommandBuilder = New 
> OleDb.OleDbCommandBuilder(adp)
>        Dim ds As DataSet = New DataSet("ds1")
>        Dim dt As DataTable = New DataTable("OrderForm")
>        Dim dr As DataRow
>        Dim ll As Integer
>        Dim loAdapt As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter
>
>        CurrentDB.Open()
>        Com.Connection = CurrentDB
>        adp.Fill(ds, "OrderForm")
>        dt = ds.Tables.Item(0)
>        ListBox1.Items.Add("Initial Orders=" & dt.Rows.Count.ToString)
>        dr = dt.NewRow
>        dt.Rows.Add(dr)
>        dt.AcceptChanges()
>        adp.Update(ds, "OrderForm")
>        ListBox1.Items.Add("After Adding Orders=" & dt.Rows.Count.ToString)
> end sub 


Relevant Pages

  • RE: POPUp - VB.Net
    ... ErrForm is the error form used above. ... Public Delegate Sub ErrorInvokeHandler ... 'This call is required by the Windows Form Designer. ... Private Sub btnHide_Click(ByVal sender As System.Object, ...
    (microsoft.public.vsnet.general)
  • RE: POPUp - VB.Net
    ... Public Delegate Sub ErrorInvokeHandler ... 'This call is required by the Windows Form Designer. ... 'Add any initialization after the InitializeComponent() call ... Private Sub btnHide_Click(ByVal sender As System.Object, ...
    (microsoft.public.vsnet.general)
  • Re: What do you do when ListView.Items.Clear() doesnt clear??
    ... Public Sub New ... 'This call is required by the Windows Form Designer. ... 'Add any initialization after the InitializeComponent() call ... Private Sub InitializeComponent() ...
    (microsoft.public.dotnet.languages.vb)
  • question about module variables and window close events
    ... after opening Form2 by clicking a button on Form1 and ... Public Sub New ... 'Required by the Windows Form Designer ... Private Sub Button1_Click(ByVal sender As System.Object, ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Scrollbars for Picturebox Controls
    ... Using just the panel you have to ... 'This call is required by the Windows Form Designer. ... Protected Overloads Overrides Sub Dispose ... Private Sub Panel1_Paint(ByVal sender As Object, ...
    (microsoft.public.dotnet.languages.vb)