Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting Data from a table of unknown Type

Status
Not open for further replies.

EnemyGateIsDown

Technical User
Oct 3, 2002
235
GB
Hi guys,

I have a loop below that reads some fields from a table. The problem is I know the name of the field (from web.config) but I wont know its type, I just want to cast everything to a string. is there a method I can use that will cast the data to a string regardless of type (the bit in bold)?

Any help is as always greatly appreciated.

Chris

Code:
			SqlCommand cmd = new SqlCommand(VisitorSQL,dbCon);
			SqlDataReader sdr = cmd.ExecuteReader();

.
.
.
			while (sdr.Read())
			{
				//build the row
				for (int dcol=1;dcol<12;dcol++)
				{
				field = ParseFieldEntry("ListField"+dcol,"Field");
					if (field.Trim() != "")
					{
						int fieldnum = sdr.GetOrdinal(field);
						
[b]datacell.InnerText = sdr.GetString(fieldnum); [/b]
						datarow.Cells.Add (datacell);
					}
				}
				tblVisList.Rows.Add(datarow);
				Viscount++;
			}
 
I would just do

Code:
datacell.InnerText = sdr.GetValue(fieldnum).ToString();

If I understand correctly, that should do what you want.

----------------------------------------

TWljcm8kb2Z0J3MgIzEgRmFuIQ==
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top