We help IT Professionals succeed at work.

An SqlParameter with ParameterName 'yada yada' is not contained by this SqlParameterCollection.

snappy3273
snappy3273 asked
on
Medium Priority
1,242 Views
Last Modified: 2011-09-20
Hello everyone,
I've got a sub that's killing me here in that I've checked all names and it still bombs on the following line:
 
cmdGetFile.Parameters("@Attachment_ID").Value = Request("AttachmentID").ToString

An SqlParameter with ParameterName '@Attachment_ID' is not contained by this SqlParameterCollection.


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        strGet = "SELECT FileSize, FileName, FileData, ContentType FROM tblAttachments WHERE (Attachment_ID = @Attachment_ID)"
        connectString = "Data Source=TXXPW320;Initial Catalog=ezscan;Integrated Security=True"
        Dim dbConn As New Data.SqlClient.SqlConnection(connectString)
        'setup command object
        Dim cmdGetFile As New Data.SqlClient.SqlCommand(strGet, dbConn)
        dbConn.Open()
        Dim dr As System.Data.SqlClient.SqlDataReader
        cmdGetFile.Parameters("@Attachment_ID").Value = Request("AttachmentID").ToString
        dbConn.Open()
        dr = cmdGetFile.ExecuteReader
        If dr.Read Then
            Response.ContentType = dr("ContentType").ToString
            Response.OutputStream.Write(CType(dr("FileData"), Byte()), 0, CInt(dr("FileSize")))
            Response.AddHeader("Content-Disposition", "attachment;filename=" + dr("FileName").ToString())
        Else
            mf.RW("File Not Found.")
        End If
    End Sub


Any suggestions would be greatly appreciated.
Thanks in advance,
S
Comment
Watch Question

Commented:
add

 cmdGetFile.Parameters.Add("@Attachment_ID")

before

 cmdGetFile.Parameters("@Attachment_ID").Value = Request("AttachmentID").ToString


it may have to have a value so may have to use this line instead and just replace your previous line

 cmdGetFile.Parameters.Add("@Attachment_ID",Request("AttachmentID").ToString)

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
When using this:
cmdGetFile.Parameters.AddWithValue("@Attachment_ID",Request("AttachmentID").ToString)

It rec i change "add" to "addwithvalue", once I ran it, it did this.
I get a NullReference exception: Object reference not set to an instance of an object.

I have no records in my DB though and am trying to get this upload script to work.
Thanks,
S
is the Nullref exception thrown coz you don't have "AttachmentID" in Request object?
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.