2014-01-15 38 views
-2

如果我在cmbGrNo.text寫話,會有一個下拉組合框的if​​ else語句錯誤

輸入的字符串格式不正確

我怎樣才能識別哪些組合框是編輯? 我寫在組合框,然後單擊搜索按鈕編譯器給我錯誤,因爲它不接受其他組合框文本。它僅接受第一個值... 請幫我

這裏是代碼

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

是與組合框的selected.value或選定的索引。 – jhyap

+0

當您通過此調試時,「cmbGrNo.Text」的值是多少? – paqogomez

回答

1

Convert.ToInt32收到一個字符串,它不能分析出現此錯誤。

您正在選擇組合框的文本值,這不會返回一個整數。您需要選擇組合框的SelectedValue

事情是這樣的:

Convert.ToInt32(cmbGrNo.SelectedValue.Text) 
+0

cmbGrNo.SelectedValue.ToString()出現不是文本,這也不符合我的條件@paqogomez –

+0

@DaniDanish'cmbGrNo.SelectedValue.ToString()'的值是什麼? – paqogomez

+0

其int值我在每個if else條件中都使用Convert.ToInt32(cmbGrNo.text) –