Click here to Skip to main content
15,894,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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....
Posted

1 solution

Easy:
C#
DateTime dt = DateTime.Parse(myTextBox.Text);
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand com = new SqlCommand("INSERT INTO myTable (myColumn) VALUES (@DATE)", con))
        {
        com.Parameters.AddWithValue("@DATE", dt);
        com.ExecuteNonQuery();
        }
    }
 
Share this answer
 
Comments
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900