Click here to Skip to main content
15,921,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

The above error is seen in my code give below.

Can any one help me?
C#
SqlCommand CMD = new SqlCommand("select productname from addproduct", con);
            dr = CMD.ExecuteReader();
            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    DropDownList drp = new DropDownList();
                    drp.Items.Add(dr.GetValue(0).ToString());
                    for (int i = 0; i <= drp.Items.Count; i++)
                    {
                        //getting above error at below line 
                        GridView1.Rows[i].Cells[1].Text = dr.GetValue(0).ToString();

Actually I'm trying here to bind data from database to drop down inside the grid view.
Posted
Updated 1-Aug-13 1:41am
v3
Comments
Syed Shabeer 1-Aug-13 7:43am    
drp.Items.Count -1 in "for loop" might work. Please try and let me know

1 solution

Please change your loop like below
C#
for (int i = 0; i < drp.Items.Count; i++)
                    {


Change <= to <

Hope this helps
 
Share this answer
 
Comments
Gautam Raithatha 1-Aug-13 7:59am    
correct..
Jameel VM 1-Aug-13 10:08am    
if it correct.please mark it as solution
Member 9660789 1-Aug-13 10:58am    
thanks for the reply but its not getting. the same error continues
Jameel VM 1-Aug-13 11:17am    
now the same error?

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