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 8038505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:15:30+00:00 2026-06-05T03:15:30+00:00

I have a problem. I have 2 tables which look like this: Table1: Country

  • 0

I have a problem. I have 2 tables which look like this:

Table1: Country

Name: Germany
ID: 1

Name: France
ID: 2

Table2: Cities

Name: Frankfurt
ID: 1

Name: Paris
ID: 2

I want to write a select statement where it compare the id’s and sorts the cities with the correct countries with the same id. The output should be in a json format.

My code look like this so far:

public class StadtHelper
{
   public class STADT
   {
       public string StadtName { get; set; }
       public string RegionID { get; set; }
   }

   internal static List<STADT> Stadt()
   {
       List<STADT> stadtObject = new List<STADT>();

       using (SqlConnection con = new SqlConnection(@"Data Source=Localhost\SQLEXPRESS;Initial Catalog=BOOK-IT-V2;Integrated Security=true;"))
       using (SqlCommand cmd = new SqlCommand(@"SELECT NAME, REGION_ID FROM STADT",con))
       {
           con.Open();
           using (SqlDataReader rdr = cmd.ExecuteReader())
           {
               while (rdr.Read())
               {
                   if (rdr["NAME"] != DBNull.Value && rdr["REGION_ID"] != DBNull.Value)
                   {
                       stadtObject.Add(new STADT()
                       {
                           StadtName = rdr["NAME"].ToString(),
                           RegionID = rdr["REGION_ID"].ToString()
                       });
                   }
               }
           }
       }

       return stadtObject;
   }
}

Thanks in advance

i edited my code:

    public class StadtHelper
{
    public class STADT
    {
        public string StadtName { get; set; }
        public string RegionName { get; set; }
    }
    internal static List<STADT> Stadt()
    {
        List<STADT> stadtObject = new List<STADT>();

        using (SqlConnection con = new SqlConnection(@"Data Source=Localhost\SQLEXPRESS;Initial Catalog=BOOK-IT-V2;Integrated Security=true;"))
        using (SqlCommand cmd = new SqlCommand(@"SELECT REGION.NAME, STADT.NAME FROM REGION, STADT WHERE REGION.ID = STADT.REGION_ID ORDER BY REGION.NAME" , con))
        {
            con.Open();
            using (SqlDataReader rdr = cmd.ExecuteReader())
            {
                while (rdr.Read())
                {
                    if (rdr["REGION.NAME"] != DBNull.Value && rdr["STADT.NAME"] != DBNull.Value)
                    {
                        stadtObject.Add(new STADT()
                        {
                            RegionName = rdr["REGION.NAME"].ToString(),
                            StadtName = rdr["STADT"].ToString()
                        });
                    }
                }
            }
        }
        return stadtObject;
    }
}

there is still a bug, my web service gives me an exeption:

 System.IndexOutOfRangeException: REGION.NAME
   at System.Data.ProviderBase.FieldNameLookup.GetOrdinal(String fieldName)
   at System.Data.SqlClient.SqlDataReader.GetOrdinal(String name)
   at System.Data.SqlClient.SqlDataReader.get_Item(String name)
   at StadtHelper.Stadt() in C:\Users\Yeah\Documents\Visual Studio 2010\Projects\WebService1\WebService1\StadtHelper.cs:line 31
   at WebService1.Service1.Stadt() in C:\Users\Yeah\Documents\Visual Studio 2010\Projects\WebService1\WebService1\Service1.asmx.cs:line 77
  • 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-05T03:15:31+00:00Added an answer on June 5, 2026 at 3:15 am

    Given your tables, the TSQL would look like,

        select ci.Name, co.Name
          from Cities  ci
    inner join Country co on co.ID = ci.ID
      order by ci.Name, co.Name
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 2 tables which in simplified form look like this: Products( id: int,
I have a problem. I have 3 tables like below: 1) met_ID (primair) etc.
Ok, here's my problem. I have database table BloodCenters , which have latitude and
I have problem when I tried to update two tables in one Store Procedure
Hi I have a problem saving 2 tables of my database at the same
So i have problem. I have a SQL server with DB and 2 tables:
I have that problem: I have a model with those 3 tables: Linha, Itinerario
I have a problem with multiple cascade path error. Here are my tables: Table
I have a problem with mutual constraints. I want to have two tables each
This is a real beginner Hibernate problem. I have a problem with the mappings

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.