Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello ;

I'm trying to insert date value to Sql server DB by using vb.net .

here is the sql command
VB
"INSERT INTO [Orders]([ProductID],[CustomerID],[EmployeeID],[OrderDate],[Quantitiy],BillNuber) VALUES (" & ProductID & "," & CustomerID & "," & EmployeeID & "," & System.DateTime.Now.ToString((" yyyy-MM-dd")) & "," & Quantity & "," & billnumber & ")"


I also used Now.tostring("yyyy/MM/DD") , Today.tostring("yyyy/MM/DD")

the problem when i Use now() error message pops "error near '2'"
and even if there is no error and when i use today() or system.datetime , when i check the database the date look like "1900-01-01 00:00:00.000"

can you help me ?

What I have tried:

now()
today()
Now.tostring("yyyy/MM/DD")
Today.tostring("yyyy/MM/DD")
Posted
Updated 4-Sep-16 0:57am

1 solution

The generic date format which works regardless of the local for SQL Server is "yyyy-MM-dd" don't use / for a separator.

Also in string form add " quotes around the date.

Better still don't use string concatenation and use SQLParameters instead :
Using SQLParameters with VB.NET/C#[^]
sqlParameters Array in VB.Net - Stack Overflow[^]
 
Share this answer
 
Comments
Member 12701700 4-Sep-16 6:58am    
Thanks man . It worked . Regarding the sql parameter , I used it but it did not work could you refer me to an easy example .
B. REGARDS
Maciej Los 5-Sep-16 4:08am    
5ed!
Mehdi Gholam 5-Sep-16 4:11am    
Thanks Maciej!

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