need some help in my code please

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2005
Posts: 129
Reputation: some one is an unknown quantity at this point 
Solved Threads: 0
some one some one is offline Offline
Junior Poster

need some help in my code please

 
0
  #1
Jan 17th, 2008
hi every one i am trying to create shopping cart working with VB & access when i wrote add function an error occur i do not know how to solve"Syntax error in INSERT INTO statment" it please help me to solve the problem
a shot screen is attached contain page and exeption msg here is the function

  1. Protected Sub getproduct_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles getproduct.Click
  2. Dim x As String
  3. x = Session("user")
  4. Dim LongTimeString As String = ""
  5. Dim DateToString As String = ""
  6. Dim ShortTimeString As String = ""
  7. Dim LongDateString As String = ""
  8. Dim Daysinmonth As Integer = 0
  9. Dim ErrorStr As String = ""
  10. Dim ErrorStr1 As String = ""
  11. con = New OleDbConnection
  12. Dim cmd As OleDbCommand = New OleDbCommand
  13. Dim Add As String
  14. con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='C:\Documents and Settings\USER\Desktop\my\WebSite2\App_Data\EC.mdb';Persist Security Info=True"
  15. If con.State = Data.ConnectionState.Closed Then
  16. con.Open()
  17. End If
  18. Dim OrderDate = System.DateTime.Now
  19. LongTimeString = OrderDate.ToLongTimeString
  20. Daysinmonth = System.DateTime.DaysInMonth(OrderDate.Year, OrderDate.Month)
  21. DateToString = OrderDate.ToString
  22. ShortTimeString = OrderDate.ToShortTimeString
  23. LongDateString = OrderDate.ToLongDateString
  24. If (product.Text = "") Then
  25.  
  26. ErrorStr = ErrorStr & " Please Enter Product number, "
  27. End If
  28. If (Quantity.Text = "") Then
  29.  
  30. ErrorStr1 = ErrorStr1 & " Please Enter Product quantity, "
  31. End If
  32. Dim pri = "SELECT price FROM product WHERE ProductNO='" & product.Text & "'"
  33.  
  34. cmd.Connection = con
  35. cmd.CommandText = pri
  36. Dim s As String = cmd.ExecuteScalar()
  37. Add = "INSERT INTO order Values ('" & 1 & "','" & Quantity.Text & "','" & pri & "','" & Daysinmonth & "','" & x & "','" & product.Text & "');"
  38. cmd.CommandText = Add
  39. Try
  40. cmd.ExecuteNonQuery()
  41. Catch ex As Exception
  42. MsgBox(ex.Message)
  43. End Try
  44.  
  45.  
  46.  
  47.  
  48. End Sub
Last edited by some one; Jan 17th, 2008 at 11:36 am.
Attached Files
File Type: doc Doc2.doc (124.0 KB, 1 views)
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 129
Reputation: some one is an unknown quantity at this point 
Solved Threads: 0
some one some one is offline Offline
Junior Poster

Re: need some help in my code please

 
0
  #2
Jan 17th, 2008
I try to change in the statment but still the same error occour please help me

  1. While (i <= 100)
  2. i = (i + 1)
  3.  
  4.  
  5. Dim pri = "SELECT price FROM product WHERE ProductNO='" & product.Text & "'"
  6.  
  7. cmd.Connection = con
  8. cmd.CommandText = pri
  9. Dim s As String = cmd.ExecuteScalar()
  10. Dim a As String
  11. a = ("INSERT INTO order Values ( i And Quantity.Text And pri And Daysinmonth And x And product.Text );")
  12. cmd.CommandText = a
  13. End While
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: need some help in my code please

 
0
  #3
Jan 17th, 2008
These are your problems:
1. You cannot add an integer into an integer column with quotes.
2. You're inserting directly into the SQL statement. Use parameters.
  1. Add = "INSERT INTO order Values ('" & 1 & "','" & Quantity.Text & "','" & pri & "','" & Daysinmonth & "','" & x & "','" & product.Text & "');"
Do this insert statement, and please change your code to use parameters. It will protect you from SQL INJECTION. Cause of right now, I can easily type into any of these fields a drop command and your table will be dropped with all data. Anyway, here's your fixed insert statement.
  1. Add = "INSERT INTO order Values (1," & Quantity.Text & "," & pri & "," & Daysinmonth & "," & x & "," & product.Text & ");"
This assumes all your fields are for integers and not text. Text columns you wrap in your quotes (" '" & .. & "' "), all integer fields you don't (" " & .. & " ")
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 129
Reputation: some one is an unknown quantity at this point 
Solved Threads: 0
some one some one is offline Offline
Junior Poster

Re: need some help in my code please

 
0
  #4
Jan 17th, 2008
thank you but still the same msg appear
it does not work
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: need some help in my code please

 
0
  #5
Jan 17th, 2008
alright, well then there can be a few problems. Make sure all data types of your columns meet the correct ways of entering them (integer - no quotes in query :: text - quotes in query), then make sure you have the right number of columns in the query as you do on your database table. If you have 7 columns on your table and 6 or 8 in your query, you will get this error. Safest bet is to do an insert like this:
  1. INSERT INTO tblname (column1, column2, column3.....) VALUES (value1, value2, value3.....)
Those should be your only two problems. Have you attempted to use parameters instead of direct inserts? It will help reduce errors ten fold.
  1. Add = "INSERT INTO tblname (column1, column2, column3, ....) VALUES (?, ?, ?, ....)"
  2. cmd.Parameters.AddWithValue( "?ColumnName", value )
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 129
Reputation: some one is an unknown quantity at this point 
Solved Threads: 0
some one some one is offline Offline
Junior Poster

Re: need some help in my code please

 
0
  #6
Jan 17th, 2008
still did not work
how can I use the parameter if you do not mind can I attach the whole program our p[roject is about online order resturant
Attached Files
File Type: zip WebSite2.zip (686.9 KB, 2 views)
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 129
Reputation: some one is an unknown quantity at this point 
Solved Threads: 0
some one some one is offline Offline
Junior Poster

Re: need some help in my code please

 
0
  #7
Jan 17th, 2008
still did not work
how can I use the parameter if you do not mind can I attach the whole program
we are supposed to make an online order resturant
thanks
Attached Files
File Type: zip WebSite2.zip (686.9 KB, 0 views)
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: need some help in my code please

 
0
  #8
Jan 17th, 2008
I am actually at work so I can't use the files anyway.

I gave you an example on how to use the parameters. It's quite simple.
  1. Add = "INSERT INTO tblname (column1, column2, column3, ....) VALUES (?, ?, ?, ....)"
  2. cmd.Parameters.AddWithValue( "?Column1", value1 )
  3. cmd.Parameters.AddWithValue( "?Column2", value2 )
  4. cmd.Parameters.AddWithValue( "?Column3", value3 )
Do that for each parameter you have. THey must be in the same order as your SQL statement.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 129
Reputation: some one is an unknown quantity at this point 
Solved Threads: 0
some one some one is offline Offline
Junior Poster

Re: need some help in my code please

 
0
  #9
Jan 17th, 2008
thanks I will try if you can have a lock at the file after work or even tommorow i will be thankfull for you
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 129
Reputation: some one is an unknown quantity at this point 
Solved Threads: 0
some one some one is offline Offline
Junior Poster

Re: need some help in my code please

 
0
  #10
Jan 17th, 2008
I try it but still and another problem occur when using session I attach a shotscreen of it
thanks
Attached Files
File Type: doc Doc1.doc (151.0 KB, 2 views)
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the ASP.NET Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC