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.
Column to be retrieved from the data base in the DGV
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.

chellappan pandiarajan79 reputationPosted Jan 9, 2013, 1:33 AM EST
"select Faculty_Name from Tb_SCH_Faculty_Details"
So you replace dr[2].ToString().Trim(); to dr[0].ToString().Trim();
It will work
-Pandian
chellappan pandiarajan79 reputationPosted Jan 9, 2013, 1:38 AM EST
SqlCommand cmd = new SqlCommand("select Faculty_Name from Tb_SCH_Faculty_Details", con);
SqlDataReader dr = cmd.ExecuteReader();
int i =2;
while (dr.Read())
{
datagridView.Columns[i].HeaderText = dr[0].ToString().Trim();
i= i +1;
if(i == 6)
break;
}
dr.Close();
con.Close();
Try this
-Pandian