[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2006-06-28
5
Medium Priority
?
314 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

649 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