3
Answers

radio button problem

praveen maurya

praveen maurya

12y
1.2k
1
hello there 

i am working on a project online examination system my all coding is good but i have a problem with my radio buttons when i checked radio button the ans save to database but when i am pressing the next button the the radio button is always checked. my coading is 

    SqlConnection cn = new SqlConnection("Data Source=.;Persist Security Info=False;Initial Catalog=admins;User ID=sa;Password=123456");
    DataSet ds = new DataSet();
    
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            cn.Open();
            SqlDataAdapter da = new SqlDataAdapter("select * from ques", cn);
            DataSet ds = new DataSet();
            da.Fill(ds, "ques");
            ViewState["i"] = 0;
            Label2.Text = ds.Tables[0].Rows[0]["quesno"].ToString();
            Label1.Text = ds.Tables[0].Rows[0]["ques"].ToString();
            RadioButton1.Text = ds.Tables[0].Rows[0]["ans1"].ToString();
            RadioButton2.Text = ds.Tables[0].Rows[0]["ans2"].ToString();
            RadioButton3.Text = ds.Tables[0].Rows[0]["ans3"].ToString();
            RadioButton4.Text = ds.Tables[0].Rows[0]["ans4"].ToString();
            cn.Close();
        }

        
    }

   
    protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
    {
        SqlCommand cmd = new SqlCommand("update uans set ans=@ans where ansno=@ansno", cn);
        cn.Open();
        cmd.Parameters.Add(new SqlParameter("@ans", RadioButton1.Text));
        cmd.Parameters.Add(new SqlParameter("@ansno", Label2.Text));
        cmd.ExecuteNonQuery();
        
    }
    protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
    {
        SqlCommand cmd = new SqlCommand("update uans set ans=@ans where ansno=@ansno", cn);
        cn.Open();
        cmd.Parameters.Add(new SqlParameter("@ans", RadioButton2.Text));
        cmd.Parameters.Add(new SqlParameter("@ansno", Label2.Text));
        cmd.ExecuteNonQuery();
    }
    protected void RadioButton3_CheckedChanged(object sender, EventArgs e)
    {
        SqlCommand cmd = new SqlCommand("update uans set ans=@ans where ansno=@ansno", cn);
        cn.Open();
        cmd.Parameters.Add(new SqlParameter("@ans", RadioButton3.Text));
        cmd.Parameters.Add(new SqlParameter("@ansno", Label2.Text));
        cmd.ExecuteNonQuery();
    }
    protected void RadioButton4_CheckedChanged(object sender, EventArgs e)
    {
        SqlCommand cmd = new SqlCommand("update uans set ans=@ans where ansno=@ansno", cn);
        cn.Open();
        cmd.Parameters.Add(new SqlParameter("@ans", RadioButton4.Text));
        cmd.Parameters.Add(new SqlParameter("@ansno", Label2.Text));
        cmd.ExecuteNonQuery();
    }

    protected void Button3_Click(object sender, EventArgs e)
    {
        
        int i = Convert.ToInt32(ViewState["i"]) + 1;
        SqlDataAdapter da = new SqlDataAdapter("select * from ques", cn);
        da.Fill(ds, "ques");
        ViewState["i"] = (int)ViewState["i"] + 1;
        if (ds != null && (int)ViewState["i"] < ds.Tables[0].Rows.Count)
        {
            Label2.Text = ds.Tables[0].Rows[i]["quesno"].ToString();
            Label1.Text = ds.Tables[0].Rows[i]["ques"].ToString();
            RadioButton1.Text = ds.Tables[0].Rows[i]["ans1"].ToString();
            RadioButton2.Text = ds.Tables[0].Rows[i]["ans2"].ToString();
            RadioButton3.Text = ds.Tables[0].Rows[i]["ans3"].ToString();
            RadioButton4.Text = ds.Tables[0].Rows[i]["ans4"].ToString();
        }
        else
        {
            Response.Redirect("result.aspx");
        }
     
Answers (3)
Next Recommended Forum