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.
Loading
chellappan pandiarajanPosted Jan 9, 2013, 6:38 AM
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
chellappan pandiarajanPosted Jan 9, 2013, 6:33 AM
"select Faculty_Name from Tb_SCH_Faculty_Details"
So you replace dr[2].ToString().Trim(); to dr[0].ToString().Trim();
It will work
-Pandian