I tried to display my record in gridview to show from most recent dates but I keep seeing records of May 3, 2023 at the top of the gridview, then December 9, 2023 is below.
I want my GridView to start from the most recent dates to the earlier date in the gridview
I am using Stored procedure to fetch the data from table
CREATE PROCEDURE [dbo].[MyDataViewTable]
@CreatedBy VARCHAR(50)
,@PageIndex INT
,@PageSize INT
,@RecordCount INT OUT
AS
BEGIN
SELECT ROW_NUMBER() OVER(ORDER BY CreatedBy) RowNumber
,Id
,Receipt_no
,email
,CreatedBy
,NameUser
,CreatedDate
INTO #Temp
FROM TemplateTable
WHERE CreatedBy = @CreatedBy ORDER BY CreatedDate DESC
SELECT @RecordCount = COUNT(*) FROM #Temp
SELECT * FROM #Temp
WHERE (RowNumber BETWEEN ((@PageIndex-1) * @PageSize) + 1 AND (@PageIndex * @PageSize)) OR @PageIndex = - 1
DROP TABLE #Temp
END
I thought that by using this line will solve the issue
WHERE CreatedBy = @CreatedBy ORDER BY CreatedDate DESC
Here is my C#
private void GetRecordsPage(int pageIndex)
{
try
{
using (SqlConnection con = new SqlConnection())
{
con.ConnectionString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
using (SqlCommand cmd = new SqlCommand("MyDataViewTable", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@CreatedBy", createby.Text.Trim());
cmd.Parameters.AddWithValue("@PageIndex", pageIndex);
cmd.Parameters.AddWithValue("@PageSize", PageSize);
cmd.Parameters.Add("@RecordCount", SqlDbType.Int, 4);
cmd.Parameters["@RecordCount"].Direction = ParameterDirection.Output;
con.Open();
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
using (SqlDataReader dr = cmd.ExecuteReader())
if (dr.HasRows)
{
Template.Visible = true;
GridView1.DataSource = dt;
GridView1.DataBind();
}
else
{
Template.Visible = false;
}
int recordCount = Convert.ToInt32(cmd.Parameters["@RecordCount"].Value);
this.PopulatePager(recordCount, pageIndex);
}
}
con.Close();
}
}
}
catch (SqlException ex)
{
string msg = "Error:";
msg += ex.Message;
throw new Exception(msg);
}
}