Showing posts with label selecteddate. Show all posts
Showing posts with label selecteddate. Show all posts

Wednesday, March 21, 2012

datetime to dd/MM/yyyy for inserting into DB

hi there, i have a calendar that i put into a string ilke this

string str = Calendar1.SelectedDate.ToShortDateString();

the result is dd/mm/yyyy date which is great, but for inserting into my DB (MSSQL) it needs to be a datetime field, however when i convert it

Datetime dtDate = Convert.ToDateTime(str);

it takes my date and adds 00:00:00 onto the end and this is not what i want! i just want the dd/mm/yyy how do i do this, it has to be simple but i have been searching for hours and cant find anything, i am using ASP.NET 2 and C#

Thanks

The datetime object MUST contain the time. You don't have any problem, if you display only the date, you are OK.Smile

|||

ah rite ok, but when i try to get the date back from the db where its stored as e.g 15/06/2007 the datetime string passes it as 15/06/2007 00:00:00 and so it wont reconise it, is there any other way to pass it so that it can compare?

|||

Hi,

Actually, when recieve the datetime as string from the database, you may convert it to DateTime type and use ToShortDataString method, in this way,you can get the datetime string without time part. See the sample below:

string dt_s = ds.Tables[0].Rows[0][0].ToString();// Get the datetime from db. DateTime dt = Convert.ToDateTime(dt_s);// Convert it to DateTime type.this.Label1.Text = dt.ToShortDateString();// Get the short date
Thanks.

Wednesday, March 7, 2012

datetime

I use Calendar asp.net(c#).It gives me Calendar.SelectedDate.Date is string
I convert it to datetime with Convert.toDateTime but Sql server give me
error...
The conversion of a char data type to a datetime data type resulted in an
out-of-range datetime value. The statement has been terminated.
How can I solve the error and add date to sql server?
Thanks...You're not using a proper datetime format for SQL Server:
http://www.karaszi.com/sqlserver/info_datetime.asp
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
"Selen" <skiyanc@.yahoo.com> wrote in message
news:OLR5VrmCEHA.2656@.TK2MSFTNGP12.phx.gbl...
> I use Calendar asp.net(c#).It gives me Calendar.SelectedDate.Date is
string
> I convert it to datetime with Convert.toDateTime but Sql server give me
> error...
> The conversion of a char data type to a datetime data type resulted in an
> out-of-range datetime value. The statement has been terminated.
> How can I solve the error and add date to sql server?
> Thanks...
>|||two options to go forwards
1. Use a neutral date format. These are the ISO and ISO 8601 standard. These
have the CONVERT sytle numbers of 112 and 126 respectively. Check the SQL S
erver documentation on the CONVERT function for details.
2. Change the language of the SQL Server login used to match the string that
is being sent down.