Solved

Database lookup in a GridView?

Posted on 2010-08-25
6
482 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
[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
  • 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
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!

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

624 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