Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So I'm trying to save RadioButtonList selections to a database but I get the error
Quote:
Conversion failed when converting the nvarchar value to data type bit.

In the database, I have set the data type as bit so I don't know why this happens.

protected void Button8_Click(object sender, EventArgs e)
    { 
      string constr = ConfigurationManager.ConnectionStrings["facultylogConnectionString"].ConnectionString;
      using (SqlConnection con = new SqlConnection(constr))
      {
          using (SqlCommand cmd = new SqlCommand())
          {
              cmd.CommandText = "Insert into MOD values (@Format, @Instructions, @Marks_Dis, @Meets_CLO, @Remarks, @Subject_Name, @Test_Number, @Exam_Month, @Exam_Year, @Semester)";
              {
                  cmd.Parameters.AddWithValue("@Format",radio4.SelectedValue);
                  cmd.Parameters.AddWithValue("@Instructions",radio2.SelectedValue);
                  cmd.Parameters.AddWithValue("@Marks_Dis",radio3.SelectedValue);
                  cmd.Parameters.AddWithValue("@Meets_CLO",radio1.SelectedValue);
                  cmd.Parameters.AddWithValue("@Remarks",TextBox2.Text);
                  cmd.Parameters.AddWithValue("@Subject_Name",DropDownList1.SelectedValue);
                  cmd.Parameters.AddWithValue("@Test_Number",DropDownList2.SelectedValue);
                  cmd.Parameters.AddWithValue("@Exam_Month",Label27.Text);
                  cmd.Parameters.AddWithValue("@Exam_Year",Label28.Text);
                  cmd.Parameters.AddWithValue("@Semester",DropDownList3.SelectedValue);
                  cmd.Connection = con;
                  con.Open();
                  cmd.ExecuteReader();

                  con.Close();
}
}
}
}


What I have tried:

The problem is here.

cmd.Parameters.AddWithValue("@Format",radio4.SelectedValue);
                  cmd.Parameters.AddWithValue("@Instructions",radio2.SelectedValue);
                  cmd.Parameters.AddWithValue("@Marks_Dis",radio3.SelectedValue);
                  cmd.Parameters.AddWithValue("@Meets_CLO",radio1.SelectedValue);


This is my output part. - Meets the expectation causes an error when I select it and click the button where the above code is there.
Format                     O Meets the Expectation	 OBelow Expectation
Instructions to Candidates O Meets the Expectation	 OBelow Expectation
Marks DistrubutionMeets    O Meets the Expectation	 OBelow Expectation
LO Objectives              O Meets the Expectation	 OBelow Expectation
Posted
Updated 29-Jun-20 13:57pm

I'm not sure what a radiobutton 'SelectedValue' gives you or what the data type is

I may have forgotten too much about C# Winforms GUIs, but I thought radiobuttons had a 'checked' (boolean) and a 'text' (string) value, so you would need to think about what you actually wanted to store in the database, and I'd therefore be careful with 'bit' ie '0' or '1' .. sure, you can represent a boolean true or false as a '1' or a '0'

I would, on first glance, try something like this

public string GetSelectedRadioButtonText(RadioButton[] radioButtons)
{
    // possible additional checks: check multiple selected, check if at least one is selected and generate more descriptive exception.

    // works for above cases, but throws a generic InvalidOperationException if it fails
    return radioButtons.Single(r => r.Checked).Text;
}
and then use the result from this below as a string to store in the database
string formatMeets = this.GetSelectedRadioButtonText(new[] { radio1, radio2 });
assuming the radio buttons were laid out like
radio1 radio2
radio3 radio4
radio5 radio6


That code came from here btw c# - Insert radio button selected value into database - Stack Overflow[^] there are a couple of options described there for handling radio buttons

Or do as MadMyche says, cast to a Boolean - easier to use a 'helper function' if you're looking at pairs/sets of radio buttons
 
Share this answer
 
v3
Comments
candijen 30-Jun-20 16:10pm    
Thank you
The logical thing to would be to check the value coming from the radio buttons; and probably cast/convert that to a boolean
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900