8 Replies - 321 Views - Last Post: 25 February 2012 - 01:57 AM Rate Topic: -----

#1 EtienneBordeaux  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 43
  • Joined: 09-February 12

Update syntax error

Posted 24 February 2012 - 11:59 PM

Is there something wrong with my code? I've followed my update code from my previous project. By the way I use text as datatype on my database but the inputs were numbers(used for payment/fees). I tried to remove the single quotes but it is still no good. What do you think guys? Thanks in advance if someone could help.

cmd.CommandText = "UPDATE tblSOF SET Tuition ='" & txtTuition.Text & "', Registration ='" & txtReg.Text & "', Athletic ='" & txtAfee.Text & "', Guidance ='" & txtGuidance.Text & "', LearningMatAndKit ='" & txtMat.Text & "', ScienceLab ='" & txtScience.Text & "', CompLab ='" & txtComplab.Text & "', Library ='" & txtLib.Text & "', MedicalDental ='" & txtMed.Text & "', SHA ='" & txtSha.Text & "', SchoolPublication ='" & txtSpub.Text & "', IDFee ='" & txtIDfee.Text & "', SWF ='" & txtSWF.Text & "', TestPaper ='" & txtTpaper.Text & "', DECsFee ='" & txtDecs.Text & "', ActivityFee ='" & txtAfee.Text & "', CSATPACT ='" & txtCsat.Text & "', DevelopmentFee ='" & txtDfee.Text & "', TechnologyFee ='" & txtTfee.Text & "', JournalSRI ='" & txtJournal.Text & "', ComputerFee ='" & txtCompfee.Text & "', TeachersAide ='" & txtTaide.Text & "', Aircon ='" & txtAircon.Text & "', WholeYear ='" & txtWyear.Text & "', 2ndChild ='" & txt2nd.Text & "', & 3rdChild ='" & txt3rd.Text & "', 4thChild ='" & txt4th.Text & "', 5thChild ='" & txt5th.Text & "' WHERE YearGrade=" & dgvLevel.CurrentRow.Cells(0).Value


Is This A Good Question/Topic? 0
  • +

Replies To: Update syntax error

#2 nK0de  Icon User is offline

  • Catch me As Exception
  • member icon

Reputation: 204
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Re: Update syntax error

Posted 25 February 2012 - 12:37 AM

I think you've missed the ending double quotes.

YearGrade='" & dgvLevel.CurrentRow.Cells(0).Value "' "

This post has been edited by nK0de: 25 February 2012 - 12:38 AM

Was This Post Helpful? 0
  • +
  • -

#3 EtienneBordeaux  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 43
  • Joined: 09-February 12

Re: Update syntax error

Posted 25 February 2012 - 01:11 AM

Tried your resolve sir nK0de but no luck. There was an error in your code so I tried to use this one instead.

This code is still not working though.
YearGrade='" & dgvLevel.CurrentRow.Cells(0).Value & "'"

This post has been edited by EtienneBordeaux: 25 February 2012 - 01:19 AM

Was This Post Helpful? 0
  • +
  • -

#4 nK0de  Icon User is offline

  • Catch me As Exception
  • member icon

Reputation: 204
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Re: Update syntax error

Posted 25 February 2012 - 01:31 AM

my bad, I forgot the ampersand. sorry.

hmm...what's the error you're getting?
Was This Post Helpful? 0
  • +
  • -

#5 EtienneBordeaux  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 43
  • Joined: 09-February 12

Re: Update syntax error

Posted 25 February 2012 - 01:34 AM

syntax error in update statement
Was This Post Helpful? 0
  • +
  • -

#6 damdimdum  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 42
  • Joined: 20-February 12

Re: Update syntax error

Posted 25 February 2012 - 01:52 AM

YearGrade='" & dgvLevel.CurrentRow.Cells(0).Value & "'"


try change to like

YearGrade Like '" & dgvLevel.CurrentRow.Cells(0).Value & "%'"

Was This Post Helpful? 0
  • +
  • -

#7 nK0de  Icon User is offline

  • Catch me As Exception
  • member icon

Reputation: 204
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Re: Update syntax error

Posted 25 February 2012 - 01:56 AM

this right here. An wild ampersand in the front.

& 3rdChild ='" & txt3rd.Text & "'


cmd.CommandText = "UPDATE tblSOF SET Tuition ='" & txtTuition.Text & "', Registration ='" & txtReg.Text & "', Athletic ='" & txtAfee.Text & "', Guidance ='" & txtGuidance.Text & "', LearningMatAndKit ='" & txtMat.Text & "', ScienceLab ='" & txtScience.Text & "', CompLab ='" & txtComplab.Text & "', Library ='" & txtLib.Text & "', MedicalDental ='" & txtMed.Text & "', SHA ='" & txtSha.Text & "', SchoolPublication ='" & txtSpub.Text & "', IDFee ='" & txtIDfee.Text & "', SWF ='" & txtSWF.Text & "', TestPaper ='" & txtTpaper.Text & "', DECsFee ='" & txtDecs.Text & "', ActivityFee ='" & txtAfee.Text & "', CSATPACT ='" & txtCsat.Text & "', DevelopmentFee ='" & txtDfee.Text & "', TechnologyFee ='" & txtTfee.Text & "', JournalSRI ='" & txtJournal.Text & "', ComputerFee ='" & txtCompfee.Text & "', TeachersAide ='" & txtTaide.Text & "', Aircon ='" & txtAircon.Text & "', WholeYear ='" & txtWyear.Text & "', 2ndChild ='" & txt2nd.Text & "', & 3rdChild ='" & txt3rd.Text & "', 4thChild ='" & txt4th.Text & "', 5thChild ='" & txt5th.Text & "' WHERE YearGrade='" & dgvLevel.CurrentRow.Cells(0).Value & "' "

btw I suggest you use the datatype money for currency values in SQL Server. And other appropriate datatypes for numeric values. Using Text for everything is not right. You may run into problems in your program later on.

and damdimdum,

View Postdamdimdum, on 25 February 2012 - 02:22 PM, said:

YearGrade='" & dgvLevel.CurrentRow.Cells(0).Value & "'"


try change to like

YearGrade Like '" & dgvLevel.CurrentRow.Cells(0).Value & "%'"


you normally don't use the LIKE operator in updating for it can cause hazardous results. It'll update not only the exact record we want to update but every record which matches the condition!

This post has been edited by nK0de: 25 February 2012 - 02:07 AM

Was This Post Helpful? 1
  • +
  • -

#8 EtienneBordeaux  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 43
  • Joined: 09-February 12

Re: Update syntax error

Posted 25 February 2012 - 01:57 AM

Thanks sir nK0de. That saved my ass. Cheers! :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#9 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1746
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Update syntax error

Posted 25 February 2012 - 01:57 AM

Always the sql error messages say something like: "Syntax error near..." or so. Also please use parameterized query, with a lot of is benefits, it is also easy to debug and readable. This will help you easily see the error.

But again you have a debug capability in your IDE, use it. Put a break before the query and then during execution check what value is contained in CommandText, and look if anything is missing

This post has been edited by smohd: 25 February 2012 - 02:00 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1