Click here to Skip to main content
15,915,328 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Error:object reference is not set to instance of an object at string b. i need to pass radiobutton selected value in my repeater from mock.aspx to mock1.aspx

mock.aspx
___________


ASP.NET
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage1.master" AutoEventWireup="true" CodeFile="mock.aspx.cs" Inherits="mock" %>


<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    


<div>
<asp:Repeater ID="RepDetails" runat="server">
<HeaderTemplate>
<table style=" border:1px solid #df5015; width:500px" cellpadding="0">
<tr style="background-color:#df5015; color:White">
<td colspan="2">
<b>Questions:</b>
</td>
</tr>
</HeaderTemplate>
<itemtemplate>
<tr style="background-color:#EBEFF0">
<td>
<table style="background-color:#EBEFF0;border-removed1px dotted #df5015; width:500px">
<tr>
<td>

    <asp:Label ID="Label1" runat="server" Text='<%#Eval("qno") %>' Font-Bold="true"/>

<asp:Label ID="lblSubject" runat="server" Text='<%#Eval("question") %>' Font-Bold="true"/>
</td>
</tr>
</table>
</td>
</tr>
<tr>

</tr>
<tr>
<td>
<table style="background-color:#EBEFF0;border-removed1px dotted #df5015;border-removed1px solid #df5015; width:500px">
<tr>
<td>
    <asp:RadioButton ID="RadioButton1" groupname="radio" Text='<%#Eval("option1") %>' EnableViewState="false" runat="server"/><br />
        <asp:RadioButton ID="RadioButton2" groupname="radio" Text='<%#Eval("option2") %>' EnableViewState="false" runat="server"/><br />
    <asp:RadioButton ID="RadioButton3" groupname="radio"  Text='<%#Eval("option3") %>' EnableViewState="false" runat="server"/><br />
    <asp:RadioButton ID="RadioButton4" groupname="radio" Text='<%#Eval("option4") %>' EnableViewState="false" runat="server"/>

          </td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="2">&nbsp;</td>
</tr>
</itemtemplate>
<footertemplate>
</footertemplate></table>


</div>
    <div>&lt;asp:Button id="btnnext" runat="server" Text="Next" OnClick="btnnext_Click"/&gt;&lt;asp:Label ID="lblvalue" runat="server"&gt;</div>



mock.aspx.cs
________________

C#
  protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            BindRepeaterData();
        }
    }

protected void BindRepeaterData()
{
    SqlConnection con=new SqlConnection(connectionString);
    
    con.Open();
    SqlCommand cmd = new SqlCommand("select qno,question,option1,option2,option3,option4 from questionstable where qno BETWEEN 3 AND 6", con);
    DataSet ds = new DataSet();
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    da.SelectCommand = cmd;
    da.Fill(ds);
    RepDetails.DataSource = ds;
    RepDetails.DataBind();
    con.Close();
}

protected void btnnext_Click(object sender, EventArgs e)
{
     foreach(RepeaterItem item in RepDetails.Items)             
            {
                RadioButton box = (RadioButton)item.FindControl("radio");
                string b = box.Text;    
         lblvalue.Text=b;  
 
             }   
    Response.Redirect("mock1.aspx?value="+lblvalue.Text);


        
    }



mock1.aspx.cs
_______________

C#
 void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = Request.QueryString["value"].ToString();
}
Posted
Updated 19-Feb-16 2:26am
v2
Comments
Sreekanth Mothukuru 19-Feb-16 2:10am    
Where exactly at what line you got this error. You are getting null value at Request.QueryString in page load or item.FindControl("radio") button click btnnext_Click events..
Richard Deeming 19-Feb-16 7:57am    
You're trying to find a RadioButton with the ID set to "radio". There is no such control in the markup you have provided.

1 solution

As Richard pointed out you are trying to find a control named "radio" in each of your repeater items. However, your code does not make any sense. It loops through each repeater item overwriting lblvalue.Text each time. So, what will end up happening, if you fix the "radio" part is that you'll put the last value in lblvalue and then redirect.

You need to decide which of your repeater items you actually want when you click the next button. Then write code for that.
 
Share this answer
 
Comments
Member 12306844 19-Feb-16 8:52am    
later i tried this...

protected void btnnext_Click(object sender, EventArgs e)
{
foreach(RepeaterItem item in RepDetails.Items)
{
int a, b, c, d; ;
RadioButton box1 = (RadioButton)item.FindControl("RadioButton1");
RadioButton box2 = (RadioButton)item.FindControl("RadioButton2");
RadioButton box3 = (RadioButton)item.FindControl("RadioButton3");
RadioButton box4 = (RadioButton)item.FindControl("RadioButton4");
if (box1.Checked)
{
a = 1;
b = c = d = 0;
}
if (box2.Checked)
{
b = 1;
a = c = d = 0;
}
if (box3.Checked)
{
c = 1;
a = b = d = 0;
}
if (box4.Checked)
{
d = 1;
a = c = b = 0;
}

lblvalue.Text = a.ToString;
Label2.Text = b.ToString();
Label3.Text = c.ToString();
Label4.Text = d.ToString();



}
Response.Redirect("mock1.aspx?value=" + lblvalue.Text+"&value1="+Label2.Text+"&value2="+Label3.Text+"&value3="+Label4.Text);


}
Member 12306844 19-Feb-16 8:55am    
i need which option the user selected so that i will make how many of them are correct in the other page to provide result to them...
ZurdoDev 19-Feb-16 8:59am    
OK, in that case do what you're doing, loop through the items and find if it is checked.

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