| | |
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:
Solved Threads: 0
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
a shot screen is attached contain page and exeption msg here is the function
ASP.NET Syntax (Toggle Plain Text)
Protected Sub getproduct_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles getproduct.Click Dim x As String x = Session("user") Dim LongTimeString As String = "" Dim DateToString As String = "" Dim ShortTimeString As String = "" Dim LongDateString As String = "" Dim Daysinmonth As Integer = 0 Dim ErrorStr As String = "" Dim ErrorStr1 As String = "" con = New OleDbConnection Dim cmd As OleDbCommand = New OleDbCommand Dim Add As String 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" If con.State = Data.ConnectionState.Closed Then con.Open() End If Dim OrderDate = System.DateTime.Now LongTimeString = OrderDate.ToLongTimeString Daysinmonth = System.DateTime.DaysInMonth(OrderDate.Year, OrderDate.Month) DateToString = OrderDate.ToString ShortTimeString = OrderDate.ToShortTimeString LongDateString = OrderDate.ToLongDateString If (product.Text = "") Then ErrorStr = ErrorStr & " Please Enter Product number, " End If If (Quantity.Text = "") Then ErrorStr1 = ErrorStr1 & " Please Enter Product quantity, " End If Dim pri = "SELECT price FROM product WHERE ProductNO='" & product.Text & "'" cmd.Connection = con cmd.CommandText = pri Dim s As String = cmd.ExecuteScalar() Add = "INSERT INTO order Values ('" & 1 & "','" & Quantity.Text & "','" & pri & "','" & Daysinmonth & "','" & x & "','" & product.Text & "');" cmd.CommandText = Add Try cmd.ExecuteNonQuery() Catch ex As Exception MsgBox(ex.Message) End Try End Sub
Last edited by some one; Jan 17th, 2008 at 11:36 am.
•
•
Join Date: Apr 2005
Posts: 129
Reputation:
Solved Threads: 0
I try to change in the statment but still the same error occour please help me
ASP.NET Syntax (Toggle Plain Text)
While (i <= 100) i = (i + 1) Dim pri = "SELECT price FROM product WHERE ProductNO='" & product.Text & "'" cmd.Connection = con cmd.CommandText = pri Dim s As String = cmd.ExecuteScalar() Dim a As String a = ("INSERT INTO order Values ( i And Quantity.Text And pri And Daysinmonth And x And product.Text );") cmd.CommandText = a End While
•
•
Join Date: Sep 2007
Posts: 1,080
Reputation:
Solved Threads: 68
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.
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.
This assumes all your fields are for integers and not text. Text columns you wrap in your quotes (" '" & .. & "' "), all integer fields you don't (" " & .. & " ")
1. You cannot add an integer into an integer column with quotes.
2. You're inserting directly into the SQL statement. Use parameters.
ASP.NET Syntax (Toggle Plain Text)
Add = "INSERT INTO order Values ('" & 1 & "','" & Quantity.Text & "','" & pri & "','" & Daysinmonth & "','" & x & "','" & product.Text & "');"
ASP.NET Syntax (Toggle Plain Text)
Add = "INSERT INTO order Values (1," & Quantity.Text & "," & pri & "," & Daysinmonth & "," & x & "," & product.Text & ");"
•
•
Join Date: Sep 2007
Posts: 1,080
Reputation:
Solved Threads: 68
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:
Those should be your only two problems. Have you attempted to use parameters instead of direct inserts? It will help reduce errors ten fold.
ASP.NET Syntax (Toggle Plain Text)
INSERT INTO tblname (column1, column2, column3.....) VALUES (value1, value2, value3.....)
ASP.NET Syntax (Toggle Plain Text)
Add = "INSERT INTO tblname (column1, column2, column3, ....) VALUES (?, ?, ?, ....)" cmd.Parameters.AddWithValue( "?ColumnName", value )
•
•
Join Date: Sep 2007
Posts: 1,080
Reputation:
Solved Threads: 68
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.
Do that for each parameter you have. THey must be in the same order as your SQL statement.
I gave you an example on how to use the parameters. It's quite simple.
ASP.NET Syntax (Toggle Plain Text)
Add = "INSERT INTO tblname (column1, column2, column3, ....) VALUES (?, ?, ?, ....)" cmd.Parameters.AddWithValue( "?Column1", value1 ) cmd.Parameters.AddWithValue( "?Column2", value2 ) cmd.Parameters.AddWithValue( "?Column3", value3 )
![]() |
Similar Threads
- Code 19 Registry Error (Windows NT / 2000 / XP)
- Why won't this code work? (VB.NET)
- Need help with DirectX code (C)
- Tutorials & Code Submissions - Questions? (DaniWeb Community Feedback)
- Some Basic Code Hopefully (Help Needed) (HTML and CSS)
Other Threads in the ASP.NET Forum
- Previous Thread: What is Talkswitch?
- Next Thread: I am struk up with this problem
| Thread Tools | Search this Thread |
.net 2.0 3.5 activexcontrol advice ajax appliances asp asp.net bc30451 beginner bottomasp.net browser businesslogiclayer button c# c#gridviewcolumn cac checkbox class click commonfunctions compatible confirmationcodegeneration content contenttype control countryselector courier css database datagrid datagridview datagridviewcheckbox datalist deadlock deployment development dgv dialog dropdownmenu dynamic edit embeddingactivexcontrol expose findcontrol flash flv formatdecimal forms formview gridview homeedition iframe iis javascript jquery list menu mono mssql multistepregistration nameisnotdeclared novell objects order problem ratings rotatepage save search security serializesmo.table sessionvariables silverlight smartcard sql sqlserver2005 ssl suse textbox tracking treeview unauthorized validatedate validation vb.net video virtualdirectory vista visual-studio visualstudio vs2008 web webarchitecture webdevelopemnt webdevelopment wizard xml youareanotmemberofthedebuggerusers






