«

»

Apr 03

how to filter dataset in asp.net using c#

Hi,

In this article I will explain you how to set filter in dataset while getting data from backend to frontend.

Observe the below sqlserver database table , In this we can see the pincode column were it contain different values in pincode column

DBImage

Eg:I want to get only picode=524004 for that I can write condition in database also but in some scenarios like dynamic sp’s, It won’t work at that scenario we can use this filter in code level

1.Add a page FilterInDS.aspx to solution

Copy and paste this Body in that page

[code language=”css”]

<body>

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

<div>

<asp:Button ID="btnGetData" runat="server" Text="GetData" OnClick="btnGetData_Click" />

<asp:GridView ID="gdvData" runat="server">

</asp:GridView>

</div>

</form>

</body>

[/code]

Copy and paste below code in cs file

[code language=”csharp”]

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data.SqlClient;

using System.Data;

public partial class FilterinDS : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

}

protected void btnGetData_Click(object sender, EventArgs e)

{

SqlConnection Connection = new SqlConnection("Server=Naseer-PC;Database=Naseer;Uid=sa;Pwd=123");

DataSet ds = new DataSet();

SqlDataAdapter adapter = new SqlDataAdapter("select * from Tbl_Mst_EmployeeDetails", Connection);

try

{

adapter.Fill(ds);

var strExpr = "PinCode = ‘" + 524004 + "’";

var dv = ds.Tables[0].DefaultView;

dv.RowFilter = strExpr;

var newDataTable = dv.ToTable();

DataSet ds1 = new DataSet();

ds1.Tables.Add(newDataTable);

if (newDataTable.Rows.Count > 0)

{

gdvData.DataSource = newDataTable;

gdvData.DataBind();

}

}

catch (Exception ex)

{

Connection.Close();

}

}

}

[/code]

Execute the code and you will get filtered data based on pincode as below

DataBind

Happy Coding!!!!!

Admin.

1 comment

  1. w45ry

    hello

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>