Home | Blog | Software | Reviews and Features | Forum | Help | Donate | About us
topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • September 02, 2015, 03:38:06 AM
  • Proudly celebrating 10 years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: ASP.NET and C# webform. Need help with query to DB  (Read 2770 times)

h0meopathic

  • Participant
  • Joined in 2007
  • *
  • default avatar
  • Posts: 24
    • View Profile
    • Donate to Member
ASP.NET and C# webform. Need help with query to DB
« on: July 30, 2009, 03:17:53 AM »
I need a way to check my database and see if a record is repeated and if it's repeated twice.

I'm making a webform that will allow the user to enter his or her personal information and select a radio button of specific times.  The radio button will correspond to a SlotID in my database. When the SlotID has been used twice I need to grey out the radio button because it can no longer be selected. Any help would be nice.

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 7,315
  • "In my dreams, I always do it right."
    • View Profile
    • wraith808
    • Donate to Member
Re: ASP.NET and C# webform. Need help with query to DB
« Reply #1 on: July 30, 2009, 09:16:02 AM »
I'm not sure what database you're using, but with ASP.NET and C#, I'll assume SQL Server.  Also assuming that the table name is Slot, and with everything else you've given, this query would give the number of times each SlotID has been used.

Code: Text
  1. SELECT count(1), SlotID
  2. FROM Slot
  3. GROUP BY SlotID
  4.  

If you only wanted to get the ones that had been used more than 1 time then you could add a having clause, i.e.

Code: Text
  1. SELECT count(1), SlotID
  2. FROM Slot
  3. GROUP BY SlotID
  4. HAVING count(1) > 1
  5.  

HTH

h0meopathic

  • Participant
  • Joined in 2007
  • *
  • default avatar
  • Posts: 24
    • View Profile
    • Donate to Member
Re: ASP.NET and C# webform. Need help with query to DB
« Reply #2 on: July 30, 2009, 06:25:47 PM »
What is count(1)?
I am using SQL server and my table is as follows:

Table AppointmentName: AppID, LastName, FirstName, Email, BirthDate, Slot.

Slot gets an SelectedIndexValue from the radio button on submitClick.

On Page_Load is where I need to have the SQL statement to see how many times the value from the column "Slot" has been used.
« Last Edit: July 30, 2009, 07:49:23 PM by h0meopathic »

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 7,315
  • "In my dreams, I always do it right."
    • View Profile
    • wraith808
    • Donate to Member
Re: ASP.NET and C# webform. Need help with query to DB
« Reply #3 on: July 30, 2009, 09:21:03 PM »
Count(1) just counts the number of records in the query.  You could use count(slotid), but by using 1, the data for the slotid doesn't have to actually be loaded.

h0meopathic

  • Participant
  • Joined in 2007
  • *
  • default avatar
  • Posts: 24
    • View Profile
    • Donate to Member
Re: ASP.NET and C# webform. Need help with query to DB
« Reply #4 on: July 30, 2009, 11:23:34 PM »
I figured it out, thanks for the help.

Code: Text
  1. SELECT Slot, COUNT(Slot)
  2. AS SlotCount
  3. FROM AppointmentName
  4. GROUP BY Slot
  5.  

h0meopathic

  • Participant
  • Joined in 2007
  • *
  • default avatar
  • Posts: 24
    • View Profile
    • Donate to Member
Re: ASP.NET and C# webform. Need help with query to DB
« Reply #5 on: July 31, 2009, 06:52:13 AM »
Code: C#
  1. SqlConnection conn;
  2.         DataSet dataSet = new DataSet();
  3.         SqlDataAdapter adapter;
  4.         SqlCommandBuilder commandBuilder;
  5.  
  6.         conn = new SqlConnection("Server=localhost\\SqlExpress; Database=HealthFair;" +
  7.                 "Integrated Security = True");
  8.         adapter = new SqlDataAdapter("SELECT Slot, COUNT(Slot) AS SlotCount FROM AppointmentName " +
  9.             "GROUP BY Slot HAVING COUNT(Slot) > 1", conn);
  10.         adapter.Fill(dataSet);
  11.         commandBuilder = new SqlCommandBuilder(adapter);
  12.         adapter.Update(dataSet);
  13.  

Now how do I get the data into objects so I can determin which records in Slots to cut off?