Reputation: 629
I have a few questions on my form with either an acceptable or an unacceptable option which is linked to a radio button. The issue i am having is that my check event is firing once when i check and firing again when i uncheck it, i am unsure of how to modify my code so that it only fires once per each questions answer, would this be the correct way to achieve what i am doing and insert the answers into SQL as it is semi working apart from the events which fire twice:
string answer1 = "Acceptable";
string answer2 = "Not Acceptable";
string n1;
string n2;
string n3;
string n4;
private void btnSubmit_Click(object sender, RoutedEventArgs e)
{
SqlConnection conDatabase = new SqlConnection(String.Format(@"Data Source={0};Initial Catalog={1};Persist Security Info=True;User ID={2};Password={3}", SQLSERVER_ID, SQLDatabaseName, SQLServerLoginName, SQLServerPassword));
string query = "INSERT INTO Answers(Option1, Option2, Option3, Option4) VALUES (@Option1, @Option2, @Option3, @Option4";
SqlCommand cmd = new SqlCommand(query, conDatabase);
cmd.CommandType = CommandType.Text;
conDatabase.Open();
cmd.Parameters.Add("@Option1", SqlDbType.NVarChar).Value = n1;
cmd.Parameters.Add("@Option2", SqlDbType.NVarChar).Value = n2;
cmd.Parameters.Add("@Option3", SqlDbType.NVarChar).Value = n3;
cmd.Parameters.Add("@Option4", SqlDbType.NVarChar).Value = n4;
cmd.ExecuteNonQuery();
conDatabase.Close();
}
My xaml code is:
<RadioButton x:Name="radio1a" GroupName="Question1" Checked="radioButton1_Checked"/>
<RadioButton x:Name="radio1b" GroupName="Question1" Unchecked="radioButton2_Checked"/>
<RadioButton x:Name="radio2a" GroupName="Question2" Checked="radioButton1_Checked"/>
<RadioButton x:Name="radio2b" GroupName="Question2" Unchecked="radioButton2_Checked"/>
Lastly my check is if the radio button1 for acceptable is clicked then 'n1' will be set to answer1 (acceptable) and if the radio button2 for unacceptable is clicked then 'n1' will be set to answer2 (unacceptable) - n1 being number 1 (the first question on my form):
private void radioButton1_Checked(object sender, RoutedEventArgs e)
{
Check();
}
private void radioButton2_Checked(object sender, RoutedEventArgs e)
{
Check();
}
public void Check()
{
if (radio1a.IsChecked == true)
{
n1 = answer1;
}
else if (radio1b.IsChecked == true)
{
n1 = answer2;
}
if (radio2a.IsChecked == true)
{
n2 = answer1;
}
else if (radio2b.IsChecked == true)
{
n2 = answer2;
}
.... etc.
}
Upvotes: 0
Views: 2097
Reputation: 3272
Alright.
Imagine you check your first radiobutton. After that you are checking the second one. Your function check() will still be true for the first radiobutton, because it is still checked!
Go this way: If radioButton1 or 2 is checked get the name of the button. Pass this button-name to your check()-function and switch it. This way it will only fire for the radiobutton which got checked, not for all that are checked too.
private void radioButton1_Checked(object sender, RoutedEventArgs e)
{
var radioFired = (RadioButton)sender;
Check(radioFired.Name);
}
private void radioButton2_Checked(object sender, RoutedEventArgs e)
{
var radioFired = (RadioButton)sender;
Check(radioFired.Name);
}
public void Check(string radioName)
{
switch(radioName)
{
case "radio1a":
n1 = answer1;
break;
case "radio1b":
n1 = answer2;
break;
etc...
}
}
As a hint for cleaner coding: There is no need to compare a returned boolean to "true" or "false".
Instead of this..
if(radioButton.IsChecked == true)
better go this way for comparing true..
if(radioButton.IsChecked)
or like this for comparing to false..
if(!radioButton.IsChecked)
If it is checked, 'radioButton.IsChecked' is true. If the value is true, your code will get executed. Its just like writing if(true) which will get executed aswell.
If you would rather like to stay with your code:
Deleted this part. Wasnt working. Better take my solution above.
Upvotes: 1