Forum guidelines
AuthorQuestion
DateTimePicker - How to save date into the DB?
Posted on: 30 May 2008

Dear Friends,

I need to save date format which displays in the dateTimePicker1 in the  window form. The date format in the dateTimePicker1 is like this "Friday, May 30, 2008".

But when i'm trying save this format into the database there is an error displaying saying that, nvarchar datatype cannot covert into datatime.

// the given below cade generate the error...

enterDate = dateTimePicker1.Text; //enterDate  is the variable name that I have defined

Could you please help me to solve this problem?

Thanks in advance.


Dinusha
AuthorReply
Re: DateTimePicker - How to save date into the DB?
Posted on: 30 May 2008  
Hi!

1. What data type is the variable enterDate?
2. How do you save it in your database? Is the db column a date datatype or a text data type? Please post your code snippet.

if your db column is of date datatype, you should use dateTimePicker1.Value instead of dataTimePicker1.Text. dateTimePicker1.Value returns a DateTime datatype.

In the error you encountered, it looks like you are inserting a non datetime data type into a data type column

Re: DateTimePicker - How to save date into the DB?
Posted on: 30 May 2008  

Thanks Jan for your reply..

string enterDate

enterDate = dateTimePicker1.Value

//the database is SQL, and the field name is 'en_date' in the table and data type is datetime.

I jz want to save date and retrive that date when the user wants to display date.


Dinusha
Re: DateTimePicker - How to save date into the DB?
Posted on: 30 May 2008  

A less propper way is if you did input a string into the DB datetime field, would be to convert it on the DB side.

table(date datetime)

insert table (convert(datetime, '5/30/2008'))

stick with passing the datetime 'value' if possible.

edit: you can't convert 'Friday, May 30, 2008' to datetime because of the 'Friday,' on the front. SQL is expecting any normal datetime formats.


Re: DateTimePicker - How to save date into the DB?
Posted on: 03 Jun 2008  
The lines:

string enterDate;
enterDate = dateTimePicker1.Value;


will not even allow you to compile. It's because dateTimePicker1.Value will return a datetime datatype and it will give you the error "Cannot implicitly convert type 'System.DateTime' to 'string' because you are assigning it to a string type (enterDate).

you have two options two store the value of dateTimePicker1 in a variable.

1. store it as string type.
    string date;
    date = dateTimePicker1.Value.ToString("yyyy/MM/dd");


2. store it as datetime type.
    DateTime date;
    date = dateTimePicker1.Value;

There are various ways to save and retrieve this in a database. Have you already tried saving values in a database? I was expecting to see the code snippet you use in accessing the database.

If you don't know how, have a research on how to use ADO.net. It's what we use when accessing MSSQL database.

Here's to get you started. ADO.Net

Re: DateTimePicker - How to save date into the DB?
Posted on: 23 Dec 2008  
Thanks. It's a helpful post.

shomel

SPONSORED BY

Custom Software Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.