0

我正在编写一个程序,在其中向 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);
        }

    }

http://imgur.com/FRl2Uew,0slWYAq 有我的表格。该链接上有2个上传。点击第二页查看第二页

4

1 回答 1

0

Can't see anything obvious that'll break.
Could you provide some sample inputs?
When you debug your code (where you get the -14,-25), are the objects the correct objects?

Code improvement suggestions:
Having said that, your variable names are ... lacking ... I can understand cmbClassRoomName, but all the iNames or dtNames don't need these prefixes. Also, if you do choose to use the type and the camel case, iclassTypeID should be iClassTypeID. Another issue is that the iprefix suggests it's an interface, not an int.

About your validation if/else block ... why is it there? Are you using WinForm/WPF/ASP? Have you considered using validations? it might be a bit funny the first time, but it's worth the time to learn in gold later on, decoupling your code so everything makes more sense.

于 2013-10-25T00:37:11.473 回答