不支持参数的DBNull值。表值参数不能是DBNull

时间:2016-02-24 12:53:42

标签: sql .net sql-server vb.net table-valued-parameters

我目前正在尝试将.net应用程序中的两个表值参数传递给数据库。

我得到的错误是

  

不支持参数'@StageNotes'的DBNull值。表值参数不能是DBNull。

我已经检查过,当我传递它时参数值不为null,而且我得到的错误不是sqlException,而只是一个将这个错误传回的常规异常

参数声明如此

Dim stageNotesparamTable2SQL As New SqlParameter("@StageNotes", SqlDbType.Structured)
Dim responsesParamTable2SQL As New SqlParameter("@Responses", SqlDbType.Structured)

并且像这样分配值

Dim responseTable = ResponsesToDt()
responsesParamTable2SQL.Value = responseTable

Dim stageTable = StageNotesToDt()
stageNotesparamTable2SQL.Value = stageTable

参数在存储过程中声明,如此

@StageNotes                 [App].[StageNotesTableType] READONLY,
@Responses                  [app].QuestionResponseTableType READONLY,

调试时我看到stageNotesparamTable2SQL.Value显示那里有一个数据表并且有数据所以它绝对不是null。

如果有人能帮助我,我将不胜感激。

感谢

- 编辑---

    Protected Function ResponsesToDt() As DataTable
    Dim dt As New DataTable()


    dt.Columns.Add("CheckId", GetType(Integer))
    dt.Columns.Add("QuestionId", GetType(Integer))
    dt.Columns.Add("AnswerId", GetType(Integer))
    dt.Columns.Add("StaffNumber", GetType(String)).MaxLength = 15

    Dim dictValues = _dicOfControlsAndValues.Where(Function(x) x.Key.Contains("ddl_Stage_"))

    For Each item In dictValues

        Dim ddlIdBreakDown = item.Key.ToString().Split("_").ToList

        Dim checkid As Integer = item.Key.ToString().Split("_").Last
        Dim questionId As Integer = Convert.ToInt16(ddlIdBreakDown(4))
        Dim answerId As Integer = Convert.ToInt16(item.Value)
        Dim staffNumber As String = GetLoggedOnStaffNumber()

        dt.Rows.Add(checkid, questionId, answerId, staffNumber)
    Next

    Return dt
End Function

 Protected Function StageNotesToDt() As DataTable

    Dim dt As New DataTable

    dt.Columns.Add("CheckId", GetType(Integer))
    dt.Columns.Add("StageId", GetType(Integer))
    dt.Columns.Add("StageNotes", GetType(String))
    dt.Columns.Add("ModifiedDate", GetType(String))

    Dim dictValues = _dicOfControlsAndValues.Where(Function(x) x.Key.Contains("textArea_Stage"))

    For Each item In dictValues
        Dim stageNote As String = String.Empty
        Dim ddlIdBreakDown = item.Key.ToString().Split("_").ToList
        If String.IsNullOrEmpty(item.Value.ToString()) Then
            stageNote = "."
        Else
            stageNote = item.Value.ToString()
        End If
        Dim checkid As Integer = item.Key.ToString().Split("_").Last
        Dim stageId As Integer = Convert.ToInt16(ddlIdBreakDown(2))
        Dim stageNotes As String = stageNote
        Dim modifiedDate As DateTime = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")

        dt.Rows.Add(checkid, stageId, stageNotes, modifiedDate)
    Next

    Return dt

End Function

- 编辑2 -

 DAL.ExecCommand("[App].[usp_SaveCheckDetails]", "",
                                                checkIdParam,
                                                userIdParam,
                                                caseNoteParam,
                                                checkNoteParam,
                                                checkCompletedParam,
                                                CheckFeedbackSentToUserId,
                                                stageNotesparamTable2SQL,
                                                responsesParamTable2SQL)

然后DAL我不得不打电话。

DAL是我目前无法编辑的共享仓库:

Public Function ExecCommand(ByVal spName As String, ByVal connStringName As String, ByVal ParamArray SPParms() As SqlClient.SqlParameter) As Boolean

    Dim SqlConnection As New SqlConnection
    Dim sqlCommand As New SqlClient.SqlCommand
    Dim returnCode As Boolean = False

    Try

        If connStringName <> "" Then
            SqlConnection.ConnectionString = ConfigurationManager.ConnectionStrings(connStringName).ConnectionString
        Else
            SqlConnection.ConnectionString = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
        End If

        sqlCommand.CommandType = CommandType.StoredProcedure
        sqlCommand.CommandText = spName
        For Each p As SqlParameter In SPParms
            If p.Direction <> ParameterDirection.Output Then
                If Not p.Value.GetType() = GetType(Integer) Then
                    If p.Value.ToString() = "" OrElse p.Value = Nothing Then
                        p.Value = DBNull.Value
                    End If
                End If
            End If

            sqlCommand.Parameters.Add(p)
        Next
        sqlCommand.Connection = SqlConnection
        SqlConnection.Open()
        sqlCommand.ExecuteNonQuery()
        returnCode = True

    Catch sqlEx As SqlException
        HelpersErrorHandling.WriteAppLogEntry(Me.GetType().Name, System.Reflection.MethodBase.GetCurrentMethod().ToString, sqlEx)
    Catch ex As Exception
        If Not ex.Message.ToString().Contains("Thread was being aborted") Then
            HelpersErrorHandling.WriteAppLogEntry(Me.GetType().Name, System.Reflection.MethodBase.GetCurrentMethod().ToString, ex)
        End If

    Finally

        Try
            sqlCommand.Parameters.Clear()
            sqlCommand.Dispose()
            SqlConnection.Close()
            SqlConnection.Dispose()

        Catch ex As Exception
            HelpersErrorHandling.WriteAppLogEntry(Me.GetType().Name, System.Reflection.MethodBase.GetCurrentMethod().ToString, ex)

        End Try

    End Try

    Return returnCode

End Function

0 个答案:

没有答案