2012-11-23 62 views
-1

我有一個的tabcontrol在它與4個TabPage每個都有它自己 texboxes和組合框一種形式,那麼我有TabControl的至極的按鈕建立從所有值SQL字符串在這些框中輸入即使有些是空白,問題是,當我按下按鈕,我收到了空引用異常,甚至當我使用在try..catch對錯誤繼續下一步編譯器重新熔合以創建文本字符串。 這裏是我的代碼:獲取文本

Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)  Handles btnUpdate.Click 
    If Me.BankGuidTextBox.Text = vbNullString Then 
     Me.BankGuidTextBox.Text = Guid.Empty.ToString 
    End If 
    If Me.BankNumTextBox.Text = vbNullString Then 
     Me.BankNumTextBox.Text = 0 
    End If 
    If Me.NameTextBox.Text = vbNullString Or Me.CodeTextBox.Text = vbNullString Then 
     GoTo outofsub 
    End If 
    Try 
     Dim strSQL3 As String 
     strSQL3 = "USE MSILHR" & vbCrLf & _ 
      "UPDATE employee SET Code = '" & Me.CodeTextBox.Text & "', Name = '" & Me.NameTextBox.Text & "', Latinname = '" & Me.LatinNameTextBox.Text & "', Barcode = '" & Me.BarcodeTextBox.Text & "', FirstName = '" & Me.FirstNameTextBox.Text & "', LastName = '" & Me.LastNameTextBox.Text & "', idFather = '" & Me.IdFatherTextBox.Text & "', idMother = '" & Me.IdMotherTextBox.Text & "', idBirthDate = '" & Me.IdBirthDateDateTimePicker.Text & "', idBirthplace = '" & Me.IdBirthPlaceTextBox.Text & "', idSex = '" & Me.IdSexComboBox.SelectedValue & "', idMaritalstat = '" & Me.IdMaritalstatComboBox.SelectedValue & "', idSmoker = '" & Me.IdSmokerComboBox.SelectedValue & "', idMilitary = '" & Me.IdMilitaryComboBox.SelectedValue & "', idRegistry = '" & Me.IdRegistryTextBox.Text & "', idNum = '" & Me.IdNumTextBox.Text & "', idAddress = '" & Me.IdAddressTextBox.Text & "', idNationality = '" & Me.IdNationalityTextBox.Text & "', idReligion = '" & Me.IdReligionComboBox.SelectedItem & "', idTel = '" & Me.IdTelTextBox.Text & "', idMobile = '" & Me.IdMobileTextBox.Text & "', idNotes = '" & Me.IdNotesTextBox.Text & "', jStartDate = '" & Me.JStartDateDateTimePicker.Text & "', jQuitDate = '" & Me.JQuitDateDateTimePicker.Text & "', jQuitReason = '" & Me.JQuitReasonTextBox.Text & "', jSocialSec = '" & Me.JSocialSecTextBox.Text & "', jSocialdin = '" & Me.JSocialdinDateTimePicker.Text & "', jSocialdout = '" & Me.JSocialdoutDateTimePicker.Text & "', jHoliday1 = '" & Me.JHoliday1ComboBox.SelectedValue & "', jHoliday2 = '" & Me.JHoliday2ComboBox.SelectedValue & "', jEmpStat = '" & Me.JEmpStatComboBox.SelectedValue & "', jEmail = '" & Me.JEmailTextBox.Text & "', jPrevvacs = '" & Me.JPrevvacsTextBox.Text & "', jAdminvacs = '" & Me.JAdminvacsTextBox.Text & "', jHealthvacs = '" & Me.JHealthvacsTextBox.Text & "', jUnpaidvacs = '" & Me.JUnpaidvacsTextBox.Text & "', DepartmentGuid = '" & Me.DepartmentGuidComboBox.SelectedValue.ToString & "', JobTitleGuid = '" & Me.JobTitleGuidComboBox.SelectedValue.ToString & "',SalarycalctypeGuid = '" & Me.SalarycalctypeGuidComboBox.SelectedValue.ToString & "', TeamGuid = '" & Me.TeamGuidComboBox.SelectedValue.ToString & "', WorkDays = '" & Me.WorkDaysTextBox.Text & "', DaHours = '" & Me.DaHoursTextBox.Text & "', OverTimeHourPrice = '" & Me.OverTimeHourPriceTextBox.Text & "', CutSalary = '" & Me.CutSalaryTextBox.Text & "',BasicSalary = '" & Me.BasicSalaryTextBox.Text & "', SpecialSalary = '" & Me.SpecialSalaryTextBox.Text & "',CurrencyGuid = '" & Me.CurrencyGuidComboBox.SelectedValue.ToString & "', BankGuid = '" & Me.BankGuidTextBox.Text & "', BankNum = '" & Me.BankNumTextBox.Text & "', PeriodGUID = '" & Me.EmpperiodCombo.SelectedValue.ToString & "'" & vbCrLf & _ 
      "WHERE GUID = '" & Me.GUIDTextBox.Text & "'" 

     '"update employee set Picturepath = (select name from types where type = '660')+'\" & Me.NameTextBox.Text & "' where code = '" & Me.CodeTextBox.Text & "' AND Name = '" & Me.NameTextBox.Text & "'" 
     Dim dbConnection As New SqlConnection(connectionString) 
     ' A SqlCommand object is used to execute the SQL commands. 
     Dim cmd As New SqlCommand(strSQL3, dbConnection) 

     ' Open the connection, execute the command, and close the connection. 
     ' It is more efficient to ExecuteNonQuery when data is not being 
     ' returned. 
     dbConnection.Open() 
     cmd.ExecuteNonQuery() 
     dbConnection.Close() 

     MessageBox.Show("Record is updated.", _ 
      "Data Addition Status", _ 
      MessageBoxButtons.OK, MessageBoxIcon.Information) 

    Catch sqlExc As SqlException 
     MessageBox.Show(sqlExc.ToString, "SQL Exception Error!", _ 
      MessageBoxButtons.OK, MessageBoxIcon.Error) 
    End Try 
    'Me.EmployeeTableAdapter.Fill(Me.MSILHRDataset.employee) 

    ResetUI() 
outofsub: 
    MsgBox("you need to add more information!", MsgBoxStyle.Information, "Error") 
End Sub 

確定新的更新: 我輸入一些數據,留下了一些文本框爲空,但確信所有的組合框在他們選擇的值,然後按下updatebutton我得到了相同的問題,我redid改變當前選項卡選擇其他任何一個(從其他四個),然後切換回到前一個選項卡,我按下按鈕和IT WORKS !! ??這就好像表格不是提交用戶輸入的值直到焦點發生變化或者它變得瘋狂。

+0

您只捕獲一個SqlException。你還需要捕捉一個普遍的例外。 – Paparazzi

+0

嘗試逐行調試,並告訴錯誤發生的位置。我創建了一個帶有文本框的tabcontrol,我可以得到沒有問題的總字符串。你是?我的意思是strSQL3有一個值(你期望的值)? – Nianios

+0

'GoTo outofsub' Aaghr ...我的眼睛... – Neolisk

回答

1

您只捕獲一個SqlException。
您還需要了解一般例外情況。

Catch sqlExc As SqlException 
    MessageBox.Show(sqlExc.ToString, "SQL Exception Error!", _ 
     MessageBoxButtons.OK, MessageBoxIcon.Error) 
Catch exc As Exception 
    MessageBox.Show(exc.ToString, "General Exception Error!", _ 
     MessageBoxButtons.OK, MessageBoxIcon.Error) 

不知道爲什麼你得到一個空例外。
但它沒有被捕獲,因爲它不是一個SqlException。
在該字符串上分解它確實看到它在哪裏打破。
在表面看起來它應該工作。
我懷疑其中一個控件返回null而不是string.empty。

+0

感謝您的提示,我更新了所有的Try..Catch語句。 –