0

Hi ,

Am currently working on online examination web project.. i have a table like this..

sno question answer1 answer2 answer3 answer4 correctanswer

1 sql structured simple short none structured

i just want to bind these 4 answers to my radiobuttonlist which is present inside the gridview.. am not able to do that so please help me ....

here is what i've tried ,
Aspx code :

<asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderText=" Online Test !">
                    <ItemTemplate>
                       
                        <asp:Label ID="lbl_que" runat="server" Text='<%# Eval("questions") %>'
                            Height="16px" Width="800px"
                            Font-Size="X-Small"></asp:Label>
                        <br />
                        <asp:RadioButtonList ID="RadioButtonList6"  runat="server" Font-Size="X-Small"
                            RepeatDirection="Horizontal"   >
                           
                        </asp:RadioButtonList>
                      
                    </ItemTemplate>
                    <EditItemTemplate>
                    <asp:TextBox ID="txt_que_edit" runat="server" Text='<%# Bind("questions") %>'
                            Width="187px"></asp:TextBox>
                    </EditItemTemplate>
                  

<ItemStyle HorizontalAlign="Left"></ItemStyle>
                </asp:TemplateField>

C# code :

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
       
            myConnection.Open();
            cmd = new SqlCommand("select option1 , option2 , option3 , option4 from online where pn_BranchID = '" + employee.BranchId + "' and pn_questID='" + txt_qsetno.Text + "' ", myConnection);
            rea1 = cmd.ExecuteReader();
            if (rea1.Read())
            {

                RadioButtonList rbl_responses = (RadioButtonList)e.Row.Cells[1].FindControl("RadioButtonList6");
              

                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    rbl_responses.Items.Add(new ListItem(Convert.ToString(rea1["option1"])));
                    rbl_responses.Items.Add(new ListItem(Convert.ToString(rea1["option2"])));
                    rbl_responses.Items.Add(new ListItem(Convert.ToString(rea1["option3"])));
                    rbl_responses.Items.Add(new ListItem(Convert.ToString(rea1["option4"])));
                }

            rea1.Close();

            myConnection.Close();
        }

But its not working.. i dont know how to bind the 4 answers fields to the radiobuttonlist..

Edited by __avd: Added [code] tags.

2
Contributors
1
Reply
3
Views
6 Years
Discussion Span
Last Post by AhmedSaud
0

Hi ,

Am currently working on online examination web project.. i have a table like this..

sno question answer1 answer2 answer3 answer4 correctanswer

1 sql structured simple short none structured

i just want to bind these 4 answers to my radiobuttonlist which is present inside the gridview.. am not able to do that so please help me ....

here is what i've tried ,
Aspx code :

<asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderText=" Online Test !">
                    <ItemTemplate>
                       
                        <asp:Label ID="lbl_que" runat="server" Text='<%# Eval("questions") %>'
                            Height="16px" Width="800px"
                            Font-Size="X-Small"></asp:Label>
                        <br />
                        <asp:RadioButtonList ID="RadioButtonList6"  runat="server" Font-Size="X-Small"
                            RepeatDirection="Horizontal"   >
                           
                        </asp:RadioButtonList>
                      
                    </ItemTemplate>
                    <EditItemTemplate>
                    <asp:TextBox ID="txt_que_edit" runat="server" Text='<%# Bind("questions") %>'
                            Width="187px"></asp:TextBox>
                    </EditItemTemplate>
                  

<ItemStyle HorizontalAlign="Left"></ItemStyle>
                </asp:TemplateField>

C# code :

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
       
            myConnection.Open();
            cmd = new SqlCommand("select option1 , option2 , option3 , option4 from online where pn_BranchID = '" + employee.BranchId + "' and pn_questID='" + txt_qsetno.Text + "' ", myConnection);
            rea1 = cmd.ExecuteReader();
            if (rea1.Read())
            {

                RadioButtonList rbl_responses = (RadioButtonList)e.Row.Cells[1].FindControl("RadioButtonList6");
              

                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    rbl_responses.Items.Add(new ListItem(Convert.ToString(rea1["option1"])));
                    rbl_responses.Items.Add(new ListItem(Convert.ToString(rea1["option2"])));
                    rbl_responses.Items.Add(new ListItem(Convert.ToString(rea1["option3"])));
                    rbl_responses.Items.Add(new ListItem(Convert.ToString(rea1["option4"])));
                }

            rea1.Close();

            myConnection.Close();
        }

But its not working.. i dont know how to bind the 4 answers fields to the radiobuttonlist..

Did you try dataTable like

SqlDataAdapter da;
DataTable dt=new DataTable();

myConnection.Open();

cmd = new SqlCommand("select option1 , option2 , option3 , option4 from online where pn_BranchID = '" + employee.BranchId + "' and pn_questID='" + txt_qsetno.Text + "' ", myConnection);

da=new SqlDataAdapter(cmd);
da.Fill(dt);//Simple Isn't It

myConnection.Close();

RadioButtonList.DataSource = dt;
RadioButtonList.DataBind();

Try this

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.