Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Database as follows;


SNO int
Faculty code varchar
Faculty Name varchar
Mobile varchar

in the data base records are there.

column to be retrieved from the data base in the data grid view in the run time.

in the form load code as follows;

C#
            con.Open();
SqlCommand cmd = new SqlCommand("select  Faculty_Name from Tb_SCH_Faculty_Details", con);
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                datagridView.Columns[2].HeaderText = dr[2].ToString().Trim();
                datagridView.Columns[3].HeaderText = dr[2].ToString().Trim();
                datagridView.Columns[4].HeaderText = dr[2].ToString().Trim();
                datagridView.Columns[5].HeaderText = dr[2].ToString().Trim();
                datagridView.Columns[6].HeaderText = dr[2].ToString().Trim();
                dr.Close();
                con.Close();
            }

when i run message shows index was outside the bound of array.


please help me. i want to use the data reader through data reader i get the Faculty Name from the data base.
Posted
Updated 8-Jan-13 23:35pm
v2

SqlCommand cmd = new SqlCommand("select Faculty_Name from Tb_SCH_Faculty_Details", con); Here you are get only one column value "Faculty_Name". so we can use dr[0]


(or)

You must change the query

SqlCommand cmd = new SqlCommand("select SNO,Faculty_code, Faculty_Name from Tb_SCH_Faculty_Details", con);
 
Share this answer
 
v2
Hi,

In your select statement your calling only one parameter.
So, whenever you call the SQLDataReader object you mentioned the possition is properly.

ex:

your calling something like this dr[2], so please be avoid this
use dr[0].

index was outside the bound of array
this error meaning is in your datareader u have only dr[0] position only but you call dr[2].So, you cross the limit that possition that's the reason to cause this error.



I think this might be help for u
 
Share this answer
 
Set dr.Close() and con.Close(); below the end bracket }. Now after 1 row you close the connection while trying to get a second row.
Furthermore all Columns get the same headertext. dr[2] should be dr[1] (start counting with 0 (c-principle))
And read about learning to use USING

C#
using (SqlConnection con = ...)
{  
   con.Open();
   using (SqlCommand cmd = new SqlCommand("select SNO, Faculty_Name from Tb_SCH_Faculty_Details", con))
     {
        using (SqlDataReader dr = cmd.ExecuteReader())
        {
            while (dr.Read())
            {
                datagridView.Columns[2].HeaderText = dr[2].ToString().Trim();
                datagridView.Columns[3].HeaderText = dr[2].ToString().Trim();
                datagridView.Columns[4].HeaderText = dr[2].ToString().Trim();
                datagridView.Columns[5].HeaderText = dr[2].ToString().Trim();
                datagridView.Columns[6].HeaderText = dr[2].ToString().Trim();
                
            }
         }
         dr.Close();
     }
     con.Close();
}
 
Share this answer
 
v3

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