C# .NET - how to insert the radio button value in database

Asked By Lata on 10-Feb-11 05:32 AM
Hello All,

       I have created asp page in that dropdownlist and radio button option is there so now i want to insert the selected dropdownlist value and radio button value in the database how to write the code for this....
Jatin Prajapati replied to Lata on 10-Feb-11 05:38 AM
Hi Lata,
Please refer the below example. This will give you better idea.

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<script runat="server">
  protected void Page_Load(object sender, EventArgs e)
  {
    if (!IsPostBack)
    {
       
    }
  }
 
  protected void btn_Click(object sender, EventArgs e)
  {
    using (SqlConnection connection = new SqlConnection("Data Source=localhost;Initial Catalog=database; Integrated Security=TRUE;"))
    {
      using (SqlCommand command = new SqlCommand("Insert into table(Item,Active) values(@Item,@Active);", connection))
      {
        command.Parameters.AddWithValue("@Item", DropDownList1.SelectedItem.Text);
        command.Parameters.AddWithValue("@Active", radioButton1.Checked.ToString());
        connection.Open();
        command.ExecuteNonQuery();
        connection.Close();
        lbl.Text = "Item added to database";
      }
    }
  }   
</script>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title></title>
</head>
<body>
  <form id="form1" runat="server">
  <div>     
    <asp:Label ID="lbl" runat="server"></asp:Label><br />
    Item:  
    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
      <asp:ListItem Text="Item1"></asp:ListItem>
      <asp:ListItem Text="Item2"></asp:ListItem>
    </asp:DropDownList><br />
    Active:  <asp:RadioButton ID="radioButton1" runat="server" />
    <asp:Button ID="btn" runat="server" Text="Test" OnClick="btn_Click" />
  </div>
  </form>
</body>
</html>
Reena Jain replied to Lata on 10-Feb-11 05:52 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
Lata replied to Jatin Prajapati on 10-Feb-11 05:58 AM
Thank you so much.....i got the result
Lata replied to Reena Jain on 10-Feb-11 06:03 AM
thanks reena.......
Anoop S replied to Lata on 10-Feb-11 06:08 AM
You can add radio button value like this way

comm.Parameters["@Radvalue"].Value = rbradvalue.SelectedValue.ToString() ->

For eg

//Define Data Objects
SqlConnection conn;
SqlCommand comm;
//Read the connection string from web.config
string connectionString = ConfigurationManager.ConnectionStrings[ "YourConnectionString"].ConnectionString;
//Initialize Connection
 conn = new SqlConnection(connectionString);
//Create Command
comm = new SqlCommand(
        "INSERT INTO IntakeQuestionnaire(Name,Address,FaveColor)" +
        "VALUES (@Name,@Address,@FaveColor)", conn);
 //Add command parameters
comm.Parameters.Add("@Name", System.Data.SqlDbType.NVarChar);
comm.Parameters["@Name"].Value = txtName.Text;
comm.Parameters.Add("@Address", System.Data.SqlDbType.NVarChar);
comm.Parameters["@Address"].Value = txtAddress.Text;
comm.Parameters["@FaveColor"].Value = rbFaveColor.SelectedValue.ToString() //adding radiobutton value
      try
      {
        // Open the connection
        conn.Open();
        // Execute the command
        comm.ExecuteNonQuery();
        // Reload page if the query executed successfully
        Response.Redirect("SubmissionComplete.aspx");
      }

Daivagna Nanavati replied to Lata on 10-Feb-11 02:36 PM
HI Lata

I hope you have some button or something based on which event you will insert those data into your database, here i am assuming that you only have two fields in your table, you can modify query based on your table structure later on, but on click of button you will do following, first you will get the selectedValue of dropdown which would be int value and value of radio buton as well like following

protected void btn_Click(object sender, EventArgs e)
  {
  SqlConnection connection = new SqlConnection("your conectionn string"))
  SqlCommand command = new SqlCommand("Insert into table(field1,field2) values(@ddlValue,@RadioValue);", connection))
  command.Parameters.AddWithValue("@ddlValue", DropDownList1.SelectedItem.Value);
  command.Parameters.AddWithValue("@RadioValue", radioButton1.Checked.ToString());
  connection.Open();
  command.ExecuteNonQuery();
  connection.Close();
   
}

here if you want to store the text of dropdown than property would be different, but as far as i know you would be storing an id only

let me know

Thanks