Solved

Filter DataSet

Posted on 2004-08-25
10
344 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
[X]
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
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Suggested Solutions

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
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 …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

738 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