Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Filter DataSet

Posted on 2004-08-25
10
342 Views
Last Modified: 2008-02-01
Hi,

I have a dataset with multiple records.  I want to find one row in that dataset.  How do I do this?  I have two primary keys to make each row unique.  So basically I want to search the dataset based on two colum values.  Also I need to account for if nothing is returned from the serach (but there will never be two records returned).

Ryan
0
Comment
Question by:NeoTek
  • 4
  • 2
  • 2
  • +1
10 Comments
 
LVL 25

Accepted Solution

by:
RonaldBiemans earned 50 total points
ID: 11891685
try this

Dim vals(1) As Object
vals(0) = "searchstringfirstkey"
vals(1) = "searchstringsecondkey"
dim dr as datarow = yourdataset.Tables(0).Rows.Find(vals)

or

dim dv as new dataview = yourdataset.tables(0).defaultview
dv.sort = "yourfirstkey, yoursecondkey"
Dim vals(1) As Object
vals(0) = "searchstringfirstkey"
vals(1) = "searchstringsecondkey"
dim recordnumber = dv.find(vals)

0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 11891702
dr will return nothing (if no records are found)
recordnumber will return -1 (if no records are found)
0
 
LVL 5

Assisted Solution

by:LindzK
LindzK earned 25 total points
ID: 11892382
'create an array to hold the rows fround from the search
Dim aRows() as datarow
'perform a select on the row
'SearchField = the name of the field you want to search on
'SearchTerm = the text /input that must be contained within the search field
aRows = DataSet1.Tables(0).Select("SearchField =" & SearchTerm)
if aRows.length > 0 then
  'found the row, do whatever code you need for the row
  'you found here
else
  msgbox("No rows found")
end if

It works just like performing a select statement on the database, and returns an array of rows
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 1

Author Comment

by:NeoTek
ID: 11892418
Hi,

I tried this...

        Dim vals(2) As Object
        vals(0) = "1001AB    " 'lstSalesman.SelectedValue
        vals(1) = 2 'Session("DivisionID")
        vals(2) = "15L       " 'lstItem.SelectedItem
        Dim dr As DataRow = DsSales.Tables("Salesman_Budget_Input").Rows.Find(vals)

        If dr Is Nothing Then
            lblTitle.Text = "Nothing"
        Else
            lblTitle.Text = "Something"
        End If

and this...

        Dim dv As DataView = DsSales.Tables("Salesman_Budget_Input").DefaultView
        dv.Sort = "DivisionID" ', DivisionID, ItemSet"
        Dim vals(2) As Object
        vals(0) = 2
        vals(1) = 2
        vals(2) = "15L"
        Dim recordnumber = dv.Find(vals)

        If recordnumber = -1 Then
            lblTitle.Text = "Nothing"
        Else
            lblTitle.Text = "Something"
        End If

Neither return anything.  I don't know what I am doing wrong.  The values do exist in the database...

Ryan
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 11892469
Hi neotek,

 dv.Sort = "DivisionID" ', DivisionID, ItemSet"

 this line seems strange why twice divisionID and the syntax is wrong  (" ')

0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 11892523
And also,  the first method you tried only works with primary keys (do you have 3 primary keys) ?

the second (with the dataview) works with all fields
0
 
LVL 1

Author Comment

by:NeoTek
ID: 11893179
Hi,

I'm sorry.  It was my mistake.  All three methods work.  I didn't fill my dataset initally.

NeoTek
0
 

Expert Comment

by:asadeen
ID: 14004197
I too use the dataset.select() statement to filter a dataset but I get an error, I use C#

dataSet11.NPX_CONRATES.Select(­"WHERE
ListID='"+this.ListBox1.Select­edValue+"'");


I get an error


"System.Data.SyntaxErrorExcept­ion: Syntax error: Missing operand
after 'ListID' operator."


for the above dataset filter statement.


Please help me to solve this.


Thanks
Deen


0
 
LVL 5

Expert Comment

by:LindzK
ID: 14006774
Hi Deen,

If you review the answer that I gave for useing a select on the dataset, you may notice that you have added extra words in than required.  I won't tell you exactly which one though - You see, people answer questions on this forums to earn points as well as to help people.  It is not very fair, to pose a question of your own, underneath a question that another member has already payed for and had answered.  

LindzK
0
 

Expert Comment

by:asadeen
ID: 14006942
I am sorry I am very new to this, I used to visit quite often but recently started using. However I figured out that soon after I posted my message.

Thanks
Deen
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

838 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question