?
Solved

See if record exists without looping?

Posted on 2005-04-20
2
Medium Priority
?
952 Views
Last Modified: 2012-06-27
Is there a built-in function to determine whether a value exists in a DataSet?  Here's my DataSet:

    DataSet ds = new DataSet();
    SqlDataAdapter da = new SqlDataAdapter ("SELECT DISTINCT d_date FROM mytable", conn);
    da.Fill(ds);

I want to know if a given date exists in the above DataSet, and I'd prefer not to loop through all the records because I need to check for dates many times and have to loop each time.  I'm hoping there's a more efficient way.  For example, something like:

   bool exists = (ds.Tables[0].Rows.FindByValue("04/01/2005") > -1);

...or something similar (this is just an example).  'exists' would be true if it finds the value in the DataSet, else false.  Or am I forced to loop through each row to find out if it exists?  Like so:

private bool CheckForDate(string sDate) {
    foreach(DataRow dr in ds.Tables[0].Rows) {
        if(dr[0].ToString() == sDate) return true;
    }
    return false;
}
0
Comment
Question by:hendridm
2 Comments
 
LVL 3

Accepted Solution

by:
sharpnet earned 1600 total points
ID: 13828951
You can use the Tables[0].DefaultView.Find() or .FindRows().  I would run sort on the column first, but if I'm not mistaken, both methods will search the entire row for the value you supply.  Keep in mind that find and findrows methods do not except wildcards, so you're input must be exact.

Also it sometimes helps if you have more than one column to set the sort to the column you want to search through (if you know it of course).

Ex:

ds.Tables[0].DefaultView.Sort = "columnname"; // if more than one column
ds.Tables[0].DefaultView.FindRows("valuetofind"); // this returns an array of DataRowView objects if any are found.

or

ds.Tables[0].DefaultView.Find("valuetofind"); // returns the index number of the row if found.

Hope this helps.

Nick
0
 
LVL 8

Author Comment

by:hendridm
ID: 13833862
Perfect.  Thanks!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month14 days, 18 hours left to enroll

840 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