you have another option with datatable,
DataTable dt = new DataTable();
try
{
Con_Open();
cmd = new SqlCommand("select * from Stu", con);
dr = cmd.ExecuteReader();
if (dr.HasRows)
{
dt.Columns.Add(new DataColumn("Reg No"));
dt.Columns.Add(new DataColumn("Name"));
dt.Columns.Add(new DataColumn("Mark1"));
dt.Columns.Add(new DataColumn("Mark2"));
dt.Columns.Add(new DataColumn("Mark3"));
while (dr.Read())
{
DataRow temp;
temp = dt.NewRow();
temp[0] = (System.Convert.IsDBNull(dr[0]) ? "" : dr[0].ToString());
temp[1] = (System.Convert.IsDBNull(dr[1]) ? "" : dr[1].ToString());
temp[2] = (System.Convert.IsDBNull(dr[2]) ? "" : dr[2].ToString());
temp[3] = (System.Convert.IsDBNull(dr[3]) ? "" : dr[3].ToString());
temp[4] = (System.Convert.IsDBNull(dr[4]) ? "" : dr[4].ToString());
dt.Rows.Add(temp);
}
cmd.Dispose();
dr.Close();
con.Close();
}
else
{
dt.Columns.Add(new DataColumn("Records"));
DataRow temp;
temp = dt.NewRow();
temp[0] = "No Records Found!!";
dt.Rows.Add(temp);
}
}
regards
sarva