2013-10-24 73 views
0

我正在寫一個程序,我添加一個數學類到sql,但使用組合框的一些外鍵,以便他們只能從某些參考表中選擇項目(例如,類名來自不同的表,所有的類名都在其中。但是由於某種原因,當我使用combobox.selectedvalue函數時,classnameid沒有給出正確的id,它繼續給出奇怪的-14和-25作爲id這是錯誤的。請幫助。這裏是我的代碼c#代碼過濾一個組合框與一個paramatized組合框

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周的上傳鏈接。點擊第二頁看到第二頁

+0

它的btnaddClass按鈕有問題 –

回答

0

看不到任何明顯的破壞。
你能提供一些樣本輸入嗎?
當你調試你的代碼(你得到-14,-25)時,對象是否是正確的對象?

代碼改進建議:
說了這麼多,你的變量名是... ...缺乏 我能理解cmbClassRoomName,但所有的iNamesdtNames不需要這些前綴。另外,如果您確實選擇使用類型和駱駝案例,iclassTypeID應該是iClassTypeID。 另一個問題是i前綴暗示它是一個接口,而不是int。

關於您的驗證if/else塊...爲什麼會出現?你使用WinForm/WPF/ASP嗎?你有沒有考慮過使用驗證?它第一次可能會有點有趣,但是值得花些時間在黃金上學習,解耦你的代碼,這樣一切都變得更有意義。

+0

if else塊只是在那裏,以確保用戶不會留下空白或無效的條目。只是我的錯誤處理的一部分。但是工作正常,如果缺少任何東西,只會給出一個消息框。 –

+0

我明白這是什麼意思,我說的是,還有其他方式來處理它,它更優雅,給用戶更好的反饋。例如,看看[這裏](例如,好的感嘆號)。你能回答我的其餘問題嗎? – Noctis

+0

這是我點擊btnAddClass時得到的錯誤:INSERT語句與FOREIGN KEY約束「FK__Class__ClassRoom__25869641」衝突。數據庫「Geared4Math」,表「dbo.ClassRoom」,列「ClassRoomID」發生衝突。 該聲明已被終止。 addclass查詢是完美的,我認爲。這是一個非常簡單的插入功能,我使用參數處理來設置它。但它看起來像問題是,我的cmbclassroomname.selectedvalue返回了錯誤的課堂ID :(當我運行該程序,並使用非常頂級的文本框(txtoutput) –