Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Database lookup in a GridView?

Posted on 2010-08-25
6
Medium Priority
?
484 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 27

Accepted Solution

by:
Shaun Kline earned 2000 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 27

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

722 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