Solved

Filter DataSet

Posted on 2004-08-25
10
341 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

813 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now