3

I previously use this code for insert operation but along with this I want to save the radiobutton values:

cmd.CommandText="INSERT INTO Table1 (username,password,gender VALUES(@username,@password,gender)";
cmd.Parameters.AddWithValue("@username", textBox1.Text);
cmd.Parameters.AddWithValue("@password", textBox2.Text);

I have two radio buttons for male and female, I want to save only one value in the database so I have used groupbox for selecting only one radio button but how to insert into database as male or female.

I am working on windows form.

0

5 Answers 5

4

How about:


cmd.CommandText="INSERT INTO Table1 (username,password,gender) VALUES (@username,@password,@gender)";
cmd.Parameters.AddWithValue("@username", textBox1.Text);
cmd.Parameters.AddWithValue("@password", textBox2.Text);

if(radioMale.Checked)
    cmd.Parameters.AddWithValue("@gender", "Male");
else
    cmd.Parameters.AddWithValue("@gender", "Female");

Sign up to request clarification or add additional context in comments.

3 Comments

thank you sir, that was very easy, why i didint thought i dont know, i thot it would be a complex step, can you tell me can i save bool values in the sql server? what is the data type for that?
@shariq_khan you can indeed, the data type is bit.
stackoverflow.com/questions/14162839/… CAN YOU HELP ME BHAI PLEASE
1

Another option is to use a combo box with 2 values in it, one for male and one for female and just insert the selected value. This would also take up less space on the form which may or may not be useful.

cmd.Parameters.AddWithValue("@gender", genderCombo.SelectedValue);

1 Comment

0
 private string selectedValue, date;
        private int i;
        private SqlCommand xcmd;
        private SqlConnection xcon;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                day_DropDownList.Items.Insert(0, new ListItem("DD", "DD"));
                month_DropDownList.Items.Insert(0, new ListItem("MM", "MM"));
                year_DropDownList.Items.Insert(0, new ListItem("YY", "YY"));
                for (i = 1; i < 32; i++)
                {
                    day_DropDownList.Items.Add(i.ToString());
                }
                for (i = 1; i < 13; i++)
                {
                    month_DropDownList.Items.Add(i.ToString());
                }
                for (i = 1950; i < 2014; i++)
                {
                    year_DropDownList.Items.Add(i.ToString());
                }
                employeeName_Txt.Focus();
            }


        }



        protected void submit_Button_Click(object sender, EventArgs e)
        {
            selectedValue = gender_RadioButtonList.SelectedValue;

            xcon = new SqlConnection("Data Source=.; DataBase=AptechDB; UID=sa; Password=123;");

            xcon.Open();
            date = day_DropDownList.Text.ToString() + "/" + month_DropDownList.Text.ToString() + "/" + year_DropDownList.Text.ToString();
            xcmd = new SqlCommand("insert into tblEmployee values('" + employeeName_Txt.Text + "','" + date + "','" + selectedValue + "','" + post_Txt.Text + "','" + city_Txt.Text + "','" + country_Txt.Text + "','" + mobileno_Txt.Text + "')", xcon);
            xcmd.ExecuteNonQuery();
            Label1.Text = "Information submitted successfully";
            xcon.Close();
            clear();
        }

        public void clear()
        {
            employeeName_Txt.Text = "";
            day_DropDownList.SelectedIndex = 0;
            month_DropDownList.SelectedIndex = 0;
            year_DropDownList.SelectedIndex = 0;
            post_Txt.Text = "";
            city_Txt.Text = "";
            country_Txt.Text = "";
            mobileno_Txt.Text = "";
        }

    }
}

Comments

0

Try this one

cmd.CommandText="INSERT INTO Table1 (username,password,gender) VALUES (@username,@password,@gender)";
cmd.Parameters.AddWithValue("@username", textBox1.Text);
cmd.Parameters.AddWithValue("@password", textBox2.Text);

if(radioMale.Checked)
    cmd.Parameters.AddWithValue("@gender", "Male");
else
    cmd.Parameters.AddWithValue("@gender", "Female");

Comments

-1
        SqlCommand cmd = new SqlCommand("insert into table1 (Name,Mail,Phone,Addres,Gender) values (@Name,@Mail,@Phone,@Addres,@Gender)", con);
        cmd.Parameters.AddWithValue("@Name", textBox1.Text);
        cmd.Parameters.AddWithValue("@Mail", textBox2.Text);
        cmd.Parameters.AddWithValue("@Phone", textBox3.Text);
        cmd.Parameters.AddWithValue("@Addres", textBox4.Text);
        if (radioButton1.Checked==true)
            cmd.Parameters.AddWithValue("@gender", "Male");
        else
            cmd.Parameters.AddWithValue("@gender", "Female");

1 Comment

How does this code improve on any of the previously posted answers?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.