I have data access class which have List<> type method. In which data come from SQL server perfectly but I need to show it in my ASP GridView through iterate List<> data. This is my Data Access Layer.
public class AdminPanelDataAccessLayer
{
public List<PersonalInformation> GetAllPersonalInfo()
{
List<PersonalInformation> listsPersonalInfo = new List<PersonalInformation>();
string connString = ConfigurationManager.ConnectionStrings["HRMS"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connString))
{
SqlCommand cmd = new SqlCommand("usp_GetApplicantCNICAndName", conn);
cmd.CommandType = CommandType.StoredProcedure;
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
PersonalInformation pi = new PersonalInformation();
pi.CNIC = reader["CNIC"].ToString();
pi.Name = reader["Name"].ToString();
listsPersonalInfo.Add(pi);
}
return listsPersonalInfo;
}
}
}
And this is the method to show data in GridView through iteration.
private void GetAllData()
{
AdminPanelDataAccessLayer apdal = new AdminPanelDataAccessLayer();
List<PersonalInformation> pi = apdal.GetAllPersonalInfo();
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("CNIC", typeof(string)));
dt.Columns.Add(new DataColumn("Name", typeof(string)));
foreach (object item in pi)
{
dr = dt.NewRow();
dr["CNIC"] = pi.Select(o => o.CNIC);
dr["Name"] = pi.Select(o => o.Name);
dt.Rows.Add(dr);
}
ViewState["CurrentTable"] = dt;
GridView2.DataSource = dt;
GridView2.DataBind();
}
Please Guide me to achieve my objective.
` – Aashir Haque Oct 18 '17 at 18:46