How do I use a form field value in this code to insert binary data into a database

The code listed a cut from an asp.net VB code behind page. I am able to insert word, excel and pdf MIMEtypes to a table in a sql server 2008 database. I am also now able to retrieve and display the the binary image without any problems. The last and absolute final problem I have is the need to add an id value to the same table. The actual id field is named rid in the table. In this line, cmd.Parameters.Add("@rid", SqlDbType.Int).Value = xxxxx, if I replace the xxxxxx with a hardcoded value of 1, 2 or 3 or any int value everything works fine. However, this is not going to be a hardcoded value but instead is going to be a dynamic value. Instead it is a hidded field with the value being assigned that is derived from an SQLDataSource. I am binding the hidden field which is named rid to the value from the SQL datasource. I tried a lot of things but mostly what I get is (Failed to convert parameter value from a HiddenField to a String.). Please don't make the assumption that I know code syntax because I don't. I can read code on a intermediate level but writing it is totally different. Any suggestions will likely have to be spelled or written out for me. The fact I've been able to get this far is a testament to Experts Exchange. What should I replace the xxxxx with. What else is needed.

'insert the file into database

            Dim strQuery As String = "insert into Requestdocs(rid, Name, ContentType, Data) values (@rid, @Name, @ContentType, @Data)"

            Dim cmd As New SqlCommand(strQuery)

            cmd.Parameters.Add("@rid", SqlDbType.Int).Value = xxxxx

            cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = filename

            cmd.Parameters.Add("@ContentType", SqlDbType.VarChar).Value() = contenttype

            cmd.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes

            InsertUpdateData(cmd)
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Partial Class Addreqdocs
    Inherits System.Web.UI.Page

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

    End Sub

    Public Function GetData(ByVal cmd As SqlCommand) As DataTable
        Dim dt As New DataTable
        Dim strConnString As String = System.Configuration.ConfigurationManager.ConnectionStrings("reportfeedRequirementsConnectionString").ConnectionString
        Dim con As New SqlConnection(strConnString)
        Dim sda As New SqlDataAdapter
        cmd.CommandType = CommandType.Text
        cmd.Connection = con
        Try
            con.Open()
            sda.SelectCommand = cmd
            sda.Fill(dt)
            Return dt
        Catch ex As Exception
            Response.Write(ex.Message)
            Return Nothing
        Finally
            con.Close()
            sda.Dispose()
            con.Dispose()
        End Try
    End Function




    Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As EventArgs)

        ' Read the file and convert it to Byte Array 

        Dim filePath As String = FileUpload1.PostedFile.FileName

        Dim filename As String = Path.GetFileName(filePath)

        Dim ext As String = Path.GetExtension(filename)

        Dim contenttype As String = String.Empty



        'Set the contenttype based on File Extension

        Select Case ext

            Case ".doc"

                contenttype = "application/vnd.ms-word"

                Exit Select

            Case ".docx"

                contenttype = "application/vnd.ms-word"

                Exit Select

            Case ".xls"

                contenttype = "application/vnd.ms-excel"

                Exit Select

            Case ".xlsx"

                contenttype = "application/vnd.ms-excel"

                Exit Select

            Case ".jpg"

                contenttype = "image/jpg"

                Exit Select

            Case ".png"

                contenttype = "image/png"

                Exit Select

            Case ".gif"

                contenttype = "image/gif"

                Exit Select

            Case ".pdf"

                contenttype = "application/pdf"

                Exit Select

        End Select

        If contenttype <> String.Empty Then

            Dim fs As Stream = FileUpload1.PostedFile.InputStream

            Dim br As New BinaryReader(fs)

            Dim bytes As Byte() = br.ReadBytes(fs.Length)



            'insert the file into database 

            Dim strQuery As String = "insert into Requestdocs(rid, Name, ContentType, Data) values (@rid, @Name, @ContentType, @Data)"

            Dim cmd As New SqlCommand(strQuery)

            cmd.Parameters.Add("@rid", SqlDbType.VarChar).Value = xxxxx

            cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = filename

            cmd.Parameters.Add("@ContentType", SqlDbType.VarChar).Value() = contenttype

            cmd.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes

            InsertUpdateData(cmd)

            lblMessage.ForeColor = System.Drawing.Color.Green

            lblMessage.Text = "File Uploaded Successfully"

        Else

            lblMessage.ForeColor = System.Drawing.Color.Red

            lblMessage.Text = "File format not recognized." & " Upload Image/Word/PDF/Excel formats"

        End If

    End Sub




    Public Function InsertUpdateData(ByVal cmd As SqlCommand) As Boolean
        Dim strConnString As String = System.Configuration.ConfigurationManager.ConnectionStrings("reportfeedRequirementsConnectionString").ConnectionString
        Dim con As New SqlConnection(strConnString)
        cmd.CommandType = CommandType.Text
        cmd.Connection = con
        Try
            con.Open()
            cmd.ExecuteNonQuery()
            Return True
        Catch ex As Exception
            Response.Write(ex.Message)
            Return False
        Finally
            con.Close()
            con.Dispose()
        End Try
    End Function

End Class

Open in new window

binary.jpg
derrekdeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

derrekdeveloperAuthor Commented:
The last thing I tried was to change the rid field from hidden to a textbox that would allow me to directly enter the int value. I received the following message:

Failed to convert parameter value from a TextBox to a Int32.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Arthur_WoodCommented:
What line of code did you attempt to use for this ("The last thing I tried was to change the rid field from hidden to a  textbox that would allow me to directly enter the int value") ?

AW
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.