Home > DeveloperSection > Forums > how use radio button in asp.net
Pawan kumar
Pawan kumar

Total Post:19

Points:133
Posted on    March-15-2011 9:28 AM

 ASP.Net ASP.Net 
Ratings:


 1 Reply(s)
 9034  View(s)
Rate this:
sir i want to use radiobutton in .aspx page. and i want to save checked radiobutton data in table.
example-i have take a field like  Sex-(1-Male,2-Female),then i want to saved data in table which is checked


Awadhendra Tiwari

Total Post:126

Points:882
Posted on    March-15-2011 9:46 AM

Write down following code on aspx page
<div>
        <asp:RadioButton ID="rdbMale" GroupName="Gender" Text="Male" runat="server" />
        <asp:RadioButton ID="rdbFemale" GroupName="Gender" Text="Female" runat="server" />
        <asp:Button ID="btnSave" Text="Save Radio Button State" runat="server" OnClick="btn_Click" />
</div>

Write down following code on code behind file.

protected void btn_Click(object sender, EventArgs e)
    {
        string gender = "";
        if (rdbMale.Checked)
            gender = rdbMale.Text;
        else
            gender = rdbFemale.Text;
        using (SqlConnection con = new SqlConnection("conString"))
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("insert into gender values(@gender)", con);
            cmd.Parameters.Add(new SqlParameter("@gender", gender));
            cmd.ExecuteNonQuery();
            con.Close();
            Response.Write("<script>alert('Status Saved')</script>");
        }
    }
This might be worked for you.

Don't want to miss updates? Please click the below button!

Follow MindStick