I tried to convert a List into a DataTable but the DataTable wont store the item inside the List.
C#
protected void RetrieveGL()
{
List<Retrieve> retrieveList = this.GetGLType();
DataTable dataTable = ConvertRetrieveListToDataTable(retrieveList);
GridViewSelection.DataSource = dataTable;
GridViewSelection.DataBind();
}
private List<Retrieve> GetGLType()
{
string QueryGLType = "SELECT * FROM BOS_M_TRADER_GL WHERE TDR_CODE = :TDR_CODE";
using (OracleCommand cmd = new OracleCommand(QueryGLType, con))
{
cmd.Parameters.Add("TDR_CODE", OracleDbType.Varchar2).Value = DropDownListTDRCode.SelectedValue;
using (OracleDataReader odr = cmd.ExecuteReader())
{
while (odr.Read())
{
retrieve.Add(new Retrieve
{
GLType = odr["GL_TYPE"].ToString(),
GLAccNo = odr["GL_ACCNO"].ToString()
});
}
}
return retrieve;
}
}
private DataTable ConvertRetrieveListToDataTable(List<Retrieve> retrieveList)
{
DataTable dataTable = new DataTable();
dataTable.Columns.Add("GLType", typeof(string));
dataTable.Columns.Add("GLAccNo", typeof(string));
foreach (Retrieve item in retrieveList)
{
DataRow row = dataTable.NewRow();
row["GLType"] = item.GLType;
row["GLAccNo"] = item.GLAccNo;
dataTable.Rows.Add(row);
}
return dataTable;
}
I managed to fetch the data from database and store it inside a list, and then from the list, I tried to store it inside the DataTable. It managed to get into
row["GLType"] = item.GLType;
row["GLAccNo"] = item.GLAccNo;