如果else语句错误,请下拉comboBox

时间:2014-01-15 12:27:06

标签: c#

如果我在cmbGrNo.text写信,那么会有一个

  

输入字符串的格式不正确

如何识别编辑哪个组合框? 我在组合框中写,然后单击搜索按钮编译器给我错误,因为它不接受其他组合框文本。它只接受第一个的价值...... 请帮帮我

这里是代码

private void btnSearch_Click(object sender, EventArgs e)
{
    if (cmbAdmissionNo.Text.Length == 0 && cmbRollNo.Text.Length == 0 && cmbStudentName.Text.Length == 0 && cmbGRNo.Text.Length == 0)
    {
        MessageBox.Show("Enter Student Name OR Admission No OR Gr No OR Roll No"," INSERT FIELDS", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    else
    {
        if (StudentDBClass.CheckStudent(cmbStudentName.Text))
        {
            DataTable dt = StudentDBClass.getTableBYStdName(cmbStudentName.Text);
            txtAdminNo.Text = "Admission No : " + dt.Rows[0]["AddmissionNo"];
            txtGrNo.Text = "GR No : " + dt.Rows[0]["GRNo"];
            txtClass.Text = "Class : " + dt.Rows[0]["ClassName"];
            txtStudentName.Text = "Student Name : " + dt.Rows[0]["StudentName"];
            txtFatherName.Text = "Father Name : " + dt.Rows[0]["FatherName"];
            txtRollNo.Text = "Roll No: " + dt.Rows[0]["RollNo"];
            dgvStdFeeCollection.DataSource = null;
            dgvStdFeeCollection.DataSource = StudentFeeCollectionDBClass.getStdNameForDgvFeeCollection(cmbStudentName.Text);
            cmbAdmissionNo.SelectedIndex = -1;
            cmbGRNo.SelectedIndex = -1;
            cmbRollNo.SelectedIndex = -1;
            cmbAdmissionNo.Text = string.Empty;
            cmbGRNo.Text = string.Empty;
            cmbRollNo.Text = string.Empty;
        }
        else if (StudentDBClass.CheckWithAdmissionNo(Convert.ToInt32(cmbAdmissionNo.Text)))
        {
            DataTable dt = StudentDBClass.getTableBYAddmissionNo(Convert.ToInt32(cmbAdmissionNo.Text));
            txtAdminNo.Text = "Admission No : " + dt.Rows[0]["AddmissionNo"];
            txtGrNo.Text = "GR No : " + dt.Rows[0]["GRNo"];
            txtClass.Text = "Class : " + dt.Rows[0]["ClassName"];
            txtStudentName.Text = "Student Name : " + dt.Rows[0]["StudentName"];
            txtFatherName.Text = "Father Name : " + dt.Rows[0]["FatherName"];
            txtRollNo.Text = "Roll No: " + dt.Rows[0]["RollNo"];
            dgvStdFeeCollection.DataSource = null;
            dgvStdFeeCollection.DataSource = StudentFeeCollectionDBClass.getAdmissionNoForDgvFeeCollection(Convert.ToInt32(cmbAdmissionNo.Text));
            cmbStudentName.SelectedIndex = -1;
            cmbGRNo.SelectedIndex = -1;
            cmbRollNo.SelectedIndex = -1;
            cmbGRNo.Text = string.Empty;
            cmbStudentName.Text = string.Empty;
            cmbRollNo.Text = string.Empty;

        }
        else if (StudentDBClass.CheckGRNo(Convert.ToInt32(cmbGRNo.Text)))
        {
            DataTable dt = StudentDBClass.getTableGrNo(Convert.ToInt32(cmbGRNo.Text));
            txtAdminNo.Text = "Admission No : " + dt.Rows[0]["AddmissionNo"];
            txtGrNo.Text = "GR No : " + dt.Rows[0]["GRNo"];
            txtClass.Text = "Class : " + dt.Rows[0]["ClassName"];
            txtStudentName.Text = "Student Name : " + dt.Rows[0]["StudentName"];
            txtFatherName.Text = "Father Name : " + dt.Rows[0]["FatherName"];
            txtRollNo.Text = "Roll No: " + dt.Rows[0]["RollNo"];
            dgvStdFeeCollection.DataSource = null;
            dgvStdFeeCollection.DataSource = StudentFeeCollectionDBClass.getGrNoForDgvFeeCollection(Convert.ToInt32(cmbGRNo.Text));
            cmbAdmissionNo.SelectedIndex = -1;
            cmbStudentName.SelectedIndex = -1;
            cmbRollNo.SelectedIndex = -1;
            cmbAdmissionNo.Text = string.Empty;
            cmbStudentName.Text = string.Empty;
            cmbRollNo.Text = string.Empty;
        }

1 个答案:

答案 0 :(得分:1)

Convert.ToInt32收到无法解析的字符串时,会发生此错误。

您正在选择组合框的文本值,这不会返回整数。

您需要选择组合框的SelectedValue

这样的事情:

Convert.ToInt32(cmbGrNo.SelectedValue.Text)