我有一个Object引用未设置为对象的实例

时间:2014-03-20 02:51:31

标签: asp.net sql-server vb.net ado.net nullreferenceexception

当我点击保存按钮内的SAVE按钮时,我收到此错误 错误是

"对象引用未设置为对象的实例"

Protected Sub btnWizardFinish_Click(sender As Object, e As EventArgs)
    If (rdoAprovalList.SelectedIndex = 1) Then
        SaveConsideration(4)
    Else
        SaveConsideration(1)
    End If
End Sub

这是保存子,用于保存和更新数据库

中DocumentStatusID字段中的值
Private Sub SaveConsideration(ByVal DocumentStatusID As Integer)
    Dim Conn As New SqlConnection(strConn)
    Dim cmd As New SqlCommand
    Try
        If ((txtProducerID.Text.Trim.Length = 9)) Then

            cmd.Connection = Conn
            cmd.CommandType = CommandType.StoredProcedure
            cmd.CommandText = "spConsiderationSaving"
            cmd.Parameters.AddWithValue("@libDocumentID", txtProducerID.Text)
            cmd.Parameters.AddWithValue("@ProducerComments", txtProducerComment.Text)
            cmd.Parameters.AddWithValue("@DocumentStatusID", DocumentStatusID)
            cmd.Parameters.AddWithValue("@ConProduceThai", chkConproduceThai.Checked)
            cmd.Parameters.AddWithValue("@ConProducePolicy", chkConProducePolicy.Checked)
            cmd.Parameters.AddWithValue("@ConProduceIP", chkConProduceIP.Checked)
            cmd.Parameters.AddWithValue("@ConProduceDanger", chkConProduceDanger.Checked)
            cmd.Parameters.AddWithValue("@ConProducePeople", chkConproducePeople.Checked)
            cmd.Parameters.AddWithValue("@ConProduceManage", chkConProduceManage.Checked)
            cmd.Parameters.AddWithValue("@ConProduceMade", chkConProduceMade.Checked)
            Conn.Open()
            Dim ResponseText As Integer = DirectCast(cmd.ExecuteScalar, Int32).ToString
            If (ResponseText = 1) Then
                lblResponseResult.Text = "<img src='../images/icons/message-boxes/confirmation.png' /> บันทึกลงฐานข้อมูลเรียบร้อยแล้ว"
                If (DocumentStatusID = 3) Then 'Goto next step
                    AlertRedirects("redirect", pnlHint.ClientID, 3, "Verification.aspx")
                Else
                    AlertBoxs("alert", pnlHint.ClientID, 2)
                End If
            End If
        Else
            Dim sb As New System.Text.StringBuilder()
            sb.Append("<script type = 'text/javascript'>")
            sb.Append("window.onload=function(){")
            sb.Append("javascript:history.back();return false;")
            sb.Append("};")
            sb.Append("</script>")
            ClientScript.RegisterClientScriptBlock(Me.GetType(), "back", sb.ToString())
        End If
    Catch ex As Exception
        Response.Write("Error Save: " & ex.Message)
    Finally
        Conn.Close()

    End Try

End Sub

我认为这是一个引用

的问题

1 个答案:

答案 0 :(得分:2)

错误请指明是什么。您发布的代码似乎是两个语法错误

  Dim ResponseText As Integer = DirectCast(cmd.ExecuteScalar, Int32).ToString
        If (ResponseText = 1) 

您正在为ResponseText分配一个字符串值,该值是一个整数 第二个是如果条件应该是双等于If (ResponseText == 1),因为它不是用于赋值的赋值单等号