C# 用参数化组合框过滤组合框的c代码

C# 用参数化组合框过滤组合框的c代码,c#,sql,combobox,C#,Sql,Combobox,我正在编写一个程序,在这个程序中,我向sql添加了一个数学类,但对一些外键使用组合框,这样它们只能从引用表中的某些项中进行选择。例如,类名来自一个不同的表,其中包含所有的类名。但是由于某些原因,当我使用combobox.selectedvalue函数时,classnameid没有给出正确的id。它不断给出怪异的-14和-25作为id,这是错误的。请帮忙。这是我的密码 private void btnAddClass_Click(object sender, EventArgs e)

我正在编写一个程序,在这个程序中,我向sql添加了一个数学类,但对一些外键使用组合框,这样它们只能从引用表中的某些项中进行选择。例如,类名来自一个不同的表,其中包含所有的类名。但是由于某些原因,当我使用combobox.selectedvalue函数时,classnameid没有给出正确的id。它不断给出怪异的-14和-25作为id,这是错误的。请帮忙。这是我的密码

    private void btnAddClass_Click(object sender, EventArgs e)
    {
        int iclassroomID = Convert.ToInt16(cmbClassRoomName.SelectedValue);
        int iclassTypeID = Convert.ToInt16(cmbClassType.SelectedValue);
        int iHours = Convert.ToInt16(cmbHours.SelectedItem);
        int iMins = Convert.ToInt16(cmbMinutes.SelectedItem);
        string sClassLength = txtLength.Text;
        DateTime dtClassdate;
        dtClassdate = dateTimePicker1.Value;
        DateTime myClassDateandTime = dtClassdate.Date.AddHours(iHours).AddMinutes(iMins);
        txtOutput.Text = Convert.ToString(iclassroomID);



        int selectedyear = this.dateTimePicker1.Value.Year;
        int selectedmonth = this.dateTimePicker1.Value.Month;
        int selectedday = this.dateTimePicker1.Value.Day;


        int thisyear = Convert.ToInt16(DateTime.Now.Year);
        int thismonth = Convert.ToInt16(DateTime.Now.Month);
        int thisday = Convert.ToInt16(DateTime.Now.Day);


        if (cmbSchool.SelectedIndex == 0) 
        {
            MessageBox.Show("Please Select A School");
        }
        else if (cmbClassRoomName.SelectedIndex == 0)
        {
            MessageBox.Show("Please Select A Classroom");
        }
        else if (cmbClassType.SelectedIndex == 0)
        {
            MessageBox.Show("Please Select A Class Type");
        }
        else if (cmbHours.SelectedIndex == 0)
        {
            MessageBox.Show("Please Select the hour for the starting time of the class");
        }
        else if (cmbMinutes.SelectedIndex == 0)
        {
            MessageBox.Show("Please Select the minute for the starting time of the class");
        }
        else if (selectedyear < thisyear)
        {
            MessageBox.Show("Please Select a date forward from today");
        }
        else if (selectedmonth < thismonth)
        {
            MessageBox.Show("Please Select a date forward from today");
        }
        else if (selectedday < thisday)
        {
            MessageBox.Show("Please Select a date forward from today");
        }
        else if (txtLength.Text == "")
        {
            MessageBox.Show("Please enter the class length");
        }
        else
        {
            classTableAdapter.AddClass(iclassroomID, iclassTypeID, myClassDateandTime, sClassLength);
            this.Validate();
            this.classBindingSource.EndEdit();
            this.tableAdapterManager.UpdateAll(this.geared4MathDataSet);
            MessageBox.Show("Class Added");
            this.Close();
        }
    }

    private void cmbSchool_SelectedIndexChanged(object sender, EventArgs e)
    {

        int iclassroomname = Convert.ToInt16(cmbSchool.SelectedValue);

        try
        {
            this.classRoomTableAdapter.FillBySchool(this.geared4MathDataSet.ClassRoom, iclassroomname);
            lblClassroomName.Visible = true;
            cmbClassRoomName.Visible = true;
        }
        catch (System.Exception ex)
        {
            System.Windows.Forms.MessageBox.Show(ex.Message);
        }

    }

这是我的表格。该链接上有2次上传。单击第二页以查看第二页

看不到任何明显的会中断的内容。 你能提供一些输入样本吗? 当你调试你的代码时,你得到了-14,-25,这些对象是正确的对象吗

代码改进建议: 话虽如此,您的变量名是。。。缺少。。。 我能理解cmbClassRoomName,但所有的iNames或DTName都不需要这些前缀。另外,如果您选择使用类型和驼峰大小写,iclassTypeID应该是iclassTypeID。 另一个问题是iprefix建议它是一个接口,而不是int


关于验证if/else块。。。为什么在那里?您正在使用WinForm/WPF/ASP吗?您考虑过使用验证吗?第一次可能会有点滑稽,但以后花时间学习gold是值得的,它可以使代码解耦,使一切变得更有意义。

问题出在btnaddClass按钮上if-else块只是为了确保用户不会留下空白或无效条目。这只是我错误处理的一部分。但这样做很好,如果有什么遗漏,只需提供一个messagebox。我理解它的用途,我想说的是,还有其他更优雅的处理方法,并向用户提供更好的反馈。例如,看看那些漂亮的感叹号。您能回答我其余的问题吗?这是我在单击btnAddClass时遇到的错误:INSERT语句与外键约束FK__类__教室_u 25869641冲突。冲突发生在数据库Geared4Math、表dbo.教室、列“ClassRoomID”中。声明已终止。我认为addclass查询是完美的。这是一个非常简单的插入函数,我使用参数处理来设置它。但问题似乎在于,我的cmbclassroomname.selectedvalue返回了错误的classroomid:当我运行程序并使用最上面的文本框TxtOutput查看id时,它与我的id根本不对应。我在id为1-14的表数据中有14行,但selectedvalue返回-24个变量例如感谢链接,我肯定会使用它:我不知道: