Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a web form that has a dropdownlist with hh:mm tt with 30 minutes interval, starting from 6:00am, 6:30 am, 7:00am....8:00pm. How can I save selected time to MS Sql. I am not sure what datatype (Date, DateTime, Time(7) in Microsoft SQL I should use. Please help me advise. This is my c# code to define hh:mm tt and load on the form.

C#
private void AddDropDownValues(ref DropDownList ddlHrMin, int startHour, int endHour, int increment)
   {
       DateTime now = DateTime.Now;
       DateTime startTime = new DateTime(now.Year, now.Month, now.Day, startHour, 0, 0);
       DateTime endTime = new DateTime(now.Year, now.Month, now.Day, endHour, 0, 0);

       while (startTime <= endTime)
       {
           ddlHrMin.Items.Add(startTime.ToShortTimeString());
           startTime = startTime.AddMinutes(increment);
       }
   }


C#
protected void Page_Load(object sender, EventArgs e)
  {

      AddDropDownValues(ref ddlHrMin, 6, 20, 30);

  }
Posted
Comments
Herman<T>.Instance 12-Aug-14 4:03am    
did you use DateTime.TryParse ?

You can use Time data type to store type. For Testing use below query

SQL
USE Master

CREATE table #t1(TT time)
Insert INTO #t1 VALUES(getdate())
SELECT * FROM #t1
Drop table #t1
 
Share this answer
 
Quote:
You can use Time data type to store type
 
Share this answer
 

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