DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

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

h0meopathic

  • Participant
  • Joined in 2007
  • *
  • Posts: 24
ASP.NET and C# webform. Need help with query to DB
« on: July 30, 2009, 08:17 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: 11,205
    • Participated in NANY Challenge Fourteen Times
    • Coding Snacks
    • Writer
    • Hardware Hacker
Re: ASP.NET and C# webform. Need help with query to DB
« Reply #1 on: July 30, 2009, 02:16 PM »
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: (sql)
SELECT count(1), SlotID
FROM Slot
GROUP BY SlotID

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: (sql)
SELECT count(1), SlotID
FROM Slot
GROUP BY SlotID
HAVING count(1) > 1

HTH

h0meopathic

  • Participant
  • Joined in 2007
  • *
  • Posts: 24
Re: ASP.NET and C# webform. Need help with query to DB
« Reply #2 on: July 30, 2009, 11:25 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 31, 2009, 12:49 AM by h0meopathic »

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 11,205
    • Participated in NANY Challenge Fourteen Times
    • Coding Snacks
    • Writer
    • Hardware Hacker
Re: ASP.NET and C# webform. Need help with query to DB
« Reply #3 on: July 31, 2009, 02:21 AM »
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
  • *
  • Posts: 24
Re: ASP.NET and C# webform. Need help with query to DB
« Reply #4 on: July 31, 2009, 04:23 AM »
I figured it out, thanks for the help.

Code: (sql)
SELECT Slot, COUNT(Slot)
AS SlotCount
FROM AppointmentName
GROUP BY Slot

h0meopathic

  • Participant
  • Joined in 2007
  • *
  • Posts: 24
Re: ASP.NET and C# webform. Need help with query to DB
« Reply #5 on: July 31, 2009, 11:52 AM »
Code: (csharp)
SqlConnection conn;
        DataSet dataSet = new DataSet();
        SqlDataAdapter adapter;
        SqlCommandBuilder commandBuilder;

        conn = new SqlConnection("Server=localhost\\SqlExpress; Database=HealthFair;" +
                "Integrated Security = True");
        adapter = new SqlDataAdapter("SELECT Slot, COUNT(Slot) AS SlotCount FROM AppointmentName " +
            "GROUP BY Slot HAVING COUNT(Slot) > 1", conn);
        adapter.Fill(dataSet);
        commandBuilder = new SqlCommandBuilder(adapter);
        adapter.Update(dataSet);

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

Original address: https://www.donationcoder.com/forum/index.php?topic=19351.0