Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi i have a method named no()

in that method i am retrieving the qno from database I have to place those numbers in array of integer type.
C#
public void no()
{
    SqlCommand cmd = new SqlCommand("select qno from QUIZ1 where applicationname = @appname", con);
    cmd.Parameters.AddWithValue("@appname", this.application);
    con.Open();
    ArrayList al = new ArrayList();
    SqlDataReader sdr = cmd.ExecuteReader();
    while (sdr.Read())
    {
        object[] values = new object[sdr.FieldCount];
        sdr.GetValues(values);
        al.Add(values);
    }
            
   this.str = ((int[])al.ToArray(typeof(int)));
}


i am getting the following error

At least one element in the source array could not be cast down to the destination array type.

can any one pls help me ?
Posted
v2

From the error, it is clear that the ArrayList contains data which are not of the same DataType.

Please debug and check whether all the values in the ArrayList are Integer or not.
 
Share this answer
 
Comments
_Amy 4-Jul-13 11:03am    
+5!
Thanks a lot _Amy... :)
Something in your array list can't be cast to an integer, probably a null value or something like that. Put a breakpoint at the this.str =... line and see what the array list is filled with, if you see anything that doesn't look like it can be cast to an integer (null, odd string, etc), then that's your problem.
 
Share this answer
 
Please go through the Link[^]. It has detailed explanation about your problem.
 
Share this answer
 

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