DBNull - 转换错误

时间:2014-05-03 06:09:46

标签: sql vb.net

我有以下错误

从类型' DBNull'转换输入'整数'无效。提交上面的表单错误显示是因为数据库中的任何字段具有NULL VALUE或者可能是真正的原因。

点击保存时

              Dim intresult As String
              Dim objParam As OleDbParameter() = {objDA.OutputParam("@Next_DocumentNo",     OleDbType.VarChar, 20), _
                objDA.InputParam("@Dtype", OleDbType.VarChar, 10, "D"), _
                objDA.InputParam("@MID", OleDbType.VarChar, 20, Session("Mid"))}

    intresult = objDA.ExecSP("dbo.GetNextDocNo", objParam)

    If intresult = 1 Then
        tNextDocNo = objDA.varOutputRet
    Else
                    Exit Sub
    End If
    vDocType = "D"

    vAllocID = "501"
    vDummy = " "
    vDummyBuyer = "NEX"
    vRemarks = Replace(Replace(Me.TextRemark.Text, "'", "''"), vbCrLf, "")

      strSQL = "INSERT INTO dbo.Hder (Document_Type, Document_No, Document_Date, Trans_Type, Type_Desc, V_Type, " & _
        "Mid,Total_Amount, Total_Qty,Prepared_By,Remarks) " & _
         "VALUES ('" & vDocType & "', '" & tNextDocNo & "', '" & Me.TextDate.Text & "', 'D', 'gift IN', 'GIFT',  " & _
        "'" & Mid & "', " & CDbl(Me.txtGrandTotal.Text) & ", " & CDbl(Me.txtTotalQty.Text) & ",  " & _
        "'" & GCurrentUserName & "', '" & vRemarks & "')"

    intresult = objDA.ExecSP(strSQL, "Trans")

     varOutputRet = CType(objCmd.Parameters(0).Value, Integer)

当我提交上述代码

时,在下面的代码中以另一种形式突出显示错误
              varOutputRet = CType(objCmd.Parameters(0).Value, Integer)

1 个答案:

答案 0 :(得分:0)

在投票前检查DBNull

 varOutputRet = CInt(If(IsDBNull(objCmd.Parameters(0).Value), 0, objCmd.Parameters(0).Value))