Click here to Skip to main content
12,698,476 members (23,677 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# ASP.NET SQL Server
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 16-Nov-12 23:51pm
devausha1.5K

1 solution

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

Solution 1

Easy:
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();
        }
    }
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.170118.1 | Last Updated 17 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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