UPDATE query not working

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • juster21
    New Member
    • Feb 2008
    • 24

    UPDATE query not working

    I am trying to create an update query but it is failing to make any changes.
    Below is the code I have...
    Code:
        Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
            Dim strSqlEditRecord As String
            Dim myCmd As OleDb.OleDbCommand
            myCmd = Nothing
            strSqlEditRecord = "UPDATE tblMain SET Per_Hour_Quota= '" & txtQuota.Text & "' WHERE EnteredRecord= '" & lblRecordNumber.Text & "';"
            myCmd = New OleDb.OleDbCommand(strSqlEditRecord, gMDB)
            myCmd.Parameters.AddWithValue("@Per_Hour_Quota", txtQuota.Text)
            myCmd.CommandType = CommandType.Text
            myCmd.CommandTimeout = 60
            myCmd.ExecuteNonQuery()
            MsgBox("This record has been updated in the database.", MsgBoxStyle.Information, "Saved Record")
            Clear()
        End Sub
    I have a form with a button to query the db and return records based on the criteria selected. The idea here is to allow the user to then make changes to that record and save back to the db. Can anyone tell me where the error is? Thanks!!
  • nateraaaa
    Recognized Expert Contributor
    • May 2007
    • 664

    #2
    There is an easy way to troubleshoot database errors. Surround your code with a try catch block. If the code in the try does not successfully execute the code will fall into the catch block giving you details of the problem.

    Nathan

    Comment

    • DrBunchman
      Recognized Expert Contributor
      • Jan 2008
      • 979

      #3
      Have you tried running your update query in Management Studio first to see whether it works?

      Is your code erroring and causing an exception?

      Dr B

      Comment

      • juster21
        New Member
        • Feb 2008
        • 24

        #4
        Originally posted by nateraaaa
        There is an easy way to troubleshoot database errors. Surround your code with a try catch block. If the code in the try does not successfully execute the code will fall into the catch block giving you details of the problem.

        Nathan
        I tried...
        Code:
                Try
                    myCmd.ExecuteNonQuery()
                Catch ex As Exception
                    MessageBox.Show(ex.ToString)
                End Try
        ...nothing was caught but there was no update made to the database either

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          I would suspect your WHERE clause.
          Is the correct information making it into that statement?
          If EnteredRecord is not a string column, you should NOT surround your values with ' (single quote)

          I also see you entering a parameter into it which doesn't look like it does anything.

          I would agree with whoever it was that said you should test the query out in the SQL management program
          Last edited by Plater; Feb 14 '08, 03:12 PM. Reason: Added a missing NOT in the statement

          Comment

          • juster21
            New Member
            • Feb 2008
            • 24

            #6
            I'm pretty sure the SQL statement is right but maybe not so much with the vb code...please advise if you know better. Thanks!!

            Code:
                Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
                    Dim strSqlEditRecord As String
                    Dim myCmd As OleDb.OleDbCommand
                    myCmd = Nothing
                    strSqlEditRecord = "UPDATE tblMain SET tblMain.Per_Hour_Quota= '" & txtQuota.Text & "' WHERE tblMain.EnteredRecord= '" & lblRecordNumber.Text & "';"
                    myCmd = New OleDb.OleDbCommand(strSqlEditRecord, gMDB)
                    myCmd.Parameters.AddWithValue("@Per_Hour_Quota", txtQuota.Text)
                    myCmd.CommandType = CommandType.Text
                    myCmd.CommandTimeout = 60
                    Try
                        myCmd.ExecuteNonQuery()
                    Catch ex As Exception
                        MessageBox.Show(ex.ToString)
                    End Try
            
                    MsgBox("This record has been updated in the database.", MsgBoxStyle.Information, "Saved Record")
                    Clear()
                End Sub

            Comment

            • Shashi Sadasivan
              Recognized Expert Top Contributor
              • Aug 2007
              • 1435

              #7
              strSqlEditRecor d = "UPDATE tblMain SET tblMain.Per_Hou r_Quota= '" & txtQuota.Text & "' WHERE tblMain.Entered Record= '" & lblRecordNumber .Text & "';"
              myCmd = New OleDb.OleDbComm and(strSqlEditR ecord, gMDB)
              myCmd.Parameter s.AddWithValue( "@Per_Hour_Quot a", txtQuota.Text)
              myCmd.CommandTy pe = CommandType.Tex t


              Is there a @Per_Hour_Quota in your strSqlRecord?

              well, the thing is it does not require a parameter to start with.
              I think you can delete the following line
              myCmd.Parameter s.AddWithValue( "@Per_Hour_Quot a", txtQuota.Text)

              You mentioned about an error !
              Is there an exception which is being thrown?
              if yes, then where and what is the the exception

              Comment

              • juster21
                New Member
                • Feb 2008
                • 24

                #8
                Originally posted by Shashi Sadasivan
                strSqlEditRecor d = "UPDATE tblMain SET tblMain.Per_Hou r_Quota= '" & txtQuota.Text & "' WHERE tblMain.Entered Record= '" & lblRecordNumber .Text & "';"
                myCmd = New OleDb.OleDbComm and(strSqlEditR ecord, gMDB)
                myCmd.Parameter s.AddWithValue( "@Per_Hour_Quot a", txtQuota.Text)
                myCmd.CommandTy pe = CommandType.Tex t


                Is there a @Per_Hour_Quota in your strSqlRecord?

                well, the thing is it does not require a parameter to start with.
                I think you can delete the following line
                myCmd.Parameter s.AddWithValue( "@Per_Hour_Quot a", txtQuota.Text)

                You mentioned about an error !
                Is there an exception which is being thrown?
                if yes, then where and what is the the exception
                if I remove myCmd.Parameter s.AddWithValue( "@Per_Hour_Quot a", txtQuota.Text) then I get an error saying 'No value given for one or more required parameters'.
                The error is thrown after Try
                myCmd.ExecuteNo nQuery()

                Comment

                • kunal pawar
                  Contributor
                  • Oct 2007
                  • 297

                  #9
                  when u used text always used trim function. As u said there is no error and no any records affected mean problem in ur where condition.

                  Private Sub btnSave_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles btnSave.Click

                  Dim strSqlEditRecor d As String
                  Dim myCmd As OleDb.OleDbComm and
                  4.
                  myCmd = Nothing
                  5.
                  strSqlEditRecor d = "UPDATE tblMain SET Per_Hour_Quota= '" & txtQuota.Text & "' WHERE EnteredRecord= '" & trim(lblRecordN umber.Text) & "';"
                  6.
                  myCmd = New OleDb.OleDbComm and(strSqlEditR ecord, gMDB)
                  7.
                  myCmd.Parameter s.AddWithValue( "@Per_Hour_Quot a", txtQuota.Text)
                  8.
                  myCmd.CommandTy pe = CommandType.Tex t
                  9.
                  myCmd.CommandTi meout = 60
                  10.
                  myCmd.ExecuteNo nQuery()
                  11.
                  MsgBox("This record has been updated in the database.", MsgBoxStyle.Inf ormation, "Saved Record")
                  12.
                  Clear()
                  13.
                  End Sub

                  Comment

                  • juster21
                    New Member
                    • Feb 2008
                    • 24

                    #10
                    Originally posted by kunal pawar
                    when u used text always used trim function. As u said there is no error and no any records affected mean problem in ur where condition.

                    Private Sub btnSave_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles btnSave.Click

                    Dim strSqlEditRecor d As String
                    Dim myCmd As OleDb.OleDbComm and
                    4.
                    myCmd = Nothing
                    5.
                    strSqlEditRecor d = "UPDATE tblMain SET Per_Hour_Quota= '" & txtQuota.Text & "' WHERE EnteredRecord= '" & trim(lblRecordN umber.Text) & "';"
                    6.
                    myCmd = New OleDb.OleDbComm and(strSqlEditR ecord, gMDB)
                    7.
                    myCmd.Parameter s.AddWithValue( "@Per_Hour_Quot a", txtQuota.Text)
                    8.
                    myCmd.CommandTy pe = CommandType.Tex t
                    9.
                    myCmd.CommandTi meout = 60
                    10.
                    myCmd.ExecuteNo nQuery()
                    11.
                    MsgBox("This record has been updated in the database.", MsgBoxStyle.Inf ormation, "Saved Record")
                    12.
                    Clear()
                    13.
                    End Sub
                    Thanks for the help. I tried adding the trim function but the record did not save and did not error. Any other thoughts?

                    Comment

                    • juster21
                      New Member
                      • Feb 2008
                      • 24

                      #11
                      Originally posted by juster21
                      Thanks for the help. I tried adding the trim function but the record did not save and did not error. Any other thoughts?
                      as it turns out, the problem was simple. I misspelled a field name in the database and had to remove the single quotes from the SQL. works fine now.

                      Comment

                      Working...