I want to implement a repeater to get data from the database in descending order.
How do I do?
I want to implement a repeater to get data from the database in descending order.
How do I do?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Vishal JoshiRank #377 · 6.7K reputationPosted Apr 16, 2024, 6:49 AM EDT
Hello
Please check out the below sample code for repeater control in asp.net.
aspx code
aspx.cs code
public partial class _Default : System.Web.UI.Page { SqlConnection con; SqlCommand cmd = new SqlCommand(); protected void Page_Load(object sender, EventArgs e) { con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString); cmd.Connection = con; cmd.CommandText = "select * from student"; con.Open(); RepeatInformation.DataSource = cmd.ExecuteReader(); RepeatInformation.DataBind(); con.Close(); } }Thanks
Vishal Joshi
Jayraj ChhayaRank #304 · 8.2K reputationPosted Apr 16, 2024, 5:45 AM EDT
To display data in descending order using a repeater in a .NET application, you can sort the data before binding it to the repeater control. Here's a simple example using C#:
// Assuming you have a DataTable named 'dataTable' with the data from the database // Sort the DataTable in descending order based on a specific column dataTable.DefaultView.Sort = "ColumnName DESC"; DataTable sortedData = dataTable.DefaultView.ToTable(); // Bind the sorted data to the repeater control repeater.DataSource = sortedData; repeater.DataBind();replace 'ColumnName' with the actual column name you want to sort the data by in descending order. This approach ensures that the data displayed by the repeater is in the desired descending order.
Vijay Pratap SinghRank #381 · 6.6K reputationPosted Apr 16, 2024, 5:51 AM EDT
// Assuming you have a SQL Server database using System; using System.Data.SqlClient; using System.Web.UI.WebControls; public partial class YourPage : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { // Connection string to your database string connectionString = "YourConnectionString"; // SQL query to retrieve data in descending order string query = "SELECT * FROM YourTable ORDER BY YourColumn DESC"; // Create a connection using (SqlConnection connection = new SqlConnection(connectionString)) { // Open the connection connection.Open(); // Create a command with the query and connection using (SqlCommand command = new SqlCommand(query, connection)) { // Execute the command and get a data reader using (SqlDataReader reader = command.ExecuteReader()) { // Bind the data reader to the Repeater control repeater.DataSource = reader; repeater.DataBind(); } } } } } }Now bind data to the repeater
'>Hope this will help.
Thanks