Click here to Skip to main content
15,901,283 members
Please Sign up or sign in to vote.
3.33/5 (3 votes)
See more:
How can i insert radio button values into database?
Hi to all
I have three radio buttons .. and i want to insert one radio button values which i will click ....give me some code....
Posted
Comments
Believer 2023 23-Jan-23 6:12am    
Gender: <asp:radiobutton id="Rd1" runat="server" text="Male" groupname="Gender">   
<asp:radiobutton id="Rd2" runat="server" text="Female" groupname="Gender">

C#
cmd.Parameters.AddWithValue("@sex", gender.SelectedValue);
---giving error,need help

if You have only three RadioButton then you create if condition and get the value of it,
Like,
C#
if (RadioButton1.Checked)
        {
            rbdtext = RadioButton1.Text;
        }
        else if (RadioButton2.Checked)
        {
            rbdtext = RadioButton2.Text;
        }
        else if (RadioButton3.Checked)
        {
            rbdtext = RadioButton3.Text;
        }

<asp:RadioButton ID="RadioButton1" runat="server" Text="First" GroupName="rbd" Checked="true"/>
<asp:RadioButton ID="RadioButton2" runat="server" Text="Second" GroupName="rbd"/>
<asp:RadioButton ID="RadioButton3" runat="server" Text="Third" GroupName="rbd"/>

And also you have to set all RadioButton's GroupName are Same. (e.g GroupName="abc")
Hope It's Help You.
 
Share this answer
 
v4
try this in your code-behind.
In aspx file if you use radio button list for three buttons then use this or if you use radio buttons individual then use rbtGender.Text

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
I suggest you to use RadioButtonList instead of using three individual radio button and insert the RadioButtonList selected value

your source code look like .....

XML
<asp:RadioButtonList ID="RadioButton1" runat="server">
<asp:ListItem Text="First" Value"First" selected="true" />
<asp:ListItem Text="Second" Value"Second" />
<asp:ListItem Text="Third" Value"Third"/>
</asp:RadioButtonList>




and code will be like

string strValue = RadioButton1.SelectedValue;


and insert strValue where ever you want.
 
Share this answer
 

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