Click here to Skip to main content
15,846,038 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i inserted date but don't know how to insert time.. here's my code.
Please Help.
protected void click_insert(object sender, EventArgs e)
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["carrental"].ConnectionString);
    SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "insert into car_insert (city,loc,c_type,name,email,mob,date) values(@city,@loc,@c_type,@name,@email,@mob,@date)";

        cmd.Parameters.AddWithValue("@city", DropDownList1.SelectedItem.Text.ToString());
        cmd.Parameters.AddWithValue("@loc", DropDownList2.SelectedItem.Text.ToString());
        cmd.Parameters.AddWithValue("@c_type", DropDownList11.SelectedItem.Text.ToString());
        cmd.Parameters.AddWithValue("@name", TextBox1.Text.ToString());
        cmd.Parameters.AddWithValue("@email", TextBox2.Text.ToString());
        cmd.Parameters.AddWithValue("@mob", TextBox3.Text.ToString());
        cmd.Parameters.AddWithValue("@date", Calendar1.SelectedDate.ToString("yyyy.MM.dd"));
    catch (Exception ex)
    { Response.Write(ex.Message); }
Updated 11-Jun-14 18:21pm
Where you want to insert the time? You have not written any code for that.
PIEBALDconsult 11-Jun-14 23:40pm    
0) I hope you are storing as DATETIME and not as text.
1) If so, then don't use .ToString("yyyy.MM.dd") !
2) You may also want to move the Close to a finally.
3) And showing the actual error message to the user is generally a bad idea.
Debanjan Mondal 11-Jun-14 23:43pm    
ya, i know that.
when i inserted date on date field,it already consist of time.Like 6/19/2014 12:00:00 AM. so is it possible to enter time in this field?Or, i need another column for time?
i already have dropdownlist for time.can it be possible to add those selected time into dtae field?

1 solution

you can get only the date from ASP.NET Calendar control. you need to find jQuery or other extended Calendar control which support both Date and Time, Check below Article for how to use jQuery DateTime picker and fetch selected datetime from code behind.
DateTimePicker control for ASP.Net TextBox Example[^]
Then you can insert selected DateTime value to a DateTime or DateTime2 column in your SQl server Database like below

DateTime dob = DateTime.Parse(Request.Form[TextBox1.UniqueID]);
cmd.Parameters.AddWithValue("@dob", dob);
Share this answer
Debanjan Mondal 11-Jun-14 23:44pm    
thanks sir...
DamithSL 12-Jun-14 0:02am    
you don't need two columns for Date and Time, use one DateTime type column and insert DateTime object value.
Debanjan Mondal 12-Jun-14 12:04pm    
how to do that?

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