仅当某些文本框未留空时,如何进行插入

时间:2017-01-25 14:33:12

标签: asp.net vb.net

我有一个在提交按钮Onclick上运行的函数,它正确地写入我需要的表。但是在这个函数中我称之为公共子“Insert Spouse(intscope)”。我遇到的问题是如果某些文本框留空,则尝试保持此插入 如果我将文本框留空,我会收到一条错误消息,指出输入字符串的格式不正确。如果那些特定的文本框不包含数据,我希望它完全跳过此插入。

  Public Function CreateRecord()
    Dim intScope As Integer
    Dim dbAddEmployee As New SqlCommand("Insert into mca_DEPDBEmployee (EmpID, FName, LName, SSN, Department, Spousebx) values (@EmpID, @FName, @LName, @SSN, @Department, @Spousebx); Select Scope_Identity()", dbGPconn)

    Dim prmEmpID As New SqlParameter("@EmpID", SqlDbType.Decimal)
    prmEmpID.Value = txtEmpID.Text
    dbAddEmployee.Parameters.Add(prmEmpID)

    Dim prmFName As New SqlParameter("@FName", SqlDbType.NVarChar, 50)
    prmFName.Value = txtFName.Text
    dbAddEmployee.Parameters.Add(prmFName)

    Dim prmLName As New SqlParameter("@LName", SqlDbType.NVarChar, 50)
    prmLName.Value = txtLName.Text
    dbAddEmployee.Parameters.Add(prmLName)

    Dim prmSSN As New SqlParameter("@SSN", SqlDbType.Int)
    prmSSN.Value = txtEmpSSN.Text
    dbAddEmployee.Parameters.Add(prmSSN)

    Dim prmDepartment As New SqlParameter("@Department", SqlDbType.NVarChar, 100)
    prmDepartment.Value = ddlDept.SelectedItem.Text
    dbAddEmployee.Parameters.Add(prmDepartment)

    Dim prmSpouse As New SqlParameter("@Spousebx", SqlDbType.Bit, 1)
    If rblSelectSpouse.SelectedValue = 1 Then
        prmSpouse.Value = 1
    Else
        prmSpouse.Value = 0
    End If
    dbAddEmployee.Parameters.Add(prmSpouse)

    dbGPconn.Open()
    intScope = dbAddEmployee.ExecuteScalar()
    dbGPconn.Close()

    AddDependents(intScope)
    InsertSpouse(intScope)

    Return intScope
End Function

Public Sub InsertSpouse(ByVal intscope As Integer)

    If txtSpouseNm.Text <> "" And txtSpouseDept.Text <> "" Then
        Dim dbInsertSpouse As New SqlCommand("Insert into mca_DEPDBSpouse ( EmpTableID, EmpID, Name, Department, SpEmpID) Values (@EmpTableID, @EmpID, @Name, @Department, @SpEmpID)", dbGPconn)

        Dim prmEmpTableID As New SqlParameter("@EmpTableID", SqlDbType.Decimal, 18)
        prmEmpTableID.Value = intscope
        dbInsertSpouse.Parameters.Add(prmEmpTableID)

        Dim prmEmpID As New SqlParameter("@EmpID", SqlDbType.Decimal, 18)
        prmEmpID.Value = txtEmpID.Text
        dbInsertSpouse.Parameters.Add(prmEmpID)

        Dim prmName As New SqlParameter("@Name", SqlDbType.NVarChar, 50)
        prmName.Value = txtSpouseNm.Text
        If txtSpouseNm.Text = "" Then prmName.Value = DBNull.Value
        dbInsertSpouse.Parameters.Add(prmName)

        Dim prmDepartment As New SqlParameter("@Department", SqlDbType.NVarChar, 50)
        prmDepartment.Value = txtSpouseDept.Text
        If txtSpouseDept.Text = "" Then prmDepartment.Value = DBNull.Value
        dbInsertSpouse.Parameters.Add(prmDepartment)

        Dim prmSpEmpID As New SqlParameter("@SpEmpID", SqlDbType.Decimal, 18)
        prmSpEmpID.Value = txtSpouseEmpID.Text
        If txtSpouseEmpID.Text = "" Then prmSpEmpID.Value = DBNull.Value
        dbInsertSpouse.Parameters.Add(prmSpEmpID)

        dbGPconn.Open()
        dbInsertSpouse.ExecuteScalar()
        dbGPconn.Close()
    End If

End Sub

1 个答案:

答案 0 :(得分:1)

假设你只关心这些领域

  • txtSpouseNm  
  • txtSpouseDept  
  • txtEmpID  
  • txtSpouseEmpID
if String.IsNullOrEmpty(txtSpouseNm.Text) Or String.IsNullOrEmpty(txtSpouseDept.Text) Or String.IsNullOrEmpty(txtEmpID.Text) Or String.IsNullOrEmpty(txtSpouseEmpID.Text) Then
'Something is missing
else
'everything filled out, do your thing
end if