Results 1 to 6 of 6

Thread: Having Trouble Updating Values [Resolved]

  1. #1

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Having Trouble Updating Values [Resolved]

    Creating a simple db application. 4 textboxes, and this is the code I am using in the click event of the update button:

    VB Code:
    1. 'dsDataSet is my dataset, already declared.
    2.  
    3.  Dim dr As DataRow
    4.  
    5.         'Set the primary key for the table
    6.         With dsDataSet.Tables("Customers")
    7.             dsDataSet.Tables("Customers").PrimaryKey = New DataColumn() {.Columns("CustomerID")}
    8.         End With
    9.  
    10.         Dim pkey As Object
    11.         pkey = TextBox1.Text
    12.  
    13.         dr = dsDataSet.Tables("Customers").Rows.Find(pkey)
    14.         dr.BeginEdit()     'Start editing process
    15.  
    16.         'Enter new field values in the row
    17.         dr.Item(0) = TextBox1.Text
    18.         dr.Item(1) = TextBox2.Text
    19.         dr.Item(2) = TextBox3.Text
    20.         dr.Item(3) = TextBox4.Text
    21.  
    22.  
    23.         dr.EndEdit() 'End editing process
    24.         dsDataSet.AcceptChanges()  'Submit changes to dataset
    25.  
    26.  
    27.      
    28.         MessageBox.Show("Successfully Updated")

    It seems that the dataset is being updated, but the values are not being changed in the database. What am I missing?
    Last edited by mendhak; May 25th, 2004 at 08:46 AM.

  2. #2
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Hi.

    I'm not sure, but I think you need to call the Update command instead of AcceptChanges.

    VB Code:
    1. dsDataSet.Tables("Customers").Update(dsDataSet.Tables("Customers")) 'Submit changes to dataset
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  3. #3

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    The dataset doesn't have an update command. I did eventually figure out two ways of updating the database.

    Here is the first way:

    VB Code:
    1. Dim dr As DataRow
    2.  
    3.         'Set the primary key for the table
    4.         With dsDataSet.Tables("Customers")
    5.             dsDataSet.Tables("Customers").PrimaryKey = New DataColumn() {.Columns("CustomerID")}
    6.         End With
    7.  
    8.         Dim pkey As Object
    9.         pkey = TextBox1.Text
    10.  
    11.         dr = dsDataSet.Tables("Customers").Rows.Find(pkey)
    12.         'dr.BeginEdit()     'Start editing process
    13.  
    14.         'Enter new field values in the row
    15.         dr.Item(0) = TextBox1.Text
    16.         dr.Item(1) = TextBox2.Text
    17.         dr.Item(2) = TextBox3.Text
    18.         dr.Item(3) = TextBox4.Text
    19.  
    20.  
    21.         'now we must explicitly set an UPDATE command to update the database.
    22.  
    23.         daDataAdapter.UpdateCommand = New SqlCommand("UPDATE Customers SET CompanyName = @CompanyName, ContactName = @ContactName, ContactTitle = @ContactTitle WHERE CustomerID = @CustomerID")
    24.         daDataAdapter.UpdateCommand.Parameters.Add("@CustomerID", TextBox1.Text)
    25.         daDataAdapter.UpdateCommand.Parameters.Add("@CompanyName", TextBox2.Text)
    26.         daDataAdapter.UpdateCommand.Parameters.Add("@ContactName", TextBox3.Text)
    27.         daDataAdapter.UpdateCommand.Parameters.Add("@ContactTitle", TextBox4.Text)
    28.         daDataAdapter.UpdateCommand.Connection = sqlConn
    29.  
    30.      
    31.  
    32.        daDataAdapter.Update(dsDataSet, "Customers")
    33.  
    34.  
    35.  
    36.         MessageBox.Show("Successfully Updated")

    This uses the parameters.

    This is the other way:

    VB Code:
    1. Dim dr As DataRow
    2.  
    3.         'Set the primary key for the table
    4.         With dsDataSet.Tables("Customers")
    5.             dsDataSet.Tables("Customers").PrimaryKey = New DataColumn() {.Columns("CustomerID")}
    6.         End With
    7.  
    8.         Dim pkey As Object
    9.         pkey = TextBox1.Text
    10.  
    11.         dr = dsDataSet.Tables("Customers").Rows.Find(pkey)
    12.         'dr.BeginEdit()     'Start editing process
    13.  
    14.         'Enter new field values in the row
    15.         dr.Item(0) = TextBox1.Text
    16.         dr.Item(1) = TextBox2.Text
    17.         dr.Item(2) = TextBox3.Text
    18.         dr.Item(3) = TextBox4.Text
    19.  
    20.  
    21.         'dr.EndEdit() 'End editing process
    22.         'dsDataSet.AcceptChanges()  'Submit changes to dataset
    23.  
    24.         'now we must explicitly set an UPDATE command to update the database.
    25.  
    26.  
    27.      
    28.  
    29.         Dim cb As New SqlCommandBuilder(daDataAdapter)
    30.  
    31.         daDataAdapter.Update(dsDataSet, "Customers")
    32.  
    33.  
    34.  
    35.         MessageBox.Show("Successfully Updated")

    MSDN told me that the command builder is an inefficient way. So I'd say that the parameter way (first method) is right.

    I hope that this is what everyone else does as well.

  4. #4
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Hi.

    Well, at least it was some update command

    I usually just drag & drop the table I want from the Server Explorer. Then rightclick the form and there's an option generate a dataset. That dataset has some classes to interact directly with the tables, with field names and all.
    And that has an update command allready attached.

    I find that to be the easiest way (at least that I know of).

    But anyway, I'm glad you solved you're problem.
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  5. #5

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Yeah, I did know how to do it that way, but I wasn't comfortable with it. If you'll see my other thread, I asked about "binding"... it might be :evil:

  6. #6
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Hi.

    Perhaps you're right...But I'm not too much into DB's so I'll use it anyway I can

    But I never bind to a control such as comboboxes or textboxes. I always fill those out manually, so I didn't really think of it as binding, but ofcourse you're right, alot of binding is probably going on "under cover".
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width