从选项卡控件上的文本框中获取文本

时间:2012-11-23 15:00:01

标签: sql vb.net winforms visual-studio-2010 tsql

我有一个带有 tabcontrol 的表单,其中 4个tabpages 每个都有拥有 texbox和组合框,然后我有一个按钮< tabcontrol的strong>外部从这些框中输入的所有值构建 SQL 字符串,即使有些是空白的,问题是当我按下按钮时我得到一个< strong> null引用异常,即使我使用 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

确定新的更新: 我输入了一些数据,一些文本框空了,但确保所有组合框都选中了值,然后按下更新按钮我遇到了同样的问题,但是我重新编写,将当前选项卡更改为任何其他选项卡(从其他四个),然后切换回上一个选项卡,我按下按钮和 IT WORKS !! ?? 就好像表单没有提交用户输入的值,直到焦点发生变化或变得疯狂。

1 个答案:

答案 0 :(得分: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。