-2

I need help in storing the values from the radiobuttonlist to SQL Server 2008. My code works fine but when I checked in the database if my values from the radiobuttonlist have been stored there's nothing in the database.

asp.net

    <p>
       Hypertension<asp:RadioButtonList 
        ID="RadioButtonList1" 
        RepeatColumns ="2" RepeatDirection = "vertical" 
        RepeatLayout= "table" runat="server" 
        AutoPostBack="True" 
         onselectedindexchanged="RadioButtonList1_SelectedIndexChanged" 
         Width="250px" Height="10px" style="margin-left: 0px" BorderStyle="None" 
          CellPadding="1" CellSpacing="1">
          <asp:ListItem Value="rbtrue">TRUE</asp:ListItem>
       <asp:ListItem Selected="True" Value="rbfalse">FALSE</asp:ListItem>
      </asp:RadioButtonList>
   </p> 

In C#:

    protected void Button1_Click(object sender, EventArgs e)
    {
        //Response.Redirect("WebForm1.aspx");
        string selectedValue1 = this.RadioButtonList1.SelectedValue;
        string selectedValue2 = this.RadioButtonList2.SelectedValue;

        using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["TumorRegistryConnectionString1"].ConnectionString))
        {
            string sql = "Insert into tbTRcBase(HPN,HPNTreatement,ISH,ISHTreatment,Asthma,AsthmaTreatment,DM,DMTreatment,OtherCo,OtherCoTreatment,SecondHandSmoke,Smoker,StopSmoking,Occupation,CancerFamilyHistory,FamilyWithCancer,ParentWithCancer) value(@d1,@d2,@d3,@d4,@d5,@d6,@d7,@d8,@d9,@d10,@d11,@d12,@d13,@d14,@d15,@d16,@d17)";

            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = sql;
            cmd.Parameters.AddWithValue("@d1", selectedValue1);
            cmd.Parameters.AddWithValue("@d2", selectedValue2);

            try
            {
                if (conn.State != ConnectionState.Open)
                    conn.Open();
                int result = cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
                return;
            }
          }
      } 
4
  • What is getting saved in database ? Commented Apr 23, 2015 at 3:23
  • Did the SqlCommand get executed without any error? Commented Apr 23, 2015 at 3:25
  • 3
    SQL command should be Insert into tbl () values (). You have mentioned value Commented Apr 23, 2015 at 3:26
  • already fixed that though still no changes Commented Apr 27, 2015 at 1:09

1 Answer 1

1

if you use this

string selectedValue2 = this.RadioButtonList2.SelectedValue;

you will get the value of the list button that is selected that is rbtrue or rbfalse

if you want to get the selected radiolistbuttons text use this

 string selectedValue2 = this.RadioButtonList1.SelectedItem.Text;

Try this query

string sql = "Insert into tbTRcBase(HPN,HPNTreatement,ISH,ISHTreatment,Asthma,AsthmaTreatment,DM,DMTreatment,OtherCo,OtherCoTreatment,SecondHandSmoke,Smoker,StopSmoking,Occupation,CancerFamilyHistory,FamilyWithCancer,ParentWithCancer) values(@d1,@d2,@d3,@d4,@d5,@d6,@d7,@d8,@d9,@d10,@d11,@d12,@d13,@d14,@d15,@d16,@d17)";

hope this helps

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

4 Comments

this works but my problem is it doesn't appear on the database
check your query there is a error you used value instead of values
thanks by the query mistake already fixed that prob, i tried the .SelectedItem.Text property but when i try to submit the form, an error occurs on the c# codes, "mishandled nullexception"
if you don't want to pass values to remaining parameter just leave them empty and try it will work only if you allowed null values in db for all that parameters

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.