Solved

What is the coolest way to get data and compare against a value?

Posted on 2006-06-28
5
311 Views
Last Modified: 2010-04-16
I am doing a project for a school that can only do business with clients who are within  certain ZipCodes. So I have a table called Users and a table called ValidZips.

I have set the Users Zip to a session variable called sv.Zip. Now what I need to do is load the data from ValidZips and compare sv.Zip against the list of Zips in ValidZips.

So far to accomplish something like this, I would create a local table, fill it with the values from ValidZips and then do a : foreach(DataRow r in Table.Rows) type thing.

I know that there are a lot of tools that I'm not using that c# offers, dataadapters, datasets, etc...

What would be the best technique to solve my problem. I would really like to get better at c# rather than just rehashing my current skills.

Thanks, Bill.
0
Comment
Question by:tatton777
[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
5 Comments
 
LVL 35

Accepted Solution

by:
mrichmon earned 500 total points
ID: 17005695
Actually hte best approach would be not to do this in C# at all.  Instead send the user's zip to the database to validate instead of returning the whole table of zip codes and then validating.

Database servers are set to easily compare information like this.

It would be a simple query like:

SELECT Zip FROM ValidZips WHERE ZipCode = @UserZip

If you get any rows returned then it is valid, else invalid.
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 17005821
Depending on load I may recommend pre-loading the table into a hashtable (note you would not want to use a foreach() etc as the operation would be O(n) as opposed to the hashtables O(1) ... but for a school project mrichmon is correct in that the SQL query would be the best option.
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17005830
As mrichmon mentioned it would be more efficient to run a query against the DB and have it filter the data. It would be even more efficient to create a Stored Procedure in the DB to do it for you.

However, if you want to use the brute-force method, then the best way would be to use a DataReader to quickly loop through all the records. DataReaders maintain a connection to the DB and only retrieve a single record at a time, so they have a much lower overhead than DataSets.
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 17005866
carl .. it would be no faster to use a sproc, please don't perpetuate the sproc efficiency myth it being dynamic sql and a sproc get their execution plans stored in the same cache (sql server).

In fact many are arguing that dynamic sql can actually be faster



0
 
LVL 41

Expert Comment

by:graye
ID: 17006199
Another approach would be to cache the zipcode table in a DataTable.   I assume that the table is rather small and the values wouldn't change very often (or at all).

So, you'd fill the DataTable using the normal DataAdapter's Fill() methdo, and then just use the DataTable's own Select() method to see if there was a match.  It'd be something like this:

DataRow[] foundRows =  SomeTable.Select( "Zip='" + UserZip + "'");

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdatadatatableclassselecttopic.asp
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

732 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