• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 80
  • Last Modified:

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

0
lankapala
Asked:
lankapala
1 Solution
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now