You must Sign In to post a response.
  • Category: Visual Studio

    How to store radio button in database

    Sir i have Coded My aspx.cs Page like this

    protected void Submit_Click(object sender, EventArgs e)
    {


    SqlConnection cnn = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=basoft;Integrated Security=True;Pooling=False");
    SqlCommand cmd = new SqlCommand("insert into Registration values('" +
    TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text +
    "','" + TextBox5.Text + "','" + TextBox6.Text + "','" + TextBox7.Text + "','" +
    DropDownList1.SelectedValue + "','" + DropDownList2.SelectedValue + "','" +
    DropDownList3.SelectedValue + "','" + TextBox8.Text + "','" + DropDownList4.SelectedValue + "')", cnn);
    cnn.Open();


    var n = cmd.ExecuteNonQuery();
    cnn.Close();


    if (n == 1)
    Response.Write("<script>alert('Registration Success');</script>");
    else
    Response.Write("<script>alert('Registration Failed');</script>");
    {

    And I want to store the gender with radio button how can i code to store the gender's radio button to the database with all that textboxes values.
  • #753200
    Sir Please Find the attachment of aspx and aspx.cs.

    aspx.zip

    Delete Attachment

  • #753201
    <asp:RadioButton ID="rbnMale" runat="server" Text="Male" />
    <asp:RadioButton ID="rbnFemale" runat="server" Text="Female" />

    string value = "";
    bool isChecked = rbnMale.Checked;
    if(isChecked )
    value=rbnMale.Text;
    else
    value=rbnFemale.Text;

    assuming that in your webpage you have two radiobuttons with the name rbnMale and
    rbnFemale with the text male and female respectively then in the above code we
    are checking if rbnMale is checked then save the text of that rbnMale radio button
    else it will save the text of the rbnFemale radio button

    Miss. Jain
    Microsoft Certified Technology Specialist in .Net

  • #753205

    Hi,

    If you want to store radio button value into database then use below sample code. I just modify your insert statement please use this for reference and modify appropriate to your requirement.


    SqlCommand cmd = new SqlCommand("insert into Registration values('" +
    TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" +
    TextBox4.Text + "','" + TextBox5.Text + "','" + TextBox6.Text + "','" +
    TextBox7.Text + "','" + DropDownList1.SelectedValue + "','" +
    DropDownList2.SelectedValue + "','" + DropDownList3.SelectedValue + "','" +
    TextBox8.Text + "','" + DropDownList4.SelectedValue +
    "','"+radiobutton1.SelectedValue.ToString()+"')", cnn);


    Hope this will helpful to you..


    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #753219
    Hi,

    You may use
    RadioButton1.Text
    to get the selected button's text. Not sure what is the confusion here.

    In order to insert this in database you should have a field in the table and then you need to change the code you mentioned here to accommodate this value.

    let me know if you are still confused this.


    Regards,
    Asheej T K

  • #753247
    Hello,

    You can use variable also.



    int gender;

    if(rdbtnmale.checked==true)
    {
    gender="Male";
    }
    else
    {
    gender="Female";
    }




    After doing this you can use your query.


    SqlCommand cmd = new SqlCommand("insert into Registration values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "','" + TextBox6.Text + "','" + TextBox7.Text + "','" + DropDownList1.SelectedValue + "','" + DropDownList2.SelectedValue + "','" + DropDownList3.SelectedValue + "','" + TextBox8.Text + "','" + DropDownList4.SelectedValue + "','" + gender + "')", cnn);



    Hope this will be helpful to you.

    Regards,
    Dhiraj C. Solanki

  • #753277
    Dear Sachin,

    The method of insertion is totally dependent on your database design. i.e. what is the data type of column where you want to store Radio Buttons value.

    If it is of type varchar, you can store just text of label or something as mentioned in above replies.
    If data type is of Bit (boolen), you might use below code;


    int radioValue;
    if( radioMale.IsChecked)
    radioValue = 1;
    else
    radioValue = 0;


    And insert value of variable radioValue in your T-SQL statement.
    All the Best.

    -------------
    Glad to be,
    John Bhatt
    Editor - DNS Forums
    https://www.pyarb.com

  • #753287
    Hi Sachin,

    Please try below snippet to save radion button value to the database:

    string Gender;
    if(rdoMale.checked==true)
    {
    Gender="Male";
    }
    else
    {
    Gender="Female";
    }

    Hope this will help you.

    Thanks,
    Ram Prasad

  • #753288
    Sir naveensanagasetti thanks i am able to store true & False into db but sir how can i store male and female or M & F?

  • #753312
    what is the datatype of the gender column in your database. is it bit or varchar?
    Miss. Jain
    Microsoft Certified Technology Specialist in .Net

  • #753321
    Mam varchar

  • #753347
    Hi Sachin,

    For male/female also you have value right.?
    Use my above sample code and pass that selected values while saved into database.

    If you struck in any place then let me know..

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #753356
    Hi,

    If u taken the gender field as Varchar then this process u can follow:

    SqlCommand cmd = new SqlCommand("insert into Registration(code,names,m_name,desgnation,gender) values ('" + textBox1.Text + "',
    '" + textBox2.Text + "',
    '" + textBox3.Text + "',
    '" + textBox4.Text + "',
    '" + textBox5.Text + "',
    '"+ radioButton1.Checked ? "Male" : "Female" +"' )");


    Or

    if u taken that value as bit then u can follow this way:

    use this code before insert record into database:

    Boolean gender=false;
    if( radioMale.IsChecked)
    gender= true;
    else
    gender= false;

    Then pass the gender variable into the Db.


    I thinks its help u out




    thanks,
    chitaranjan


  • Sign In to post your comments