我如何使用多个单选按钮并将其保存到数据库中,以访问选择项文本单选按钮,当我尝试从名称到性别时,他仅保存性别。当我尝试将查询插入到(lname,mname,fname,当然,专业,未成年人,性别),当我尝试将年份插入另一个单选按钮(lname,mname,fname,课程,专业,未成年人,性别,年份)时,他将数据保存在我的数据库访问权限中,但插入时出错,但是当我在字符串年份中保留文本时,他会保存文本。
我的表单设计:
public partial class student : UserControl
{
OleDbConnection con = new OleDbConnection(@"Provider =
Microsoft.ACE.OLEDB.12.0; Data Source
=C:\Users\ronaldcaymo\Desktop\Student Information System.accdb");
string Gender;
string year;
string Status;
private void button4_Click(object sender, EventArgs e)
{
con.Open();
OleDbCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into
table1(Lname,Fname,Mname,Course,Major,Minor,semester,Gender,Status)
values('"+tblname.Text+"','"+tbfname.Text+"','"+tbmname.Text+
"','"+cbcourse.Text+"','" +cbmajor.Text
+"','"+cbminor.Text+"','"+cbsemester.Text+"','"+Gender+"','"+Status+"')";
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Student Added");
}
private void radioButton5_CheckedChanged(object sender, EventArgs e)
{
Gender = "Male";
}
private void radioButton6_CheckedChanged(object sender, EventArgs e)
{
Gender ="Female";
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
year = "1st";
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
year ="2nd";
}
private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
year ="3rd";
}
private void radioButton4_CheckedChanged(object sender, EventArgs e)
{
year ="4th";
}
private void radioButton8_CheckedChanged(object sender, EventArgs e)
{
Status = "Single";
}
private void radioButton7_CheckedChanged(object sender, EventArgs e)
{
Status = "Married";
}
}
答案 0 :(得分:0)
单选按钮的CheckedChanged
事件将在每次更改单选按钮的状态时触发,即选中或取消选中状态。
因此,当选中“第一年”并选中另一个单选按钮(例如“第三年”)时,“第一年”将变为未选中状态,从而与{{1}同时触发事件radioButton1_CheckedChanged
}。
要解决此问题,请在所有radioButton3_CheckedChanged
事件中添加条件检查,例如:
radioButton_CheckedChanged