Filter DataSet (XSD) with parameter using C# and VB.Net in ASP.Net

Last Reply one month ago By pandeyism

Posted one month ago

Hi,

Using parameter query in DataSet XSD which is below.

SELECT Atn_No,Atn_Date2, Atn_Type, Dg_Code, Emp_Name, Emp_Number, Hst_No, Shift_Desc FROM View_AttendanceReport Where Atn_No=@Atn_No

How to filter DataSet XSD with parameter in ASP.Net

Posted one month ago

I will get back soon.


Posted one month ago

Hi basit0079,

Refer below sample.

HTML

<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="CustomerId" HeaderText="CustomerId" />
        <asp:BoundField DataField="Name" HeaderText="Name" />
        <asp:BoundField DataField="Country" HeaderText="Country" />
    </Columns>
</asp:GridView>

Namespaces

C#

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

VB.Net

Imports System.Data.SqlClient
Imports System.Data

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        int customerid = 1;
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        SqlConnection con = new SqlConnection(constr);
        SqlCommand cmd = new SqlCommand("SELECT CustomerId,Name,Country FROM Customers", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);

        DataSet1 dsCustomers = new DataSet1();
        foreach (DataRow row in dt.Rows)
        {
            dsCustomers.Tables[0].ImportRow(row);
        }
        // Filter Data from dataset 
        DataTable dtFiltered = dsCustomers.Tables[0].Select("CustomerId = " + customerid + "").CopyToDataTable();

        //Filter Data from dataset using dataview
        DataView dataView = dsCustomers.Tables[0].DefaultView;
        if (customerid != 0)
        {
            dataView.RowFilter = "CustomerId = " + customerid + "";
        }

        gvCustomers.DataSource = dtFiltered;
        gvCustomers.DataBind();
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim customerid As Integer = 1
        Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
        Dim con As SqlConnection = New SqlConnection(constr)
        Dim cmd As SqlCommand = New SqlCommand("SELECT CustomerId,Name,Country FROM Customers", con)
        Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
        Dim dt As DataTable = New DataTable()
        da.Fill(dt)
        Dim dsCustomers As DataSet1 = New DataSet1()

        For Each row As DataRow In dt.Rows
            dsCustomers.Tables(0).ImportRow(row)
        Next
        'Filter Data from dataset 
        Dim dtFiltered As DataTable = dsCustomers.Tables(0).[Select]("CustomerId = " & customerid & "").CopyToDataTable()
        'Filter Data from dataset using dataview
        Dim dataView As DataView = dsCustomers.Tables(0).DefaultView
        If customerid <> 0 Then
            dataView.RowFilter = "CustomerId = " & customerid & ""
        End If

        gvCustomers.DataSource = dtFiltered
        gvCustomers.DataBind()
    End If
End Sub

Screenshot