Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Code samples - voting

Status
Not open for further replies.

StevenB

IS-IT--Management
Sep 25, 2000
247
US
Hi folks,

I'm looking to build a relatively simple .NET web site that handles voting with a database back-end, something almost exactly like this site:


Well, it would be simple for you programmer types. I'm a .NET novice, so I'm looking for code snippets to give me some ideas. But most of what I can find is traditional polling code. (Here are 5 questions, answer them and get a graph of the results.)

I want mine to behave very much like that site above:

Basically, the site will present you with a value from the database. You'll vote on it by clicking a radio button, and the form will immediately submit and generate a new value for you to vote on.

Any suggestions for good places to find snippets of code?

Steve
 
I glanced over the site listed. You want to display random questions/surveys. The randomness can be limited by categories.

being I novice I would start simple and then expand the project to include more features. If you try to build everything you want in the first build you will get frustrated and end up with something shoddy.

the biggest challenge with your idea is selecting a random question. if your survey table looks like this
Code:
question
----------
question_id PK
question
...other columns
then select a random number to query the database with and display that question. Use the min/max question_id to contain the random range.

.Net has a random object which would make this very easy.
Code:
int minQuestionId = //select min(question_id) from question
int maxQuestionId = //select max(question_id) from question
int randomId = new Random(DateTime.Now.Ticks).Next(minQuestionId, maxQuestionId);

//select * from question where question_id = @randomId
You will need to use ADO.Net to communicate with the database. ASP.Net 2.0 has a SqlDataSource object you can implement directly in the asp.net code. This makes writing queries very easy.

before you write a single line of code I would highly recommend reading online tutorials (codeproject, aspalliance, 4guysfromrolla, etc...) and purchasing a good asp.net book.

The biggest pitfalls for beginners are:
1. web state: there isn't any. once a request is processed it's forgotten. (asp.net has some tricks for this, but the fact is state doesn't exist)
2. Learn the page life cycle. This is crucial to know when and where objects should be created, added, loaded and disposed of. The primary events are Init, Load, Render and Unload
3. Understand ViewState, Session, Application and Cache objects. each have their pros/cons. knowing which to use can make or break a site. this also ties into #2.

hopefully this isn't too much at once:)

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top