Click here to Skip to main content
15,886,734 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using asp.net repeater in the web application in that i am having four radio buttons and also fetching the data from the database to the repeater and radio buttons as well. Actual my problem is I am designing online examination site for which i am getting questions and options from the database but only thing when submitting the form it is considering first question fine but when comes to second question it is taking only first questions to second question and so on. How to solve this problem... Please suggest me where i am doing wrong.

What I have tried:

<div>
    <asp:Repeater ID="QuestionRepeater" runat="server">
        <HeaderTemplate>
            <table>
                <tr>
                    <td>Online Examination</td>
                </tr>
        </HeaderTemplate>
        <SeparatorTemplate>
                <tr>
                    <td>
                        <br />
                    </td>
                </tr>
        </SeparatorTemplate>
        <ItemTemplate>
                <tr>
                    <td>
                        <%#Eval("QuestionNumber") %>
                        <%#Eval("Question") %>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:RadioButton runat="server" ID="rb1" GroupName="Rb_Choice" OnCheckedChanged="CheckChanged" Text='<%# Bind("ChoiceA") %>'></asp:RadioButton>
                    </td>
                    <td>
                        <asp:RadioButton runat="server" ID="rb2" GroupName="Rb_Choice" OnCheckedChanged="CheckChanged" Text='<%# Bind("ChoiceB") %>'></asp:RadioButton>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:RadioButton runat="server" ID="rb3" GroupName="Rb_Choice" OnCheckedChanged="CheckChanged" Text='<%# Bind("ChoiceC") %>'></asp:RadioButton>
                    </td>
                    <td>
                        <asp:RadioButton runat="server" ID="rb4" GroupName="Rb_Choice" OnCheckedChanged="CheckChanged" Text='<%# Bind("ChoiceD") %>'></asp:RadioButton>
                    </td>
                 </tr>
        </ItemTemplate>
        <FooterTemplate>
            </table>
        </FooterTemplate>
    </asp:Repeater>
</div>


CodeBehind:
foreach (RepeaterItem rp in QuestionRepeater.Items)
{
    while (dr.Read())
    {
        if (rp.ItemType == ListItemType.Item || rp.ItemType == ListItemType.AlternatingItem)
        {
            a.QuestionNumber = Convert.ToInt32(dr["QuestionNumber"]);
            var rb1 = rp.FindControl("rb1") as RadioButton;
            var rb2 = rp.FindControl("rb2") as RadioButton;
            var rb3 = rp.FindControl("rb3") as RadioButton;
            var rb4 = rp.FindControl("rb4") as RadioButton;
        }
    }
}
Posted
Updated 14-Oct-18 20:49pm
Comments
Member 8583441 15-Oct-18 2:12am    
Here i am displaying all questions in one page itself

1 solution

I solved my problem... A simple mistake by me that is to interchange foreach loop and while loop.

Previously foreach loop is written first and then while loop to read the data from the database.

foreach (RepeaterItem rp in QuestionRepeater.Items)
{
    while (dr.Read())
    {
        if (rp.ItemType == ListItemType.Item || rp.ItemType == ListItemType.AlternatingItem)
        {
            a.QuestionNumber = Convert.ToInt32(dr["QuestionNumber"]);
            var rb1 = rp.FindControl("rb1") as RadioButton;
            var rb2 = rp.FindControl("rb2") as RadioButton;
            var rb3 = rp.FindControl("rb3") as RadioButton;
            var rb4 = rp.FindControl("rb4") as RadioButton;
        }
    }
}


For a sample i just changed the order while loop top and foreach loop down that means reading from the database and then getting questions and options in correct way

while (dr.Read())
{
    foreach (RepeaterItem rp in QuestionRepeater.Items)
    {
        if (rp.ItemType == ListItemType.Item || rp.ItemType == ListItemType.AlternatingItem)
        {
            a.QuestionNumber = Convert.ToInt32(dr["QuestionNumber"]);
            var rb1 = rp.FindControl("rb1") as RadioButton;
            var rb2 = rp.FindControl("rb2") as RadioButton;
            var rb3 = rp.FindControl("rb3") as RadioButton;
            var rb4 = rp.FindControl("rb4") as RadioButton;
        }
    }
}
 
Share this answer
 
Comments
Member 8583441 15-Oct-18 4:09am    
with this solution i am getting problem with next record. Next record not fetching the data. How to solve it
CHill60 15-Oct-18 4:12am    
I notice that you are asking lots of questions which you then answer yourself. That behaviour is a little suspicious and looks like reputation farming. You also only accept your own answer even though other members have attempted to help.
Perhaps if you tried a little harder to solve your problems before posting them on here you might save yourself some time ?
Member 8583441 15-Oct-18 6:40am    
i am really sorry for that sir... I am sincerely trying for the solution and then asking the question in this site. Later on I am just trying to change the code as required and i am getting the output. I am really sorry for this and i make sure i never repeat this forgive me sir
CHill60 15-Oct-18 7:57am    
Don't worry too much, I was just letting you know :-)

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