Solved

Database lookup in a GridView?

Posted on 2010-08-25
6
478 Views
Last Modified: 2012-05-10
I have a gridview where I'm getting a number of rows returned...

SELECT SessionName, Capacity from Sessions

However,  this makes my GridView look something like this

Session 1 |  20
Session 2 |  25

However, I'd like to do another lookup on the registrants table to see how many are registered for each session, so that my GridView would look something like this...

Session 1 | 14/20
Session 2 | 11/25

Is there a way that I could do a database lookup from the registrants table for each row??  Kind of hard to explain...

But basically it will add a database lookup for every session in the sessions table.  That might be 30-40 lookups.  Is that a problem?

Can I do this in a GridView?  Or do I need to use a C# loop?
0
Comment
Question by:sayguh
  • 4
  • 2
6 Comments
 
LVL 26

Accepted Solution

by:
Shaun Kline earned 500 total points
ID: 33525286
There should be no problem with that. You could use the OnRowDataBound event of the GridView to perform the subsequent lookups.

However, it would be more efficient to perform all of the data retrieval using SQL. If your registrants table has a foreign key field to your Sessions table, you should be able to retrieve a count of the registrants per session. From there, you can either use SQL to format the returned information (registrants / capacity), or return them as separate values in your dataset.
0
 
LVL 2

Author Comment

by:sayguh
ID: 33531144
Anyway you could provide some sample code?  Everything I'm googling looks overly complicated

Thanks if you can!
0
 
LVL 2

Author Comment

by:sayguh
ID: 33531640
Alright, I have it doing the database lookup for each row now..   but I'm just doing

"SELECT count from registrants"

How can I do "SELECT count from registrants WHERE session_ID=" + ???

How can I reference the dynamic session ID for that row?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 2

Author Comment

by:sayguh
ID: 33531740
I tried

session_ID=" + e.Row.Cells[1].Text

But that didn't seem to work...

(the session ID is in that cell)
0
 
LVL 2

Author Comment

by:sayguh
ID: 33532121
Got it!
0
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 33533050
Just to follow up on the my SQL comment above, you could use a SQL call such as:

SELECT Sessions.Session_ID, Sessions.SessionName, Sessions.Capacity, Count(*) Registrant_Count
FROM Sessions
   INNER JOIN registrants ON Sessions.session_ID = registrants.session_ID
GROUP BY Sessions.Session_ID, Sessions.SessionName, Sessions.Capacity

This should bring back all of the parts you would need to display the data that you want in your gridview and, therefore, reduce the number of database calls your application makes.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

759 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

18 Experts available now in Live!

Get 1:1 Help Now