Click here to Skip to main content
15,889,931 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
Dim cb As String = "insert into Product(ProductID,ProductName,ProductType,Category,Specification,Date3,UnitPrice) VALUES (@d1,@d2,@d3,@d4,@d5,'""#" & StockDate.Text & "#""',@d7)"


                cmd = New OleDbCommand(cb)

                cmd.Connection = con


                cmd.Parameters.Add(New OleDbParameter("@d1", System.Data.OleDb.OleDbType.VarChar, 20, "ProductID"))
                cmd.Parameters.Add(New OleDbParameter("@d2", System.Data.OleDb.OleDbType.VarChar, 250, "ProductName"))
                cmd.Parameters.Add(New OleDbParameter("@d3", System.Data.OleDb.OleDbType.VarChar, 250, "ProductType"))
                cmd.Parameters.Add(New OleDbParameter("@d4", System.Data.OleDb.OleDbType.VarChar, 150, "Category"))
                cmd.Parameters.Add(New OleDbParameter("@d5", System.Data.OleDb.OleDbType.VarChar, 250, "Specification"))
                cmd.Parameters.Add(New OleDbParameter("@d6", System.Data.OleDb.OleDbType.Date, "StockDate"))
                cmd.Parameters.Add(New OleDbParameter("@d7", System.Data.OleDb.OleDbType.Double, 10, "UnitPrice"))

                


                cmd.Parameters("@d1").Value = ProductID.Text
                cmd.Parameters("@d2").Value = ProductName1.Text
                cmd.Parameters("@d3").Value = ProductType1.Text
                cmd.Parameters("@d4").Value = Category.Text
                cmd.Parameters("@d5").Value = Specification.Text
                md.Parameters("@d6").Value = StockDate.Text
                cmd.Parameters("@d7").Value = CDbl(UnitPrice.Text)
Posted
Updated 9-Apr-15 1:53am
v2
Comments
CHill60 9-Apr-15 8:01am    
You haven't defined the maximum size in cmd.Parameters.Add(New OleDbParameter("@d6", System.Data.OleDb.OleDbType.Date, "StockDate"))
Richard MacCutchan 9-Apr-15 8:03am    
You have declared it as a Date type, but then set the value as text.

Probably because you are trying to put a string value: '#your date#' into a date field.

So why - if you know what parametrized queries are, and you clearly do - are you directly creating a problem ***and*** leaving your DB open to SQL Injection attacks but not using a parameterized query for the date column as well?
Parse your user input into a DateTime value, and pass that as a parameter - your problem will go away, and you will be protected...
 
Share this answer
 
Comments
Richard Deeming 9-Apr-15 8:05am    
Snap!
Why have you used string concatenation to pass one parameter? You clearly know how to use a parameterized query, since all of the other parameters are passed correctly, but you've chosen to use string concatenation for the StockDate.Text, leaving your code vulnerable to SQL Injection[^].

Fix the vulnerability, and you will fix your error:
VB.NET
Dim cb As String = "insert into Product(ProductID,ProductName,ProductType,Category,Specification,Date3,UnitPrice) VALUES (@d1,@d2,@d3,@d4,@d5,@d6,@d7)"
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900