I have an aspx form with controls along the lines of:
<asp:TextBox ID="FirstName" runat="server" />
<asp:TextBox ID="LastName" runat="server" />
<asp:TextBox ID="Age" runat="server" />
In the .cs I can set the .Text property as follows:
FirstName.Text = "Gary";
LastName.Text = "King";
Age.Text = "40";
Simple! BUT.....
The values are being retrieved from a SQL database table that looks like this:
ID | FirstName | LastName | Age |
1 | Gary | King | 40 |
2 | Bob | Smith | 36 |
What I
want to do is read a row into a DataReader and then iterate through the columns in the DataReader and use the information in the code like this:
for (int i = 1; i < dr.FieldCount; i++)
{
// use the column name to determine which form control (textbox) .Text property to set
dr.GetName(i).Text = dr.GetValue(i); // -- This is the bit that I am struggling with!
}