Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
i have doubt when clicking on radio button how the value is storing in the database.
Posted 5-Dec-12 20:04pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

First select the radiobutton and pass it as parameter in query..
 
sting query = insert into emp(name, gender) values ("AAA", genderRadioBtn.SelectedItem.ToString());
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

example: my designing page like this
 
<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 this gender option i use radio button...
 
this gender value i wanna store in database....for that i use this coding for save this value in database...
 
i wrote this code in button control:
 
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!!!";
    }
 

 
If u feel my answer is useful ur programming ..mark this... as accept solution...
thank uSmile | :) Happy codingSmile | :)
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

protected void Button1_Click(object sender, ImageClickEventArgs e)
    {
string a="";
 if (RadioButton1.Checked == true)
            {
                a = RadioButton1.Text;
            }
            else if (RadioButton2.Checked == true)
            {
                a = RadioButton2.Text;
            }
 
SqlConnection sqlconn = new SqlConnection("server=.\\sqlexpress;database=db;integrated security=true");
            SqlCommand sqlcomm = new SqlCommand("insert into reg(gender) values(@gender)", sqlconn);
sqlcomm.Parameters.Add("@gender", SqlDbType.VarChar, 50).Value = a.ToString();
sqlconn.Open();
            sqlcomm.ExecuteNonQuery();
sqlconn.close();
}
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Shai Vashdi 1,990
1 OriginalGriff 558
2 Manas Bhardwaj 383
3 Tadit Dash 335
4 Damith Weerasinghe 330
0 Sergey Alexandrovich Kryukov 9,575
1 OriginalGriff 6,006
2 Peter Leow 4,500
3 Maciej Los 3,540
4 Abhinav S 3,513


Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 6 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid