Click here to Skip to main content
14,427,436 members
Rate this:
Please Sign up or sign in to vote.
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 22:07pm
Rate this:
Please Sign up or sign in to vote.

Solution 2

in markup:

<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:

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


hope it helps :)
   
v3
Comments
Abhinav S 29-Aug-11 13:01pm
   
Fairly accurate. My 5.
Uday P.Singh 29-Aug-11 14:23pm
   
thanks Abhinav :)
Rate this:
Please Sign up or sign in to vote.

Solution 3

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.

if (drReader("rbMale") == "1")
    rbMale.checked = true;
else
   rbFemale.checked = true;
   
Comments
Abhinav S 29-Aug-11 13:02pm
   
Fair enough. My 5.
Rate this:
Please Sign up or sign in to vote.

Solution 4

Hi sunil,

i am explaining your question through a sample example.
in markup:
<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:
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!!!
   
v2
Comments
Abhinav S 29-Aug-11 13:02pm
   
Detailed. Have a 5.
srinivas vadepally 30-Aug-11 0:46am
   
thank you abhinav
Rate this:
Please Sign up or sign in to vote.

Solution 1

if store radio button value code like this
RadioButtonList1.SelectedValue


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


Thanks
   
Comments
Tiwari Avinash 29-Aug-11 5:15am
   
easiest way.. rahman! my 4
Rate this:
Please Sign up or sign in to vote.

Solution 5

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!!!";
    }
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100