Click here to Skip to main content
15,904,153 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi,

Basically I have read in an entire column from my database and stored it as an ArrayList 'al'. To prove this works i've got it displayed in a list box wich is good.

However my main aim is to convert the ArrayList into a Int Array so that I can display that information on the chart I also have on the form.

I currently get the error "At least one element in the source array could not be cast down to the destination array type."

Below is my code:
private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("Server=MASTER;DataBase=iTylerHood;Integrated Security=SSPI");    
            SqlCommand cmd = new SqlCommand("CustomerByCountry",con);
            cmd.CommandType = CommandType.StoredProcedure;
            con.Open();
        
            ArrayList al = new ArrayList();
            SqlDataReader dr = cmd.ExecuteReader();
        
            while(dr.Read()) 
            {
                    object[] values = new object[dr.FieldCount];
                    dr.GetValues(values);
                    al.Add(values);
            }        
            dr.Close();
            con.Close();
        
            foreach(object[] row in al) {
            foreach(object column in row) 
            {
              listBox1.Items.Add(column.ToString());
            }
            }

            //------ Convert to Int Array

            int[] ia = (int[])al.ToArray(typeof(int));
            int sum = 0;

            for (int i = 0; i < ia.Length; i++)
            sum += ia[i];


            //------- Display on Chart

            string[] seriesArray = { "Fish", "Sharks" };
                            
            this.chart1.Palette = ChartColorPalette.SeaGreen;
                          
            this.chart1.Titles.Add("Pets");
                            
            for (int i = 0; i < seriesArray.Length; i++)
            {
                Series series = this.chart1.Series.Add(seriesArray[i]);

                series.Points.Add(ia[i]);
            }

        }



I think the problem may lie with the conversion of the Array list to an Int Array. The information from the database is integers of 6000, 7000 and 8000.
Any help would be most appreciated.
Posted
Updated 14-Jan-12 7:48am
v2
Comments
Sergey Alexandrovich Kryukov 15-Jan-12 0:21am    
What part of Internet was broken when you were asking this question: MSDN? Google? what? :-)
--SA

1 solution

First of all, there is not need to use the type ArrayList anymore. Non-generic collection types were rendered obsolete with .NET Framework 2.0. Instead, use generic type System.Collections.Generic.List<>. Non-generic collection types are not marked deprecated though. They can still be used, but mostly to keep legacy software working. But they are bad as they require type casting. For new development, always use generic types.

Now, in all cases, list classes has the method ToArray, just use it.

—SA
 
Share this answer
 
Comments
WurmInfinity 15-Jan-12 5:11am    
The problem is I need to pass the data through a WCF component. Its all well and good just reading the data straight in but how do I pass it through. Oh well I've decided to give up on it and try something else thanks for your time.

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