C# class - List issue

Hi, See below my code, No output, any idea?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Configuration;
using System.Text.RegularExpressions;
namespace TestPush
{
    public class Test
    {
     

        public string TestName { get; set; }

 
       
        private SqlConnection con;
        private dbConnect dbconnect;

       

        

        //===========================================================================================
        public List<Test> Readytosend()
        {
            
            List<Test> lstready = new List<Test>();
            con = new SqlConnection();
            con.ConnectionString = dbconnect.Connectionname;
            con.Open();
            SqlCommand cmdReadysend = con.CreateCommand();
            cmdReadysend.CommandText = "TestSp"; // no parameters,Only select statement
            cmdReadysend.CommandType = CommandType.StoredProcedure;
            SqlDataReader rdrReadytoSend = cmdReadysend.ExecuteReader();
            while (rdrReadytoSend.Read())
            {

                lstready.Add(

              new Test
              {
                  TestName  = rdrReadytoSend["TestName"].ToString()

              }





                    );
            
            }



            con.Close();

            return lstready;
        
        
        
        }
        //===========================================================================================


    }
}

     //===========================================================================================

// output

     //===========================================================================================
  class Program
    {
        static void Main(string[] args)
        {
  


            List<Test> jb1 = new List<Test>();

            foreach (var j in jb1)
            {
                j.Readytosend();
            
            }
Console.Read();
          }
}

Open in new window

LVL 2
lankapalaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

it_saigeDeveloperCommented:
You are not writing anything out, your method simply returns a list.  Instead you need to iterate over the result of Readytosend; e.g. -
using System;
using System.Collections.Generic;
using System.Linq;

namespace EE_Q29074148
{
    class Program
    {
        static void Main(string[] args)
        {
            foreach (var test in MockTest.ReadyToSend())
                Console.WriteLine(test.TestName);
            Console.ReadLine();
        }
    }

    class MockTest
    {
        public string TestName { get; set; }

        public static IEnumerable<MockTest> ReadyToSend()
        {
            // Mocking the db call
            var results = (from i in Enumerable.Range(0, 20) select $"ReadyToSend{i}");
            var enumerator = results.GetEnumerator();
            while (enumerator.MoveNext())
            {
                yield return new MockTest { TestName = enumerator.Current };
            }
        }
    }
}

Open in new window

Which produces the following output -Capture.PNG
-saige-
0
Dirk StraussSenior Full Stack DeveloperCommented:
The first problem is that you are trying to loop through an empty collection because List<Test> jb1 is empty.
Empty collectionThe loop is therefore skipped. It is also not really clear what you are trying to achieve here. I would expect something along the lines of:
List<Test> jb1 = new List<Test>();
Test myTest = new Test();
 jb1 = myTest.Readytosend();

foreach (var j in jb1)
{
   // Read the data and put the output into a Console.WriteLine statement
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.