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

  • Home
  • SEARCH
  • 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 8126581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:11:54+00:00 2026-06-06T07:11:54+00:00

Every time I run the code below it is supposed to come up with

  • 0

Every time I run the code below it is supposed to come up with anyone with the name of “sam” in the database but it doesn’t i just comes up with one person…

public IEnumerable<Person> GetPersons(string name)
        {
            string PersonID;
            string PersonName;
            var info = new Person[] {};
            try
            {
                string connectionString =
                    @"Password=nottelling;Persist Security Info=True;User ID=nottelling;Initial Catalog=customers;Data Source=db.example.com;";
                using (var connection = new SqlConnection(connectionString))
                {
                    string command = string.Format(@"SELECT Fname, PersonID, Lname FROM Person Where Fname = '{0}'",name);
                    connection.Open();
                    var getperson = new SqlCommand(command, connection);
                    SqlDataReader reader = getperson.ExecuteReader();
                    while (reader.Read())
                    {
                        PersonID = reader["PersonID"].ToString();
                        PersonName = reader["Fname"].ToString();
                        PersonName += " ";
                        PersonName += reader["Lname"].ToString();
                        info = new Person[] {new Person {PersonId = PersonID, Name = PersonName}};
                    }
                    connection.Close();
                }
            }
            catch (Exception ex)
            {
                PersonID = "Error:";
                PersonName = ex.Message;
                info = new[] {new Person {PersonId = PersonID, Name = PersonName}};

            }
            return info;
        }

and Person is :

namespace Calculator.Models
{
    public class Person
    {
        public string PersonId { get; set; }
        public string Name { get; set; }
    }
}

The query works because I already tested it multiple times on another program.
I think the error is from :

info = new Person[] {new Person {PersonId = PersonID, Name = PersonName}};

because it replaces the whole value of info how would I not replace the whole value and just add to it.

  • 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-06T07:11:57+00:00Added an answer on June 6, 2026 at 7:11 am

    UPDATE: As recommended by DanC, I have changed the array type

    You are assigning a brand new array each time… You should be adding to the array… Look at using something like the ArrayList class for ease of use

    Instead of…

    public IEnumerable<Person> GetPersons(string name)
    

    Change to…

    public List<Person> GetPersons(string name)
    

    Instead of…

    var info = new Person[] {};
    

    Change to…

    List<Person> info = new List<Person>;
    

    Instead of…

    info = new Person[] {new Person {PersonId = PersonID, Name = PersonName}};
    

    Change to…

    info.Add(new Person {PersonId = PersonID, Name = PersonName});
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone explain why the code below, when run, will occasionally result in some
I have a very simple class (shown below), however every time I run it,
I have this warning every time I run my CGI-script (output is rendered by
I'm running through the gitimmersion.com labs and every time I run a: git hist
I am getting a warning I do not fully understand every time I run
I have a spork gem issue. Every time I run the spork command I
I am having problem with RubyMine 4.0.1 on Windows 7. Every time i run
Why does every time I do mvn jetty:run, maven tries to download some dependencies
I'm using VS2008 (with windows XP). Every time I try to run a unit
I have a Visual Studio 2005 solution. Every time I try to run the

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.