"I am a bad developer" - Frustrated with DataTable, use DataSet to get values for each row
I couldn't believe it. I tried my very best to avoid DataSet in ASP.NET 2.0 but I just couldn't. I will store my values within a DataTable, but later I need to add that DataTable into my DataSet. Not only that, it took me so long to solve this problem. Lucky my colleagues hinted me. Quite embarassing. Plus I never think out of the box. (Hint: my gf can solve a problem in 2 minutes, and I take ages)
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text =
HttpContext.Current.User.Identity.Name;
DataTable dt = new DataTable();
// Don't forget to name your DataTable :) It is crucial. You will get some errors stating that
// DataTable name not set.
dt.TableName = "Test";
dt.Columns.Add("Company / Organisation", typeof(string));
dt.Columns.Add("Position Held", typeof(string));
dt.Columns.Add("Representative", typeof(string));
dt.Columns.Add("Date of Appointment", typeof(string));
// 3 rows of dummy values
dt.Rows.Add((new object[] {
"1", "2", "3", "4"}
)
);
dt.Rows.Add((new object[] {
"5", "6", "7", "8"}
)
);
dt.Rows.Add((new object[] {
"9", "10", "11", "12"}
)
);
// Dummy strings
string strValue1 = string.Empty;
string strValue2 = string.Empty;
string strValue3 = string.Empty;
string strValue4 = string.Empty;
DataSet d1 = new DataSet();
d1.Tables.Add(dt);
// I think you can't use DataTable here, if not you will an error stating that DataTable has no GetEnumerator... correct me if I am wrong.
foreach (DataRow dr in d1.Tables["Test"].Rows)
{
strValue1 = dr["Company / Organisation"].ToString();
strValue2 = dr["Position Held"].ToString();
strValue3 = dr["Representative"].ToString();
strValue4 = dr["Date of Appointment"].ToString();
}
}
Yeap. You will get the results you want :) Back to work.