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 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...
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...
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...
I need a few unit tests done on my SelectionSort program in Visual Studio 2019 with...
I need a few unit tests done on my SelectionSort program in Visual Studio 2019 with MSTest. My program is below. I just need screen shots of the unit test code in Visual Studio 2019 with MSTest. Please and thank you. so easentially I need someone to write some unit tests with my program in MSTest visual studio 2019. Then I need the unit tests supplied as answers. using System; namespace SelectionSortAlgorithm { public class SelectionSort { public static void...
I need the code for following in C++ working for Visual studio please. Thanks Use a...
I need the code for following in C++ working for Visual studio please. Thanks Use a Struct to create a structure for a Player. The Player will have the following data that it needs maintain: Struct Player int health int level string playerName double gameComplete bool isGodMode Create the 2 functions that will do the following: 1) initialize(string aPlayerName) which takes in a playername string and creates a Player struct health= 100 level= 1 playerName = aPlayerName gameComplete = 0...
C# Yahtzee Program I am creating a Yahtzee program where you have the option to choose...
C# Yahtzee Program I am creating a Yahtzee program where you have the option to choose 5 or more dice that will be rolled. I just need help trying to create a random dice roll and seeding it. Could you give me some code examples of getting a dice roll of a six sided die that won't display similar results.
In visual Studio C++ Create a program that uses a for loop to input the high...
In visual Studio C++ Create a program that uses a for loop to input the high temperature, and low temperature for each day of the week. The high and low will be placed into two elements of the array. For each loop the high and low will be placed into the next set of elements of the array. After the temps for all seven days have been entered into the array, a for loop will be used to pull out...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT