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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

display radio button selection from database

Status
Not open for further replies.

ciberperson

Programmer
Aug 31, 2000
29
CA
I have a form of text boxes and radio button lists in C#. The user submits and all is saved to the SQLServer database. But the user can return and make changes to this form. When the user returns I want to retrieve his previously entered values and display them.

I created a DataReader to retrieve the values from the DB. I have a DataList as a container for my form fields. Displaying the value entered for the radio button lists is not clear to me.

I have searched several forums but I have not found the answer.

Here's a radio button list:
<asp:RadioButtonList ID="rbAge" RepeatDirection="Horizontal"
runat="server">
<asp:ListItem>Under 40</asp:ListItem>
<asp:ListItem>Over 40</asp:ListItem>
</asp:RadioButtonList>

In the .cs file I grab the user id and select his entries using a DataReader. Now how do I bind the results to the form fields and how do I display his selection for the radio button lists?

Thanks for any help - it is frustrating to keep looking; maybe I am using the wrong search?
 
Well no one answered so I guess I explained it poorly. However I have found a way to get the data displayed the way I want and I am posting for the benefit of anyone else with a similar issue. It might be clumsy and I would love to know a better way. But here's what I have in my code behind page:

SqlDataReader readr;
RadioButtonList radioGender = (RadioButtonList)formPage1.FindControl("rbGender");
RadioButtonList radioAge = (RadioButtonList)formPage1.FindControl("rbAge");
RadioButtonList radioEthnic = (RadioButtonList)formPage1.FindControl("rbEthnicity");

cmd = new SqlCommand("dbo.usp_StoredProcedure_GetStuff", conn);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@uid", iUserid);
//open the connection
conn.Open();
readr = cmd.ExecuteReader();

if (readr.HasRows){

// get the results and display in form
readr.Read();
textProject.Text = readr["Project"].ToString();
foreach (ListItem lst in radioGender.Items)
{
if (lst.Value.ToString() == readr["Gender"].ToString())
{
lst.Selected = true;
}
} //end for
foreach (ListItem lst in radioAge.Items)
{
if (lst.Value.ToString() == readr["Age"].ToString())
{
lst.Selected = true;
}
} //end for
foreach (ListItem lst in radioEthnic.Items)
{
if (lst.Value.ToString() == readr["Ethnicity"].ToString())
{
lst.Selected = true;
}
} //end for

} // end if
readr.Close();
} // end if

//close the connection
conn.Close();
cmd.Dispose();

I would be interested in learning a better way so jump in. thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top