Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
<blockquote class="quote"><div class="op">Quote:</div><blockquote class="quote"><div class="op">Quote:</div> try
            {
                if (cn.State.ToString() != "Open")
                    cn.Open();


                for (int i = 0; i < 10; i++)
                {
                    cmd = new SqlCommand("select fahsName from Name_fahs where Number_N='1'  and Number_of_each_Name='"+i+1+"'", cn);
                    dr = cmd.ExecuteReader();
                    dr.Read();
                    dataGridView1.Rows[i].Cells[i].Value = dr["fahsName"].ToString();
                    
                    dr.Close();
                }
               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "jwnjew ");
            }
            finally 
            {<pre lang="c#">

cmd.Cancel();
cn.Close();
}

What I have tried:

C#
<blockquote class="quote"><div class="op">Quote:</div><blockquote class="quote"><div class="op">Quote:</div> try
            {
                if (cn.State.ToString() != "Open")
                    cn.Open();


                for (int i = 0; i < 10; i++)
                {
                    cmd = new SqlCommand("select fahsName from Name_fahs where Number_N='1'  and Number_of_each_Name='"+i+1+"'", cn);
                    dr = cmd.ExecuteReader();
                    dr.Read();
                    dataGridView1.Rows[i].Cells[i].Value = dr["fahsName"].ToString();
                    
                    dr.Close();
                }
               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "jwnjew ");
            }
            finally 
            {<pre lang="c#">

cmd.Cancel();
cn.Close();
}
Posted
Updated 26-Nov-18 5:27am

Look at the error message:
C#
Index was out of range. Must be nonnegative and less than the size of the collection
It couldn;t be a lot clearer if it tried!

You are accessing an array via an index and it is either negative or too big for the array. For example,
C#
int[] m = new int[3];
Can only be access ed as
C#
int i1 = m[0];
int i2 = m[1];
int i3 = m[2];
Any other value will give you this error.

In your code, you are trying to access the DGV at locations
[0,0]
[1,1]
[2,2]
[3,3]
[4,4]
[5,5]
[6,6]
[7,7]
[8,8]
[9,9]
So unless your DGV has at least 10 rows and at least 10 columns, it will fail.
I suspect that you meant to reference a specific column for all the rows, rather than a diagonal.

Why not do a single SqlCommand and with
SQL
... WHERE Number_Name = '1' AND Number_of_each_Name BETWEEN 0 AND 9
instead of doing multiple round trips to the server?
 
Share this answer
 
v2
Comments
Richard Deeming 27-Nov-18 12:12pm    
int[] m = new int[2];

Can only be accessed as
...
int i3 = m[2];


Are you sure about that? :)

new int[2] declares an array of two elements. m[2] will throw.
OriginalGriff 27-Nov-18 12:31pm    
Oops. Fixed!
dataGridView1.Rows[i].Cells[i].Value = dr["fahsName"].ToString();


Check that "i" is valid here. If i is 3 and there are not at least 4 rows then you'll get this error, or if the fourth row doesn't have at least 4 cells you'll get the error. Basically this code will only work if you've already populated your datagridview with enough rows and columns. If you're looking to build the grid as you go then things don't come into existence as you reference them, you'll need to explicitly create them. So google how to add rows and cells to a datagridview.
 
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