C# .NET - passing value from radio button to database

Asked By rakesh on 21-Sep-11 07:13 AM
can any one plz tell me how to pass radiobutton value to database
Reena Jain replied to rakesh on 21-Sep-11 07:18 AM
hi,

its Simple to Store Radio Button Value In SQL server take Bit Data type to store the value of Radio Button in to Database

Now if Bit = 1 then male and Bit=0 then female .

in coding

int x ;
if ( RadioButton1.Checked == true)
{
  x = 1;
}
else if(RadioButton2.Checked == true)
{
  x=0;
}
// Now Use X in Insert Statement to fill the value of that Bit Column

for combo box use selectedText or selectedValue property to insert in database

hope this will help you
Web Star replied to rakesh on 21-Sep-11 07:24 AM
  1. you simply get value from radiobutton to variable and then you can insert in database by adding parameter with that vlaue
    string str = "";
  2. if (radioButton1.Checked)
  3. {
  4. str = "Y";
  5. }
  6. if (radioButton2.Checked)
  7. {
  8. str = "N";
  9. }
    //here you add selected value to command parameter and insert it into database as you inserted other data
  10. cmd.Parameters.Add("@paperback", SqlDbType.NVarChar).Value = str;
aneesa replied to rakesh on 21-Sep-11 07:28 AM
1.con.Conn.Open();
2.SqlCommand cmd = new SqlCommand("procedurename", con.Conn);
3.cmd.CommandType = CommandType.StoredProcedure;
4.cmd.Parameters.AddWithValue("@parametername", RadioButtonList1.SelectedValue);
//here the same way u can do for radiobutton
like
cmd.Parameters.AddWithValue("@parametername", RadioButton1.SelectedValue);
5.cmd.ExecuteNonQuery();
6.con.Conn.Close();

dipa ahuja replied to rakesh on 21-Sep-11 07:34 AM
First if you have many option in radiobutton then you should take radiobuttonList and write the code this way:


protected void Button1_Click(object sender, EventArgs e)
{
  string s = RadioButtonList1.SelectedItem.ToString();
  SqlConnection conn = new SqlConnection("constring");
  SqlCommand comm = new SqlCommand("insert into table1 (f1) values '" + s + "'", conn);
  comm.ExecuteNonQuery();
}

Suchit shah replied to rakesh on 21-Sep-11 07:51 AM
you can pass the value like below code

int rdbutton;
if(rdbutton1.checked == true)
{
  rdbutton = 0;
   //any other logic if u want to implement
}
else if ( rdbutton2.checked == true)
{
  rdbutton = 1;
  //any other logic if u want to implement
}
.................................................
............................................... So on
else
{
  rdbutton = 99;
}

in this way u can check for value and assign the value and then according 2 ur requirment u can save that value in to the DB