"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.

 

Published Thursday, March 30, 2006 4:21 PM by chuawenching

Comments

# re: "I am a bad developer" - Frustrated with DataTable, use DataSet to get values for each row

Thursday, March 30, 2006 9:27 PM by Firedancer
I don't understand, why can't you use dt.Rows?

# re: "I am a bad developer" - Frustrated with DataTable, use DataSet to get values for each row

Thursday, March 30, 2006 11:46 PM by chuawenching
Is it? Hmm... Let me try later, I am dead tired... just look at code way too long until blur already

# re: "I am a bad developer" - Frustrated with DataTable, use DataSet to get values for each row

Friday, March 31, 2006 12:00 AM by chuawenching
Can wor ... lol :( what had I been doing :(

# re: "I am a bad developer" - Frustrated with DataTable, use DataSet to get values for each row

Friday, March 31, 2006 9:35 PM by Firedancer
When things don't work, go to the pantry, make some coffee, look out the window, refresh yourself and then get back to your seat. Then you will be able to things differently. ;)

# Tyrone

Friday, July 25, 2008 10:36 AM by Justen

f2c2437854cdfe924a4c1515e4b15436

<a href="njdokj.info/.../f2c2437854cdfe924a4c1515e4b15436">">njdokj.info/.../f2c2437854cdfe924a4c1515e4b15436"> njdokj.info/.../f2c2437854cdfe924a4c1515e4b15436 </a>

njdokj.info/.../f2c2437854cdfe924a4c1515e4b15436

[url]njdokj.info/.../f2c2437854cdfe924a4c1515e4b15436[url]