Question

In: Computer Science

C# ( asp.net ) 2019 visual studio I have a dropdown option. If I choose "date"...

C# ( asp.net ) 2019 visual studio

I have a dropdown option. If I choose "date" option, I get to enter list of dates like 02/08/1990, 06/14/1890 in (mm/dd/YYYY) format.

How can I sort these dates in ascending order?. Can you provide me some code. Thank you

Solutions

Expert Solution

There are so many ways to sort the date field in the dropdown.

Method 1:
---------

you can simply write the query using "ORDER BY" in your code it self. 

select * from [table name] order by convert(nvarchar,DateValidFrom,103) 

Method 2 :

-------------

For this syntax first you need to add the namespace otherwise you will face an issue.Which is,

[add namespace using System.Linq;]

Syntax: var orderedDateList = date.OrderBy(x => DateTime.Parse(x)).ToList();

Here 'var' can accept any type of variable.

Method 3:

--------------

Here i am just providing an example for understanding.

<%@ Page Language="C#" AutoEventWireup="true"%>

<!DOCTYPE html>

<script runat="server">

protected void Button1_Click(object sender, System.EventArgs e)

{

List<DateTime> holidays = new List<DateTime>();

DateTime today = DateTime.Today; holidays.Add(today);

holidays.Add(today.AddDays(5));

holidays.Add(today.AddDays(1));

holidays.Add(today.AddDays(7));

holidays.Add(today.AddDays(3));

Label1.Text = "holidays.....<br /> ";

foreach (DateTime d in holidays)

{

Label1.Text += d.ToShortDateString() + "<br />";

}

var sortedholidays = holidays.OrderBy(x => x);

Label1.Text += "<br />ascending sorted holidays.........<br />";

foreach (DateTime d in sortedholidays)

{

Label1.Text += d.ToShortDateString() + "<br />";

}

}

</script>

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

<title>c# linq example - sort date List order by ascending descending</title>

</head>

<body>

<form id="form1" runat="server">

<div>

<h2 style="color:DarkBlue; font-style:italic;"> c# example - sort date List order <br /> by ascending and descending </h2> <hr width="550" align="left" color="LightBlue" />

<asp:Label ID="Label1" runat="server" Font-Size="Large" >

</asp:Label> <br />

<asp:Button ID="Button1" runat="server" Text="sort date List" OnClick="Button1_Click" Height="40" Font-Bold="true" /> </div>

</form>

</body>

</html>

Method 1 is very easy process to solve your problem. As i do not know your exact code here i am providing a simple example.


Related Solutions

C# ( asp.net ) 2019 Visual Studio I have a dropdown where you can select (...
C# ( asp.net ) 2019 Visual Studio I have a dropdown where you can select ( integer, string, date ) After selecting the desired dropdown option, user can input a list of inputs. For example; for integer: 2,5,7,9,1,3,4 And then , I have a 'sort' button Can you please help me with the code behind for sorting them( For integer, string, and date ) Thank you.
Create an ASP.Net Website using Visual Studio with C#: Create a simple calculator that has 3...
Create an ASP.Net Website using Visual Studio with C#: Create a simple calculator that has 3 text boxes: 2 of them to enter numbers, the 3rd one displays the results Create 4 buttons to add, subtract, multiply, and divide Prevent the user from entering text in the number fields Display a message indicating “cannot divide by” when the user click “/” and there is a zero the in the second box Create two additional buttons: - One to store data...
Create a new website using C# & ASP.Net in Visual Studio: 1. Create a web page...
Create a new website using C# & ASP.Net in Visual Studio: 1. Create a web page to access a database and display the data from a table by providing an SQL statement. Create the page with these requirements:     create label, textbox and button     the textbox will capture the SQL statement     the button will process the statement and display the results 2. Add necessary data access pages to your project. 3. Display the data in Gridview
Create an ASP.Net Website using Visual Studio with Visual Basic.Net: Create a simple calculator that has...
Create an ASP.Net Website using Visual Studio with Visual Basic.Net: Create a simple calculator that has 3 text boxes: 2 of them to enter numbers, the 3rd one displays the results Create 4 buttons to add, subtract, multiply, and divide Prevent the user from entering text in the number fields Display a message indicating “cannot divide by” when the user click “/” and there is a zero the in the second box Create two additional buttons: - One to store...
USING VISUAL STUDIO 2017, LANGUAGE VISUAL C# I have struggled on this program for quite some...
USING VISUAL STUDIO 2017, LANGUAGE VISUAL C# I have struggled on this program for quite some time and still can't quite figure it out. I'm creating an app that has 2 textboxes, 1 for inputting customer name, and the second for entering the number of tickets the customer wants to purchase. There are 3 listboxes, the first with the days of the week, the second with 4 different theaters, and the third listbox is to display the customer name, number...
Subject- ( App Development for Web) ( language C#, software -visual studio) I have created a...
Subject- ( App Development for Web) ( language C#, software -visual studio) I have created a 'Calculator' named program in visual studio. This is the main console program(Program.cs) shown below. Then i have created a "CaculatorLibrary" named project by adding the project from FILE--->ADD--->NEW PROJECT. Then i selected library and selected classLibrary(.NETcore). The programs for both are given below. There are no errors in the program. Now the requirement is to build unit test project within this project that will...
ONLY USE VISUAL STUDIO (NO JAVA CODING) VISUAL STUDIO -> C# -> CONSOLE APPLICATION In this...
ONLY USE VISUAL STUDIO (NO JAVA CODING) VISUAL STUDIO -> C# -> CONSOLE APPLICATION In this part of the assignment, you are required to create a C# Console Application project. The project name should be A3<FirstName><LastName>P2. For example, a student with first name John and Last name Smith would name the project A1JohnSmithP2. Write a C# (console) program to calculate the number of shipping labels that can be printed on a sheet of paper. This program will use a menu...
C++ Visual Studio 2019 Part A : Program Description: Write a program to generate a report...
C++ Visual Studio 2019 Part A : Program Description: Write a program to generate a report based on input received from a text file. Suppose the input text file student_grades.txt contains the student’s Last name , First name, SSN, Test1, Test2, Test3 and Test4. (25%) i.e. Alfalfa   Aloysius   123-45-6789 40.0    90.0   100.0    83.0 Generate the output Report File student_final.txt in the following format : LastName FirstName   SSN Test1   Test2   Test3   Test4 Average FinalGrade i.e. Alfalfa   Aloysius   123-45-6789 40.0    90.0   100.0   ...
Create a C++ project in visual studio. You can use the C++ project that I uploaded...
Create a C++ project in visual studio. You can use the C++ project that I uploaded to complete this project. 1. Write a function that will accept two integer matrices A and B by reference parameters, and two integers i and j as a value parameter. The function will return an integer m, which is the (i,j)-th coefficient of matrix denoted by A*B (multiplication of A and B). For example, if M = A*B, the function will return m, which...
Write a Visual C# project that will allow the user an option to choose from four...
Write a Visual C# project that will allow the user an option to choose from four different countries. When a country is chosen, the program will display the country flag and information that the user wishes to see. The name of the country selected will be displayed in a label under the country's flag image. The user may also choose to display or hide the form's title, the country name, and the name of the programmer/developer.   Check boxes will be...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT