Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have 2 radio button for male and female,what will C# code to store in database?
after checking any one button...
Posted
Updated 9-Jan-20 21:07pm

in markup:

ASP.NET
<asp:radiobuttonlist id="myradio1" runat="server">
        <asp:listitem id="option1" runat="server" value="male" />
        <asp:listitem id="option2" runat="server" value="female" />
<asp:radiobuttonlist />


in code behind:

C#
void Page_Load()
  { 
    if (Page.IsPostBack)
     { 
        Message.Text = "You have selected the " + radio1.SelectedItem.Value;
     }
  }


hope it helps :)
 
Share this answer
 
v3
Comments
Abhinav S 29-Aug-11 13:01pm    
Fairly accurate. My 5.
Uday P.Singh 29-Aug-11 14:23pm    
thanks Abhinav :)
Use Bit Data type to store radio button values.
if male radio button is checked then store "1" in database in male column.
when you fecth it back, depends upon values check radio buttons.

C#
if (drReader("rbMale") == "1")
    rbMale.checked = true;
else
   rbFemale.checked = true;
 
Share this answer
 
Comments
Abhinav S 29-Aug-11 13:02pm    
Fair enough. My 5.
Hi sunil,

i am explaining your question through a sample example.
in markup:
XML
<body>
    <form id="form1" runat="server">
    <div>

        <table align="center" class="style1">
            <tr>
                <td colspan="2">
                    <h3>
                        Sample Registraion</h3>
                </td>
            </tr>
            <tr>
                <td class="style3">
                    Name:</td>
                <td>
                    <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style3">
                    Email</td>
                <td>
                    <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style3">
                    Gendere</td>
                <td>
                    <asp:RadioButtonList ID="rbtGender" runat="server" RepeatDirection="Horizontal">
                        <asp:ListItem>Male</asp:ListItem>
                        <asp:ListItem>Female</asp:ListItem>
                    </asp:RadioButtonList>
                </td>
            </tr>
            <tr>
                <td align="center" class="style2" colspan="2">
                    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Submit" />
&nbsp;&nbsp;
                </td>
            </tr>
            <tr>
                <td align="center" class="style2" colspan="2">
                    <asp:Label ID="lblmsg" runat="server"></asp:Label>
                </td>
            </tr>
        </table>

    </div>
    </form>
</body>


in code bind:
C#
protected void Button1_Click(object sender, EventArgs e)
   {
       string cs = ConfigurationManager.ConnectionStrings["srinivasdb"].ConnectionString;
       SqlConnection cn = new SqlConnection(cs);
       SqlCommand cmd = new SqlCommand();
       cmd.Connection = cn;
       cmd.CommandType = CommandType.Text;
       cmd.CommandText = "insert into GenderSample values(@Name,@Email,@Gender)";
       cmd.Parameters.Clear();
       cmd.Parameters.AddWithValue("@Name", txtName.Text);
       cmd.Parameters.AddWithValue("@Email", txtEmail.Text);
       cmd.Parameters.AddWithValue("@gender", rbtGender.SelectedValue);
       if (cn.State == ConnectionState.Closed)
           cn.Open();
       cmd.ExecuteNonQuery();
       cn.Close();
       lblmsg.Text = "Data entered successfully!!!";
   }<pre>



I hope this example clears your problem.
Have a good day!!!
 
Share this answer
 
v2
Comments
Abhinav S 29-Aug-11 13:02pm    
Detailed. Have a 5.
srinivas vadepally 30-Aug-11 0:46am    
thank you abhinav
if store radio button value code like this
RadioButtonList1.SelectedValue


if store radio button text code like this
RadioButtonList1.SelectedItem.Text


Thanks
 
Share this answer
 
Comments
Tiwari Avinash 29-Aug-11 5:15am    
easiest way.. rahman! my 4
C#
protected void Button1_Click(object sender, EventArgs e)
    {
        string cs = ConfigurationManager.ConnectionStrings["srinivasdb"].ConnectionString;
        SqlConnection cn = new SqlConnection(cs);
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = cn;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "insert into GenderSample values(@Name,@Email,@Gender)";
        cmd.Parameters.Clear();
        cmd.Parameters.AddWithValue("@Name", txtName.Text);
        cmd.Parameters.AddWithValue("@Email", txtEmail.Text);
        cmd.Parameters.AddWithValue("@gender", rbtGender.SelectedValue);
        if (cn.State == ConnectionState.Closed)
            cn.Open();
        cmd.ExecuteNonQuery();
        cn.Close();
        lblmsg.Text = "Data entered successfully!!!";
    }
 
Share this answer
 
v2

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