Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a program that displays a list of values matching particular conditions. When i run the SP to retrieve the values in C# console app i get System.Collections.Generic.List'1[StringsTest.Models.Broker] being printed instead of the result set. However running exec getCOAgents in SSMS returns my desired resultset.
My SP is as below :
SQL
 CREATE PROCEDURE [dbo].[getCOAgents]    
    AS
    BEGIN   
SET NOCOUNT ON;
SELECT         a.agent_shortname
    FROM            BrokerTest AS a INNER JOIN
                         (SELECT        agent_shortname, COUNT(*) AS Expr1
                           FROM            BrokerTest
                           GROUP BY agent_shortname
                           HAVING         (COUNT(*) > 1)) AS b ON a.agent_shortname = b.agent_shortname
    WHERE        (a.agent_shortname LIKE '_________C%') AND (a.term = 
    'NB')
    ORDER BY a.agent_shortname
            END
            GO


What I have tried:

This SP returns a list of values with the character C at index 10 . I am invoking the GetData() in console app's Main method as below:

C#
	namespace StringsTest
{
   class Program
    {
        static void Main(string[] args)
        {
 Console.WriteLine(GetData());
} } }


And the function as :
C#
public static List<Broker> GetData()
        {          
            List<Broker> details = new List<Broker>();
            using (SqlConnection conn = new SqlConnection(Helpers.DatabaseConnect))
            {
                conn.Open();
                DataTable dt = new DataTable();
                SqlCommand cmd = new SqlCommand("getCOAgents", conn);
                cmd.CommandType = CommandType.StoredProcedure;               
                cmd.ExecuteNonQuery();
           
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dt);
                foreach (DataRow dr in dt.Rows)
                {
                    Broker broker = new Broker();
                    broker.Code = Convert.ToString(dr["agent_shortname"]);
                    details.Add(broker);
                }
                conn.Close();
            }
           return  details;
        }


Expected output in console (example result set)

ALID-HEA-CO-001
ALID-HEA-CO-001
ALID-HEA-CO-001
EMIL-MTR-CO-012
EMIL-MTR-CO-012
Posted
Updated 27-Sep-19 3:48am

Simple:
C#
static void Main()
{
    List<Broker> data = GetData();
    foreach (Broker broker in data)
    {
        Console.WriteLine(broker.Code);
    }
}
 
Share this answer
 
GetData returns a List of type Broker, and your Console.WriteLine is reflecting just that; and is working as intended. What You really want to do then is to iterate through that List. And there are a couple of ways of doing this

The first would be in your Main module
C#
static void Main(string[] args) {
 	List<Broker> BrokerList = GetData();
	foreach(Broker b in BrokerList) {
		Console.WriteLine(b);
	}
}
Another would be to pass this off into another Void
C#
private void DisplayList(List<Broker> bl) {
	foreach(Broker b in BrokerList) {
		Console.WriteLine(b);
	}
}
// alter Main to use this display method
static void Main(string[] args) {
 	List<Broker> BrokerList = GetData();
	DisplayList(BrokerList);
}
There are other ways you could do this as well... If you wanted to super-encapsulate this you create create a wrapper for the BrokerList and give it it's own ToString() override, which basically would fall back on the same iteration routine
C#
public class BrokerListWrapper {
	List<Broker> BrokerList { get; set; }
	public BrokerListWrapper(){}

	public override string ToString() {
		StringBuilder sb = new StringBuilder();
		foreach(Broker b in BrokerList) {
			sb.AppendLine(b);
		}
		return sb.ToString();
	}
}

So in short... there are a myriad of ways to do what you want, you just need to know what you want and how to make it work via whatever calling method
 
Share this answer
 
Comments
Tshumore 27-Sep-19 10:11am    
Thanks for the insight. I have amended to
static void Main(string[] args) {
List<broker> BrokerList = GetData();
foreach(Broker b in BrokerList) {
Console.WriteLine(b);
}
}
as above .. Console prints a list but incorrect values :
StringsTest.Models.Broker.
StringsTest.Models.Broker
StringsTest.Models.Broker
StringsTest.Models.Broker
..........................
Seems GetData() is still not retrieving values. Could it be because my SP does not have an OUTPUT parameter.
MadMyche 27-Sep-19 10:18am    
Debugging will tell you if GetData is getting values... And I missed the code property in Broker so it should actually be Console.WriteLine(b.Code);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900