ASP.Net SqlCommand Parameter Error: Object reference not set to an instance of an object

Last Reply 11 months ago By Sumeet

Posted 11 months ago

Sir,

I am getting Object reference not set to an instance of an object. on command parameter while updating Bytes in table. I am getting bytes length but still giving me Null Reference error while passing variable in command. 

why?

below is my code: 

    Protected Sub btnFileupload_Click(sender As Object, e As EventArgs) Handles btnFileupload.Click
        'str = vali.tabmgmtdocuments(fileExtension.PostedFile.InputStream, filePrevManagingCommittee.PostedFile.InputStream, fileMinutesMeeting.PostedFile.InputStream, fileEnrollment.PostedFile.InputStream, fileStaffMember.PostedFile.InputStream, fileByLaws.PostedFile.InputStream, filePrev3Yr.PostedFile.InputStream, file10Members.PostedFile.InputStream)

        Dim filename As String = ""
        Dim filePath As String = ""
        'Dim fs As FileStream = New FileStream()
        Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("dsejConnectionString").ConnectionString)
        Dim cmdInsert As SqlCommand = Nothing
        Dim query As String = "update management_approvaltbl set "

        If Not fileExtension Is Nothing Then
            If fileExtension.HasFile Then
                query += "prev_recognition_ext_affli_order_copy=@prev_recognition_ext_affli_order_copy,"
            End If
        ElseIf Not Session("fileExtension") Is Nothing Then
            query += "prev_recognition_ext_affli_order_copy=@prev_recognition_ext_affli_order_copy,"
        End If

        query = query.Substring(0, (query.Length() - 1))
        query = query & " where mgmtapprid=@mgmtapprid"

        '1
        If Not fileExtension Is Nothing Then
            If fileExtension.HasFile Then                
                lfileExtension.Text = filename
                Dim b1 As Byte() = toBinary(filename)
                cmdInsert.Parameters.AddWithValue("@prev_recognition_ext_affli_order_copy", b1)
            End If            
        ElseIf Not Session("fileExtension") Is Nothing Then
            fileExtension = DirectCast(Session("fileExtension"), FileUpload)
            filename = Now.Hour & Now.Minute & Now.Second & Now.Millisecond & "_" & fileExtension.PostedFile.FileName.ToString()
            fileExtension.PostedFile.SaveAs(Server.MapPath("Photo/" & filename))
            If CheckImage(filename) <> True Then
                lblmsg.Text = "Kindly Upload Proper File"
                lfileExtension.Text = "Not a valid .jpg file"
                Exit Sub
            End If
            lfileExtension.Text = filename
            Dim b1_ As Byte() = toBinary(filename)
            cmdInsert.Parameters.AddWithValue("@prev_recognition_ext_affli_order_copy", b1_)
        End If
        
        cmdInsert.Parameters.AddWithValue("@mgmtapprid", Session("mgmtvalue"))
        con.Open()
        Dim i As Integer = cmdInsert.ExecuteNonQuery()
        con.Close()
        If i > 0 Then
            lblmsg.Text = "Record saved successfully."
            Menu1.Items(2).Enabled = True
            Menu1.Items(2).Selected = True
            MultiView1.ActiveViewIndex = 2
        Else
            lblmsg.Text = "Some error occured while saving the data."
        End If
    End Sub

 

You are viewing reply posted by: Sumeet 11 months ago.
Posted 11 months ago

Got it.

From top on line : 

Dim cmdInsert As SqlCommand = Nothing

Here New must be created for SqlCommand class. Therefore it should be 

Dim cmdInsert As New SqlCommand()

And also connection property is missing, therefore a line should be added below line : 

query = query & " where mgmtapprid=@mgmtapprid" 

Add below line :

cmdInsert = New SqlCommand(query, con)