Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C中仅选择一个单选按钮_C# - Fatal编程技术网

C# 在C中仅选择一个单选按钮

C# 在C中仅选择一个单选按钮,c#,C#,我是windows窗体应用程序的初学者。 我在维护学生记录。 问题是,当我选中男性或女性按钮时,在sql数据库中,女性和男性都显示在性别列中,而不是只有一个。 请帮帮我。 这是我的密码 private void button1_Click(object sender, EventArgs e) { coachingdbDataContext coach = new coachingdbDataContext(); std_table std = new std_table()

我是windows窗体应用程序的初学者。 我在维护学生记录。 问题是,当我选中男性或女性按钮时,在sql数据库中,女性和男性都显示在性别列中,而不是只有一个。 请帮帮我。 这是我的密码

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");                       
}   

这似乎是罪魁祸首:

Gender = malebutton.Text + femalebutton.Text
无论选择哪个按钮,两个按钮都有文本。所以这两个都将被插入

您可能需要检查选择了哪一个。男按钮和女按钮的类型是什么?例如,它们是否具有选中的属性?这样做可能会奏效:

Gender = malebutton.Checked ? malebutton.Text :
         (femalebutton.Checked ? femalebutton.Text :
         "unspecified");
如果选中男性按钮,则显示该文本。否则,如果选中女性按钮,则使用该文本。否则,请使用默认值。

您必须检查选中的单选按钮:

Gender = malebutton.Checked == true ? malebutton.Text : femalebutton.Text;
这就是罪魁祸首:

Gender= malebutton.Text + femalebutton.Text,
改为这样做:

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;
}

如果你把两个文本加在一起,当然你会在我们的输出中得到它们

Gender= malebutton.Text + femalebutton.Text
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
使用以下命令:

    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;
    };
你能读到关于这个故事吗?操作员。

您可以更换

Gender= malebutton.Text + femalebutton.Text 


希望这能解决您的问题

为什么要这样做-性别=男性按钮。文本+女性按钮。文本,非常感谢,我不知道如何使用“?”标记进行选择。谢谢?:操作符是terniary操作符,它是if/else语句的快捷方式。是的,现在它已经完成了。非常感谢,我不知道如何使用“?”标记进行选择。谢谢,这不一定是必需的,但对于内联条件语句来说非常有用。我贴了一个链接到一个页面,可以告诉你更多关于那个操作的信息。哦,非常感谢。第一次使用这个网站,我很开心,很有帮助不,我不能多次使用性别,我已经尝试过了。谢谢你的建议这是故意的?我不知道你的意思。你的建议是对的,我只是说你的答案与其他人不同。啊,明白了。是的,我故意使用了更基本的结构,希望它更容易理解。但我看到的所有答案都是好答案。
if(malebutton.checked == true ) 
{
Gender= malebutton.Text;
}
else
{
Gender= femalebutton.Text;
}