Wednesday, March 21, 2012

DataSet Filter

I have one dataset that contains thousands of records i want to filter the dataset by one of the name

select col1,col2,col3 from tablename

this col1 contains various names i want to check wheather an name exists in the col1 or not

please guide me

Hi, Febin

For your problem there is two possible solutions, First one is RowFilter and second one is Select

Solution 1 (RowFilter)

Dim ds As New Data.DataSet

Dim CN As New SqlClient.SqlConnection(MyClass1.ConnStr)

Dim Stmt As String

Stmt = "select * from USER_MASTER"

Dim DA As New SqlClient.SqlDataAdapter(stmt, CN)

DA.Fill(ds, "USER_MASTER")

ds.Tables("USER_MASTER").DefaultView.RowFilter = "DEPT ='ADMIN'"

MsgBox(ds.Tables("USER_MASTER").DefaultView.Count)

Solution 2 (Select)

Dim ds As New Data.DataSet

Dim CN As New SqlClient.SqlConnection(MyClass1.ConnStr)

Dim Stmt As String

Stmt = "select * from USER_MASTER"

Dim DA As New SqlClient.SqlDataAdapter(stmt, CN)

DA.Fill(ds, "USER_MASTER")

Dim R() As DataRow

R = ds.Tables("USER_MASTER").Select("DEPT ='ADMIN'", "EMP_NAME")

If R.Length > 0 Then

MsgBox(R.Length & " Record(s) Found")

End If

In the sample code it shows the number of records found for dept called "ADMIN",

Happy Coding

sql