locked
Create a method to retrieve values from database and return an array with all the values retrieved in C# windows form application RRS feed

  • Question

  • I wrote a method in my C# windows form application to retrieve data from an access database.

    And I want this method to return an array to store all the values I retrieved.

    Here is the code I wrote and there is an error.

            private Array GetYvalue(float[] arr)
            {
                int cont = 0;
                // CONNECT TO A DATABASE
                OleDbConnection Con = new OleDbConnection();
                Con.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\yorks\Documents\Database1.accdb";
                string query = "SELECT [Date/Time] AS DATA, ROUND(AVG(CurrentEnergy),2) AS AVGCUR FROM ENERGYSYSTEM GROUP BY [Date/Time]";
                SelectData [] arr = null;
                using (var command = new OleDbCommand(query, Con))
                {
                    Con.Open();
                    using (var reader = command.ExecuteReader())
                    {
                        var list = new List<SelectData>();
                        while (reader.Read())
                            list.Add(new SelectData { Time = reader.GetDateTime(0), Yvalue = reader.GetFloat(1) });
                        arr = list.ToArray();
                    }             //another way to get y values
    
                   
                    return arr;
                }
    
       public class SelectData
        {
            public DateTime Time { get; set; }
            public float Yvalue { get; set; }
    
        }

    Can someone help to fix it?

    Friday, January 20, 2017 1:51 PM

Answers

  • Hi,

    It seems that the variable  "arr" has same name with parameter of GetYvalue method.

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by York Gong Saturday, January 21, 2017 8:53 PM
    Friday, January 20, 2017 3:54 PM