Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
i have 2 radio button for male and female,what will C# code to store in database?
after checking any one button...
Posted 28-Aug-11 21:34pm
Rate this: bad
good
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 Smile | :)
  Permalink  
v3
Comments
Abhinav S at 29-Aug-11 13:01pm
   
Fairly accurate. My 5.
Uday P.Singh at 29-Aug-11 14:23pm
   
thanks Abhinav :)
Rate this: bad
good
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;
  Permalink  
Comments
Abhinav S at 29-Aug-11 13:02pm
   
Fair enough. My 5.
Rate this: bad
good
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!!!
  Permalink  
v2
Comments
Abhinav S at 29-Aug-11 13:02pm
   
Detailed. Have a 5.
srinivas vadepally at 30-Aug-11 0:46am
   
thank you abhinav
Rate this: bad
good
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
  Permalink  
Comments
Avinash Tiwari MCA at 29-Aug-11 5:15am
   
easiest way.. rahman! my 4
Rate this: bad
good
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!!!";
    }
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 195
1 ProgramFOX 130
2 Maciej Los 105
3 Sergey Alexandrovich Kryukov 85
4 Afzaal Ahmad Zeeshan 82
0 OriginalGriff 6,564
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web01 | 2.8.1411022.1 | Last Updated 31 Aug 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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