M.L. Martin
asked on
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("@Conte ntType", SqlDbType.VarChar).Value() = contenttype
cmd.Parameters.Add("@Data" , SqlDbType.Binary).Value = bytes
InsertUpdateData(cmd)
'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",
cmd.Parameters.Add("@Name"
cmd.Parameters.Add("@Conte
cmd.Parameters.Add("@Data"
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
binary.jpg
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
AW