Create drop down list in asp.net comparing time field in sql?


I want to load a dropdown list in my web forms based on certain conditions, first here is my Table_columns(With types)

id--Int

food_type --varchar

First_start_time -- time

First_end_time -- time

second_start_time -- time

second_end_time -- time

 

Now I want to load values in which Current_Time > First_start_time

How to do this, it's basic question but I am not able to do it, so please let me know, thanks


Asked by:- RameshwarLate
1
: 2376 At:- 10/29/2017 10:57:21 AM
asp.net dropdown list based on condition compare-time-in-sql







1 Answers
profileImage Answered by:- bhanu

You can compare time field in sql using the code below

Select * from Table_name where TimeField_column > CONVERT(time, '11:37') //11:37 is some time

The above code compares time (11:37 in this case) with database column TimeField & returns the result based on that.

So your complete code can be

.aspx

    <form id="form1" runat="server">  
    <div>  
        <asp:DropDownList ID="DropDownList1" runat="server">  
        </asp:DropDownList>  
       
    </div>  
    </form>  

C# code behind assuming the table columns based on above description

  string str = "Data Source=.;uid=User_name;pwd=password;database=Database_name";  
    protected void Page_Load(object sender, EventArgs e)  
    {  
        SqlConnection con = new SqlConnection(str);  
        string time = DateTime.Now.ToString("HH:MM"); //to get time into 24 hours format
        string com = "Select * from FoodTable Where first_start_time < CONVERT(time, '" + time + "')";  
        SqlDataAdapter adpt = new SqlDataAdapter(com, con);  
        DataTable dt = new DataTable();  
        adpt.Fill(dt);  
        DropDownList1.DataSource = dt;  
        DropDownList1.DataBind();  
        DropDownList1.DataTextField = "food_type";  
        DropDownList1.DataValueField = "id";  
        DropDownList1.DataBind();  
    }  

That's it, you will get your desired results, remember i am using time format of 24 hours in the above code.

To get 12 hours time format 'time', use

string time = DateTime.Now.ToString("hh:mm"); //small hh:mm

hope this helps, please upvote & mark it as answer it helped you, thanks

1
At:- 10/29/2017 11:40:26 AM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use