Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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;

            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 8-Jan-13 23:32pm
Edited 8-Jan-13 23:35pm
digimanus30.2K
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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);
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
 
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();
}
  Permalink  
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 693
1 George Jonsson 345
2 BillWoodruff 337
3 PIEBALDconsult 280
4 Sergey Alexandrovich Kryukov 244
0 Sergey Alexandrovich Kryukov 10,744
1 Maciej Los 5,474
2 OriginalGriff 5,352
3 CPallini 3,819
4 BillWoodruff 3,556


Advertise | Privacy | Mobile
Web03 | 2.8.141030.1 | Last Updated 9 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100