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

    Gender storage in database

    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 + "','" + RadioButton1.checked.ToString() + "')", 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>");
    {

    sir from these code i am able to store the gender true and false my coulumn gender datatype is varchar sir how can i able to store male and female or m & f in database/
  • #753334
    assuming that RadioButton1 is displays the text male. please follow the below code

    string gender = RadioButton1.Checked ? "Male" : "Female";

    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 + "','" + gender + "')", cnn);
    cnn.Open();


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

    Miss. Jain
    Microsoft Certified Technology Specialist in .Net

  • #753354
    Hi,

    Can you please post your gender binding part of design. You need to store the value of that particular database based on that field.

    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/

  • #753386
    Instead of storing Radio Button value you can store male of Female as below.



    string strGender = RadioButton1.Checked ? "Male" : "Female";

    And Replace the value as

    ,'" + strGender +"')",

    Thanks & Regards
    Anil Kumar Pandey
    Microsoft MVP, DNS MVM

  • #753392
    Hi sachin,

    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:
    Plz refer the below sample code,it may help u out
    Boolean gender=false;
    if( radioMale.IsChecked)
    gender= true;
    else
    gender= false;

    Then pass the gender variable into the Db.

  • #753410
    You need to use condition operators in your RadioButton1 to check gender according to you database .


  • Sign In to post your comments