Question

I am a complete 100% beginner so please forgive my ignorance in advance!!

Can someone help me return this list array from my c# database query? I am writing a c# application using visual Studio that connects to a MySQL DB I have created.

Here is the code I am having trouble with. (I have searched many times but I am just not understanding something, please help!)

public List<string>[] clientList()  // Select from customer table in MySQL db
  {

       string query = "SELECT " + thisItem + " FROM " + thisTable;
       // Create list to store the results result
       List<string>[] list = new List<string>[9];

            list[0] = new List<string>();
            list[1] = new List<string>();
            list[2] = new List<string>();
            list[3] = new List<string>();
            list[4] = new List<string>();
            list[5] = new List<string>();
            list[6] = new List<string>();
            list[7] = new List<string>();
            list[8] = new List<string>();

         if (this.OpenConnection() == true) // Open connection 
            {
                MySqlCommand cmd = new MySqlCommand(query, connection); // Create Command
                MySqlDataReader dataReader = cmd.ExecuteReader(); // Create data reader and Execute command

                // Read data and store in list
                // Request data from SQL db
                Console.WriteLine("AQUIRRING DATA FROM SQL SERVER....");
                while (dataReader.Read())
                {
                    if (thisItem == "*" && (thisTable == "CUSTOMER") || (thisTable == "customer"))
                    {
                        list[0].Add(dataReader["ID"] + "");
                        list[1].Add(dataReader["NAME"] + "");
                        list[2].Add(dataReader["COMPANY"] + "");
                        list[3].Add(dataReader["PLANT"] + "");
                        list[4].Add(dataReader["CITY"] + "");
                        list[5].Add(dataReader["STATE"] + "");
                        list[6].Add(dataReader["CREATED"] + "");
                        list[7].Add(dataReader["VIEWED"] + "");
                        list[8].Add(dataReader["MODIFIED"] + "");

                    }
                    else
                    {
                        list[0].Add(dataReader[thisItem] + "");
                    }
                }    

                dataReader.Close();

                this.CloseConnection(); //Close Connection
                return list[0];         // Return list to be used
            }
            else
            {
                return list[0];         // Return non-updated list if connection failed
            }

When I call this function (As follows:)

public static List<string>[] queryList = new List<string>[9];
queryList = sqlConn.clientList();   // Return client name list

However, when I do this, instead of getting the string data in the MySQL database, I instead return :

System.Collections.Generic.List`1[System.String]

Can someone help with this, most likely, very simple issue?

Was it helpful?

Solution

Best to use a class with data properties to hold your information. (Wrote this in Notepad so please forgive any mistakes)

public class Whatever
{
    public List<Company> clientList()  // Select from customer table in MySQL db
    {
        string query = "SELECT " + thisItem + " FROM " + thisTable;
        // Create list to store the results result
        List<Company> list = new List<Company>();
        if (this.OpenConnection() == true) // Open connection 
        {
            MySqlCommand cmd = new MySqlCommand(query, connection); // Create Command
            MySqlDataReader dataReader = cmd.ExecuteReader(); // Create data reader and Execute command

            // Read data and store in list
            // Request data from SQL db
            Console.WriteLine("AQUIRRING DATA FROM SQL SERVER....");
            while (dataReader.Read())
            {
                if (thisItem == "*" && (thisTable == "CUSTOMER") || (thisTable == "customer"))
                {
                    Company newCompany = new Company();

                    newCompany.ID = dataReader["ID"].ToString();
                    newCompany.Name = dataReader["NAME"].ToString();
                    ...
                    ...
                    ... (You get the jist of it.)

                    list.add(newCompany);
                }

            }    

            dataReader.Close();

            this.CloseConnection(); //Close Connection
            return list;       // Return list to be used
        }
        return list;
    }
}

public class Company
{
    ID {get;set;}
    NAME {get;set;}
    COMPANY {get;set;}  
    PLANT {get;set;}
    CITY {get;set;}
    STATE {get;set;}
    CREATED {get;set;}
    VIEWED  {get;set;}
    MODIFIED {get;set;}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top