0

real newbie question and I would appreciate any support you could offer. I am trying to output SQL results to a list. But when I return the list I get System.Collections.Generic.List`1[System.String] This my code, any ideas? Thank you in advance.

    public static List<string> GetDTSXPackages()
    {
        List<String> packages = new List<String>();

        using (SqlConnection connection = new SqlConnection(SQLConnectionString()))
        {
            string query = "SELECT PackageName FROM SharedServices.DTSXPackages";
            using (SqlCommand command = new SqlCommand(query, connection))
            {
                connection.Open();
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        packages.Add(reader["PackageName"].ToString());
                    }
                }
                connection.Close();
            }
        }

        return packages;
    }


    static void Main(string[] args)
    {
        List<string> x = SQLAccessLayer.GetDTSXPackages();

        foreach (var package in x)
        {
            Console.WriteLine(x);
        }
    }
3
  • This code seems ok. How do you call GetDTSXPackages and use the returned value? Commented Jul 11, 2016 at 10:20
  • What are you trying to do with this list? I guess, you're trying to write it to console or something like this, using List.ToString since output you've provided is exactly result of calling ToString on such a list. Commented Jul 11, 2016 at 10:20
  • I suppose you are expecting packages.ToString() to display [package1,package2] ? Iterate over the list Commented Jul 11, 2016 at 10:22

2 Answers 2

1

It is not an error By doing a loop you can see all values

foreach(var i in Packages)
{
    Console.WriteLine(i);
}
Sign up to request clarification or add additional context in comments.

1 Comment

@user3801561 its same code..instead of printing the each var u r printing wrongly..thats the problem
0

Change

Console.WriteLine(x);

To

Console.WriteLine(package);

1 Comment

@user3801561 Glad I could help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.