Reputation: 9
i am a beginner in windows form application. i am maintaining student record. the problem is that when i checked Male or female button , in sql database both female and male show in gender column , not only one. please help me. here is my code
private void button1_Click(object sender, EventArgs e)
{
coachingdbDataContext coach = new coachingdbDataContext();
std_table std = new std_table()
{
Name= nametxt.Text,
Father_Name= fnametxt.Text,
Class= classtxt.Text,
Roll_Number= rolltxt.Text,
Address=addresstxt.Text,
Contact_Number=numtxt.Text,
Date_Of_Birth=daybox.Text +" "+ monthbox.Text +" "+ yearbox.Text,
Gender= malebutton.Text + femalebutton.Text,
};
coach.std_tables.InsertOnSubmit(std);
coach.SubmitChanges();
MessageBox.Show("Sucessfully Submitted");
}
Upvotes: 0
Views: 265
Reputation: 2376
If you add the text from both together then of course you get both in our output.
Gender= malebutton.Text + femalebutton.Text
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Use this:
std_table std = new std_table()
{
Name= nametxt.Text,
Father_Name= fnametxt.Text,
Class= classtxt.Text,
Roll_Number= rolltxt.Text,
Address=addresstxt.Text,
Contact_Number=numtxt.Text,
Date_Of_Birth=daybox.Text +" "+ monthbox.Text +" "+ yearbox.Text,
Gender= malebutton.Checked ? malebutton.Text : femalebutton.Text;
};
You can read about the ?
operator here.
Upvotes: 1
Reputation: 36
you can replace the
Gender= malebutton.Text + femalebutton.Text
by
if(malebutton.checked == true )
{
Gender= malebutton.Text;
}
else
{
Gender= femalebutton.Text;
}
hopefully this will solve your problem
Upvotes: 0
Reputation: 6995
This is the culprit:
Gender= malebutton.Text + femalebutton.Text,
Do this instead:
std_table std = new std_table()
{
Name= nametxt.Text,
Father_Name= fnametxt.Text,
Class= classtxt.Text,
Roll_Number= rolltxt.Text,
Address=addresstxt.Text,
Contact_Number=numtxt.Text,
Date_Of_Birth=daybox.Text +" "+ monthbox.Text +" "+ yearbox.Text
};
if ( malebutton.Checked ) {
std.Gender = malebutton.Text;
}
if (femalebutton.Checked ) {
std.Gender = femalebutton.Text;
}
Upvotes: 0
Reputation: 12439
You'll have to check for Checked
radio button:
Gender = malebutton.Checked == true ? malebutton.Text : femalebutton.Text;
Upvotes: 5
Reputation: 218827
This looks like a likely culprit:
Gender = malebutton.Text + femalebutton.Text
Regardless of which button is selected, both of the buttons have text. So both will be inserted.
You probably want to check which one is selected. What are the types of malebutton
and femalebutton
? Do they, for example, have a Checked
property? Something like this might work then:
Gender = malebutton.Checked ? malebutton.Text :
(femalebutton.Checked ? femalebutton.Text :
"unspecified");
If the male button is checked, us that text. Else, if the female button is checked, use that text. Else, use a default value.
Upvotes: 2