how can rilter top 5 record from dataset to dataview/datatable

Posted by Bhuwan87rawat under ASP.NET on 8/20/2013 | Points: 10 | Views : 3095 | Status : [Member] | Replies : 4
In my application i make a AdvisorComment.aspx page in this page user comments on his article and show comments
i want to show only 5 records on page load and when user select all show on dropdown then show all comments on the page

this is code ..
void RecentView()
{
string dyntable = string.Empty;
DataTable dtfilter = new DataTable();
Financial_Action.ActionClient obj = new ActionClient();
DataSet objAdArtcl = new DataSet();
try
{
string query_string = HttpContext.Current.Request.QueryString.ToString();
obj.Open();
//if (Session["ADVISOR_ID"] != null)
//{
// if (Request.QueryString["story_type_ID"] != null && Request.QueryString["ADVISOR_ID"] != null)
if (Request.QueryString["ID"] != null)
{
story_id = Request.QueryString["ID"];

}
if (Request.QueryString["ADVISOR_ID"] != null)
{
advisor_ID = Request.QueryString["ADVISOR_ID"];
}



objAdArtcl = obj.GetAdvisor_Comments(advisor_ID, story_id, obj_presentation);

if (objAdArtcl != null && objAdArtcl.Tables[0].Rows.Count > 0)
{
//string GEtTopComments = "5";
DataView dv = new DataView(objAdArtcl.Tables[0]);
// dv.RowFilter = objAdArtcl.Tables[0].Select("top '"+ GEtTopComments +"'");

// dv.RowFilter = objAdArtcl.Tables[0].Select("Col < 3");
//dv.RowFilter = "Advisor_id<5";
dtfilter = dv.ToTable();
if (!string.IsNullOrEmpty(Convert.ToString(objAdArtcl.Tables[0].Rows[0]["Descreption"])))
{
for (int i = 0; i < objAdArtcl.Tables[0].Rows.Count; i++)
{
// int count = 0;
//if (count < 5)
// {



dyntable += "<table width='99%' style='style='border-bottom: 5pt solid silver;text-align: center;border-collapse: collapse'><tbody>";


dyntable += "<tr >";





dyntable += "<td >";
dyntable += " <div style='text-align:left;font-size: 14px;padding: 0px 4px 0px 9px;font-family: arial;'>";
dyntable += "<div><span class=\"vm_arrow\"style='float:left;'>" + objAdArtcl.Tables[0].Rows[i]["Descreption"].ToString() + "</span></div></td></tr><tr><td colspan='2'";
dyntable += "<div style='float:right;'>[<i><span>" + objAdArtcl.Tables[0].Rows[i]["AdvisorName"].ToString() + "</span>" + Convert.ToDateTime(objAdArtcl.Tables[0].Rows[i]["CREATED_DATE"].ToString()).ToString("dd/MM/yyyy") + "</i>]</div>";
dyntable += "</div></td>";


dyntable += "</tr>";
dyntable += "</tbody></table><hr/>";


// }

}
}


recntComt.InnerHtml = dyntable;
}
else
{
recent.Vis




Responses

Posted by: Bandi on: 8/20/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
dt.Rows.Cast<System.Data.DataRow>()[b].Take(n)[/b]

-- Sample code

//Create a Datatable
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Item"), new DataColumn("Price"), new DataColumn("Total") });
dt.Rows.Add("Shirt", 200, 0);
dt.Rows.Add("Football", 30, 0);
dt.Rows.Add("Bat", 22.5, 0);
dt.Rows.Add("Ring", 25, 0);
dt.Rows.Add("Band", 77, 0);
dt.Rows.Add("Glass", 57, 0);

//Fetch Top 2 Rows
DataTable dtTop = dt.Rows.Cast<DataRow>().Take([b]2[/b]).CopyToDataTable();


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Bhuwan87rawat, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 8/20/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
-- Alternate solution is
write a stored procedure in the database and call procedure in the application
CREATE PROCEDURE USP_GetComments( @Filter VARCHAR(20))

AS
BEGIN
IF ( @filter IS NULL)
SELECT * FROM TableName ORDER BY CREATE_DATE DESC; -- select all records from table
ELSE IF( @filter = 'All')
SELECT TOP 5 * FROM TableName ORDER BY CREATE_DATE DESC; -- selects recent 5 comments
END
GO
EXEC USP_GetComments NULL -- for all records
GO
EXEC USP_GetComments 'All' -- for top 5 records


on page load call USP_GetComments by passing 'All' string to the param @filter
For "All Comments" selection, call USP_GetComments by passing param value as NULL

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Bhuwan87rawat, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jayakumars on: 8/20/2013 [Member] [MVP] Bronze | Points: 25

Up
0
Down
hi
Bandi

Good code for this

//Fetch Top 2 Rows
DataTable dtTop = dt.Rows.Cast<DataRow>().Take(2).CopyToDataTable();

Mark as Answer if its helpful to you

Bhuwan87rawat, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 8/20/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Another way to get this using LINQ is as follows:-

DataTable dtTop = dt.AsEnumerable().Take(5).CopyToDataTable();

Happy coding.

Bhuwan87rawat, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response