Click here to Skip to main content
15,919,931 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Instead of using every time if (i==3) then {radiobuttionlist3 is selected } how to standize that for the loop something like radiobuttionlist[i] ???

C#
for (int i = 1; i <= 25; i++)
                {

                    SqlCommand cmd1 = new SqlCommand("select ans1,ans2,ans3,ans4,cans from     questions where tname='Prelim' and questionno='" + i + "'", con);
                    SqlDataAdapter daa = new SqlDataAdapter(cmd1);
                    DataSet dss = new DataSet();
                    daa.Fill(dss, "questions");

                    DataRow DRR = dss.Tables[0].Rows[0];

                    if (i == 1)
                    {
                        RadioButtonList1.Items.Clear();
                        RadioButtonList1.Items.Add(DRR[0].ToString());
                        RadioButtonList1.Items.Add(DRR[1].ToString());
                        RadioButtonList1.Items.Add(DRR[2].ToString());
                        RadioButtonList1.Items.Add(DRR[3].ToString());
                    }
                    else if (i == 2)
                    {
                        RadioButtonList2.Items.Clear();
                        RadioButtonList2.Items.Add(DRR[0].ToString());
                        RadioButtonList2.Items.Add(DRR[1].ToString());
                        RadioButtonList2.Items.Add(DRR[2].ToString());
                        RadioButtonList2.Items.Add(DRR[3].ToString());
                    }
                    else if (i == 3)
                    {
                        RadioButtonList3.Items.Clear();
                        RadioButtonList3.Items.Add(DRR[0].ToString());
                        RadioButtonList3.Items.Add(DRR[1].ToString());
                        RadioButtonList3.Items.Add(DRR[2].ToString());
                        RadioButtonList3.Items.Add(DRR[3].ToString());
.....


}


Instead of using every time if (i==3) then {radiobuttionlist3 is selected } how to standize that for the loop something like radiobuttionlist[i] ???

[Edit]Code block added[/Edit]
Posted
Updated 16-Mar-13 7:39am
v2

1 solution

There can be various way to standardize this code..

my suggestion would be:

1. Write a function which will populate the RadioButton list.

C#
public void FillRadioButton(RadioButtonList rbList, DataRow dr)
{
    rbList.Items.Clear();
    rbList.Items.Add(dr[0].ToString());
    rbList.Items.Add(dr[1].ToString());
    rbList.Items.Add(dr[2].ToString());
    rbList.Items.Add(dr[3].ToString());
}



2. Update the foreach loop to fill the invoke this method for all the radiobutton list

C#
for (int i = 1; i <= 25; i++)
            {

                SqlCommand cmd1 = new SqlCommand("select ans1,ans2,ans3,ans4,cans from questions where tname='Prelim' and questionno='" + i + "'", con);
                SqlDataAdapter daa = new SqlDataAdapter(cmd1);
                DataSet dss = new DataSet();
                daa.Fill(dss, "questions");

                DataRow DRR = dss.Tables[0].Rows[0];

                RadioButtonList rbList = this.FindControl("RadioButtonList" + i.ToString()) as RadioButtonList;
                FillRadioButton(rbList, DRR);
            }



Hope this helps....
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900