Click here to Skip to main content
15,888,803 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
Hi
I am using dataetimepicker to save date.
But when i am save the date that time in datepicker date is system current date and in database 01/01/1900

This is the string of insert
VB
Query = "Insert Into TableBill (ID,TableNo,BillDate,ItemName,Quantity,UnitMl,DrinkAmount,FoodDet,FoodAmount,TotalBill) Values (" & txtid.Text & "," & txttableno.Text & "," & DateTimePicker1.value& ",'" & rw.Cells(0).Value & "','" & rw.Cells(1).Value & "','" & rw.Cells(2).Value & "','" & rw.Cells(3).Value & "','" & rw.Cells(4).Value & "','" & rw.Cells(5).Value & "'," & txtfinalbill.Text & ")"

What is the problem i am not getting please help me..
Thanks in advance..
Posted
Updated 30-Nov-13 22:11pm
v3
Comments
Sergey Alexandrovich Kryukov 1-Dec-13 0:34am    
Why are you asking what's the problem? You are the one who is supposed to tell us your problem.
(Maybe you try to work with strings instead of time-related types, like DATE, or are you just not familiar with such types or their formatting...)
—SA
basurajkumbhar 1-Dec-13 2:11am    
HiThanks for reply

my problem is when i am store the date in database that time it is store by default 01/01/1900
i want to store current system date that is displayed in the date time picker.
I want to store the date time picker value or date.
PIEBALDconsult 1-Dec-13 0:59am    
No way to know without seeing your code.
basurajkumbhar 1-Dec-13 2:12am    
Hi
Thanks for reply..

Please see the code..
Thanks.

The major problem is that you are concatenating strings to form your SQL command. Don't.
Firstly, because it leave you wide open to SQL Injection attacks which can damage or destroy your database.
Secondly because it leaves you wide open to problems like this.

When you concatenate strings, it builds a string value to pass to SQL. So, to cut down your code a little:
VB
Query = "Insert Into TableBill (TableNo,BillDate,ItemName) Values (txttableno.Text & "," & DateTimePicker1.value& ",'" & rw.Cells(0).Value & "')"
This does a bunch of ToString calls behind the scenes and you end up with the equivalent of:
VB
Query = "Insert Into TableBill (TableNo,BillDate,ItemName) Values (1234,1/12/2013,'The Item I Want')"
Which is passed to SQL for parsing. It looks at it, takes an integer value 1, divides it by 12 to give 0, which it then divides by 2013 to still give 0. It then converts the zero to a date and comes up with the wrong value. The same thing happens if your system is set to US: 12/1/2013 is still zero, and so forth.

Use parameterized queries. Not only do such problems not occur, but your database is safe from me typing commands to destroy it into your text boxes...
 
Share this answer
 
Query = "Insert Into TableBill (ID,TableNo,BillDate,ItemName,Quantity,UnitMl,DrinkAmount,FoodDet,FoodAmount,TotalBill) Values (" & txtid.Text & "," & txttableno.Text & ",'" & DateTimePicker1.Text & "','" & rw.Cells(0).Value & "','" & rw.Cells(1).Value & "','" & rw.Cells(2).Value & "','" & rw.Cells(3).Value & "','" & rw.Cells(4).Value & "','" & rw.Cells(5).Value & "'," & txtfinalbill.Text & ")"


Here I need to give the single quote.

like this..
'" DateTimePicker1.Text "'

or
'" & DateTimePicker1.Value & "'
 
Share this answer
 
v2
Comments
SoMad 1-Dec-13 3:25am    
Is this what solved your problem or are you asking a question?

Soren Madsen

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