Click here to Skip to main content
14,334,763 members
Rate this:
Please Sign up or sign in to vote.
See more:
I am using Vs2010 and Sql server 2005. I get date and time from user with seperated textbox. How to save it to sql server 2005 datetime column, or how to save it seperate columns date datatype and time datatype. Please help me....

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

DateTime dt = DateTime.Parse(myTextBox.Text);
using (SqlConnection con = new SqlConnection(strConnect))
    using (SqlCommand com = new SqlCommand("INSERT INTO myTable (myColumn) VALUES (@DATE)", con))
        com.Parameters.AddWithValue("@DATE", dt);
devausha 17-Nov-12 5:21am
I am getting date and time seperate textbox from user, So how to save it into sql server 2005.
OriginalGriff 17-Nov-12 5:32am
Concatenate the strings!
DateTime dt = DateTime.Parse(myTextBoxDate.Text + " " + myTextBoxTime.Text);
devausha 17-Nov-12 7:42am
Thank u It's Working
OriginalGriff 17-Nov-12 7:56am
You're welcome!
(But I would suggest you look at the DateTimePicker instead!)
OriginalGriff 17-Nov-12 5:33am
I wouldn't use a text box myself - have a look at the DateTimePicker instead, it's easier for the user to use, and provides a DateTime value directly. This means the user can't enter 34th Barktember 20012 or similar!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100