Results 1 to 6 of 6

Thread: Data not Updated ?

  1. #1

    Thread Starter
    Addicted Member Tengkorak's Avatar
    Join Date
    Nov 2006
    Posts
    240

    Data not Updated ?

    i can't update my data at database
    this my code :
    VB.NET Code:
    1. Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
    2.         Try
    3.             'Dim ds As New DataSet
    4.             Dim scmd As New OleDbCommand
    5.             If bAdd Then
    6.                 scmd.CommandText = "insert into inv_t_products (prod_id,prod_name,unit,unitprice) " & _
    7.                         "values(@prodid,@prodname,@unit,@unitprice)"
    8.             Else
    9.                 'modify
    10.                 scmd.CommandText = "update inv_t_products set prod_name=@prodname,unit=@unit,unitprice=@unitprice " & _
    11.                     "where prod_id=@prodid"
    12.             End If
    13.  
    14.             scmd.Parameters.Add(New OleDbParameter("@prodid", txtKd.Text))
    15.             scmd.Parameters.Add(New OleDbParameter("@prodname", txtNm.Text))
    16.             scmd.Parameters.Add(New OleDbParameter("@unit", txtSat.Text))
    17.             scmd.Parameters.Add(New OleDbParameter("@unitprice", txtHarga.Text))
    18.  
    19.             scmd.Connection = dbConn
    20.             scmd.ExecuteNonQuery()
    21.             bUpdated = True
    22.  
    23.             Me.Close()
    24.  
    25.         Catch ex As Exception
    26.             ERRDesc(ex)
    27.         End Try
    28.  
    29.         MsgBox("Saved", MsgBoxStyle.Information)
    30.  
    31.     End Sub

    i have problem when update data, data not updated.

    how the solution ?

    thank you

  2. #2
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Data not Updated ?

    ExecuteNonQuery returns an integer value indicating how many rows has been affected by the command. See what it is:

    Code:
    Dim rowsaffected As Integer = scmd.ExecuteNonQuery()
    MsgBox("Rows affected:" & rowsaffected.ToString())
    Also I didn't see where dbconn connection is opened in your code.
    Are you sure it stays open?

    Code:
    If dbconn.State <> ConnectionState.Open Then dbconn.Open()

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Data not Updated ?

    How EXACTLY do you know that data isn't saved? When you try to save data, there are only three possibilities:

    1. There are changes to save, they are saved successfully and ExecuteNonQuery returns a non-zero value.
    2. There are no changes to save so ExecuteNonQuery returns zero.
    3. An exception is thrown.

    Which is it in your case? If it's not 2 or 3 then the data IS being saved and you're just looking in the wrong place or at the wrong time. In that case, follow the first link in my signature.
    Last edited by jmcilhinney; Mar 18th, 2011 at 07:06 AM. Reason: Changed Update to ExecuteNonQuery
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Addicted Member Tengkorak's Avatar
    Join Date
    Nov 2006
    Posts
    240

    Re: Data not Updated ?

    thank you for your replay.

    i tried check connection state and the result is 1 (Open)
    and when i try this code :
    Code:
    Dim rowsaffected As Integer = scmd.ExecuteNonQuery()
    MsgBox("Rows affected:" & rowsaffected.ToString())
    the result is 0

  5. #5

    Thread Starter
    Addicted Member Tengkorak's Avatar
    Join Date
    Nov 2006
    Posts
    240

    Re: Data not Updated ?

    I've got the solution, I changed my code to:
    Code:
     scmd.CommandText = "update inv_t_products set prod_name=@prodname,unit=@unit,unitprice=@unitprice " & _
                        "where prod_id='" & txtKd.Text & "'"

  6. #6
    New Member
    Join Date
    Apr 2010
    Posts
    5

    Re: Data not Updated ?

    yes the solution is correct
    Rocky (rocks the world)
    Reliable and trustable Hyip investment Monitoring and advertising service http://www.TheBig1.Tk

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