Solved

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

Posted on 2006-06-28
5
299 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
5 Comments
 
LVL 35

Accepted Solution

by:
mrichmon earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 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

16 Experts available now in Live!

Get 1:1 Help Now