Click here to Skip to main content
15,890,336 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am Developing Window Based Application Through vb.net but Each Time i am saving the date in access ms db,it is saving the date like 01-07-1894 00:00:00 type even When I am saving 12-03-2017 from Date Time Picker...
I tried selecting different dates and each time i am getting the date been stored in db like "**-**-1894"
I redesigned form controls,reinstalled software's still i am unlucky..
Please somebody Help Me...!!
Thanks in Advance..........!!

What I have tried:

Try
Dim ds As New DataSet()
cn = New OleDbConnection(str)
cn.Open()
cmd = New OleDbCommand("select * from cus_reg_table", cn)
Dim da As New OleDbDataAdapter(cmd)
cmd = New OleDbCommand("insert into cus_reg_table(cid,cdate,cname,caddr,cmob) values(" + cnumLabel.Text + "," + DateTimePicker1.Text + ",'" + cnameTextBox.Text + "','" + addressTextBox.Text + "'," + cmobnoTextBox.Text + ") ", cn)
cmd.ExecuteNonQuery()
MsgBox("Record added successfully.....")
cn.Close()
Catch ex As Exception
MsgBox(ex.Message.ToString)
End Try
Posted
Updated 11-Mar-17 19:51pm
v3
Comments
Bryian Tan 12-Mar-17 0:27am    
missing a single quote?

",'" + DateTimePicker1.Text + "','"
Member 12996616 12-Mar-17 0:31am    
but what if i use a single quote then it will store my date as string no..???
Later i dont want problems while retrieving them and comparing dates...
Member 12996616 12-Mar-17 0:36am    
as "cdate" datatype i am using in access db is "Date/Time"..
I think if i use '"+DateTimePicker1.text+"' or '"+DateTimePicker.Value.Date+"' then it will store my date as string/Text and not as a Date itself..but i am not sure with my thinking...
Bryian Tan 12-Mar-17 0:58am    
Maybe # instead of '. Think of it this way, If you going to write a query from the Access database to insert the data , how the query will look like?

1 solution

Stop doing it like that: never concatenate strings to form an SQL command as it both causes problems like this, and leaves you wide open to SQL Injection, which can damage or destroy your database. Always use Parameterised queries.

Change your code:
VB
cmd = New OleDbCommand("INSERT INTO cus_reg_table (cid,cdate,cname,caddr,cmob) VALUES (@CID, @CDT, @CNM, @CAD, @CMO)"
cmd.Parameters.AddWithValue("CID", cnumLabel.Text)
cmd.Parameters.AddWithValue("CDT", DateTimePicker1.Value)
cmd.Parameters.AddWithValue("CNM", cnameTextBox.Text)
cmd.Parameters.AddWithValue("CAD", addressTextBox.Text)
cmd.Parameters.AddWithValue("CMO", cmobnoTextBox.Text)
cmd.ExecuteNonQuery()


The chances are that will fix your problem at the same time.
 
Share this answer
 
Comments
Maciej Los 12-Mar-17 15:39pm    
5ed!

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