Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all i am working on a project urgent.I need to save some values from my webform to a SQL Server 2005 database.

I have two radio buttons male and female of datatype is Bit.I would like to save a value of 1 or 0 depending on the selection of the user any ideas and sample code will be welcome.

Code

C#
protected void cmdSubmit_Click(object sender, EventArgs e)
       {
           SqlDataStaff.InsertCommand = INSERT INTO [Staff] ([Firstname], +
                                         "[Lastname], [GenderID]) " +
                                         "VALUES ('" + txtFName.Text + "', " +
                                          "'" + txtLastName.Text + "', " +
                                         "" + rdbgender.SelectedValue + ")";
           SqlDataStaff.Insert();
           txtFName.Text = "";
           txtLastName.Text = "";



Thanks
Posted
Updated 28-Sep-11 3:48am
v2

Hi,

Use radio button list for gender selection

Here I'm providing some example to get selected value from radio buttonlist control

ASP.NET
<asp:radiobuttonlist id="RadioButtonList1" runat="server" xmlns:asp="#unknown">
     <asp:listitem text="Male" value="1"></asp:listitem>
     <asp:listitem text="Female" value="0"></asp:listitem>
    </asp:radiobuttonlist>


and place below code in button click event
C#
if (RadioButtonList1.SelectedIndex >= 0)
            {
                int gend = int.Parse(RadioButtonList1.SelectedValue);
            }


By using gend variable you can save it in your database

All the Best
 
Share this answer
 
Hello Friend,

 Use Radio ButtonList then you can get selected value of radio button for save.
 
Share this answer
 
you can use 2 radio button

1. rad_male & 2. rad_female

if(rad_male.checked)
{
//store 0 in databse
}
else

{
1 in database
}

if your field is int type then you can use case statement at the back end

Ex. Case when gender=0then 'Male' else 'Female' end as Gender
 
Share this answer
 
Another way is that
XML
<asp:radiobuttonlist id="RadioButtonList1" runat="server" xmlns:asp="#unknown">
         <asp:listitem text="Male" value="0"></asp:listitem>
         <asp:listitem text="Female" value="1"></asp:listitem>
        </asp:radiobuttonlist>


and

SQL
SqlDataStaff.InsertCommand = INSERT INTO [Staff] ([Firstname], +
                                         "[Lastname], [GenderID]) " +
                                         "VALUES ('" + txtFName.Text + "', " +
                                          "'" + txtLastName.Text + "', " +
                                         "" + rdbgender.SelectedIndex + ")";
           SqlDataStaff.Insert();
           txtFName.Text = "";
           txtLastName.Text = "";
 
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