Filtering A Dataset Using Select Method In CSharp

  • This article will teach us how to filter a dataset in C# using the select function.
  1. App.xml file
<?xml version="1.0" encoding="utf-8" ?>

<configuration>

<appSettings>

                        <add key="dsn" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\emp.mdb" />

            </appSettings>

</configuration>
using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Data.OleDb;

namespace Filtering_a_dataset_using_select_method_Cs

{

    public partial class Form1 : Form

    {

        string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];

        OleDbCommand com;

        OleDbDataAdapter oledbda;

        DataSet ds;

        DataTable dt;

        string str;

        public Form1()

        {

            InitializeComponent();

        }

 

        private void btnfilterdata_Click(object sender, EventArgs e)

        {

            OleDbConnection con = new OleDbConnection(ConnectionString);

            try

            {

                con.Open();

                str = "select * from student";

                com = new OleDbCommand(str, con);

                oledbda = new OleDbDataAdapter(com);

                ds = new DataSet();

                oledbda.Fill(ds, "student");

                dt = ds.Tables["student"];

                string s1 = null;

                string s2 = null;

                s1 = "smarks>60";

                s2 = "smarks asc";

                DataRow[] result = null;

                result = dt.Select(s1, s2);

                int ctr = 0;

                for (ctr = 0; ctr <= (result.Length - 1); ctr++)

                {

                    ListBox1.Items.Add(result[ctr]["smarks"]);

                }

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

            con.Close();

 

        }

    }

}

Output :

Submit a Comment

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

Subscribe

Select Categories