Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9023103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:40:54+00:00 2026-06-16T05:40:54+00:00

I’m using ASP.NET C# to output the results from a SQL Server database query

  • 0

I’m using ASP.NET C# to output the results from a SQL Server database query and trying to get the results to display only one record per court but that also displays multiple contact details per court.

The tables concerned are (PK in bold, FK in Italics):

Court (Court_ID, Court_Name, Note, Town, Postcode)

Contacts (Contact_ID, Contacts_Name, Contacts_no, *Court_ID*, *Court_Contact_Type_ID*)

Contact_Type (Court_Contact_Type_ID, Court_Contact_Type_Desc)

Currently my issue is that I get multiple repetitions of each court detail for as many contacts as each court has. I know why it’s doing it, but I don’t know the method for how to make it work the way I want.

I’m not sure if the solution lies within the SQL query itself (maybe resolved by nesting…?) or with the C# code because I’m using ‘HasRows’. I’ve included both code snippets below.

string myQuery = "SELECT Court_Name, Town, Postcode, Note, Contacts_Name, Contacts_no, Court_Contact_Type_Desc " +
                 "FROM Court C, Contacts CON, Contact_type CONT " +
                 "WHERE C.Court_ID = CON.Court_ID AND CON.Court_Contact_Type_ID = CONT.Court_Contact_Type_ID AND C.Court_ID = '3' ";


SqlConnection connection = new SqlConnection(connStr);
SqlCommand myCommand = new SqlCommand(myQuery, connection);
SqlDataReader myDataReader;
connection.Open();

myDataReader = myCommand.ExecuteReader();

if (myDataReader.HasRows)
{

    while (myDataReader.Read())
    {

        string court_name = myDataReader["Court_Name"].ToString();
        string court_town = myDataReader["Town"].ToString();
        string court_pcode = myDataReader["Postcode"].ToString();
        string court_note = myDataReader["Note"].ToString();
        string court_contact_name = myDataReader["Contacts_Name"].ToString();
        string court_contact_desc = myDataReader["Court_Contact_Type_Desc"].ToString();
        string court_contacts_no = myDataReader["Contacts_no"].ToString();

        Response.Write("<strong>" + court_name + "</strong><br>" + court_town + "<br>" + court_pcode + "<p>" + court_note + "</p>" + "<p>" + court_contact_name + " - " + court_contact_desc + " : " + court_contacts_no + "</p>");

    }

}

The eventual output should look something like this:

Abergavenny Magistrates’ Court
Abergavenny
NP7 5DL

This court is open for hearings only. Additional Court Notes….

Contacts
Contact Name 1 – Acting Court Manager: 01633 64xxxx
Contact Name 2 – Acting Office Manager: 01633 64xxxx
Contact Name 3 – Acting List Officer: 01633 64xxxx
Contact Name 4 – Justices’ Clerk: 01633 64xxxx

As ever any help gratefully received!

Cheers

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T05:40:55+00:00Added an answer on June 16, 2026 at 5:40 am

    You can solve the problem like this:

    • Add court_id to the select list
    • Add ordering criteria that put rows that belong to the same court next to each other
    • When iterating through the data coming back, see if you have seen the court id before
    • If this is the first time that you see the court id, show both the court and the contact details
    • If you have seen this court id before, show only the contact information

    Here is an example of how you can do it:

    string myQuery = "SELECT C.Court_ID, Court_Name, Town, Postcode, Note, Contacts_Name, Contacts_no, Court_Contact_Type_Desc " +
                 "FROM Court C "+
                 "JOIN Contacts CON ON C.Court_ID = CON.Court_ID "+
                 "JOIN Contact_type CONT ON CON.Court_Contact_Type_ID = CONT.Court_Contact_Type_ID " +
                 "WHERE C.Court_ID = '3' "+ // I assume the search criteria will be different
                 "ORDER BY C.Court_ID"; // Very important: records for the same court must be together
    
    ...
    string last_id = string.Empty;
    while (myDataReader.Read())
    {
        string court_id = myDataReader["Court_ID"].ToString();
        string court_name = myDataReader["Court_Name"].ToString();
        string court_town = myDataReader["Town"].ToString();
        string court_pcode = myDataReader["Postcode"].ToString();
        string court_note = myDataReader["Note"].ToString();
        string court_contact_name = myDataReader["Contacts_Name"].ToString();
        string court_contact_desc = myDataReader["Court_Contact_Type_Desc"].ToString();
        string court_contacts_no = myDataReader["Contacts_no"].ToString();
        if (last_id != court_id) {
            // Write court AND contact
            Response.Write("<strong>" + court_name + "</strong><br>" + court_town + "<br>" + court_pcode + "<p>" + court_note + "</p>" + "<p>" + court_contact_name + " - " + court_contact_desc + " : " + court_contacts_no + "</p>");
        } else {
            // Write only contact
            Response.Write("<p>" + court_contact_name + " - " + court_contact_desc + " : " + court_contacts_no + "</p>");
        }
        last_id = court_id;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to understand how to use SyndicationItem to display feed which is
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a view passing on information from a database: def serve_article(request, id): served_article
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I am using jsonparser to parse data and images obtained from json response. When
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.