Hello Everyone I am trying to write image files to sql server using the following code. I have recieved the following error message
Failed to convert parameter value from a String to a Byte[]. Please help. I am sure it is a problem gathering the iostream. I am not very familiar. Any help is gretaly appreciated.
Thanks in advance
Here is the code:
'Dim User As MembershipUser = Membership.GetUser(CreateUserWizard1.UserName)
'Dim t As TextBox = DirectCast(FormView1.FindControl("aspnet_UserID"), TextBox)
Dim objConnAs SqlConnectionDim objComAs SqlCommand
IfMe.FileUpload1.HasFileThen
Dim fileExtensionAsString
Dim fileOKAsBoolean =False
fileExtension = System.IO.Path. _
GetExtension(FileUpload1.FileName).ToLower()
Dim allowedExtensionsAsString() = _{".jpg",".jpeg",".png",".gif",".mwv"}
For iAsInteger = 0To allowedExtensions.Length - 1If fileExtension = allowedExtensions(i)Then
fileOK =True
EndIf
Next
'Try
Dim imagestreamAs System.IO.Stream = FileUpload1.FileContentDim data()AsByte
ReDim data(imagestream.Length - 1)imagestream.Read(data, 0, imagestream.Length)
imagestream.Close()
objConn =New SqlConnection(strNewConnection)objCom =New SqlCommand("insert into ProfileImagesAndDocs(aspnet_userid,img_name,img_data,img_contenttype)values(@.aspnet_userid,@.imagename,@.Picture,@.CategoryName)", objConn)
'--------------
'this is the aspnet_userid
Dim useridparameterAs SqlParameter =New SqlParameter("@.aspnet_userid", SqlDbType.VarChar)useridparameter.Value =Me.aspnet_userid.TextobjCom.Parameters.Add(useridparameter)
'--------------
'This is the image name
Dim imagenameparameterAs SqlParameter =New SqlParameter("@.imagename", SqlDbType.VarChar)imagenameparameter.Value =Me.FileUpload1.FileNameobjCom.Parameters.Add(imagenameparameter)
'--------------
'this is the picture data
Dim pictureParameterAs SqlParameter =New SqlParameter("@.Picture", SqlDbType.Image)pictureParameter.Value = data
objCom.Parameters.Add(pictureParameter)
'--------------
'this is the profile area or category i.e. video intro
Dim categorynameParameterAs SqlParameter =New SqlParameter("@.CategoryName", SqlDbType.VarChar)pictureParameter.Value ="IntroVideo"
objCom.Parameters.Add(categorynameParameter)
objConn.Open()
objCom.ExecuteNonQuery()
objConn.Close()
'bookmark
Label1.Text ="File uploaded!"
'Catch ex As Exception
Label1.Text ="File could not be uploaded."
EndIf
Hi,
You may use FileStream to read the image content and save it as SqlDbType.Image into your database. See the following sample.
|||Private Function GetFile()As String''' GetFileDim fileAs HttpPostedFile = File1.PostedFilefileName = file.FileNameReturn fileNameEnd Function''' ReadFilePrivate Function ReadFile()As Byte()Dim fileAs FileStream = File.OpenRead(GetFile())Dim contentAs Byte() =New Byte(file.Length - 1) {}file.Read(content, 0, content.Length)file.Close()Return contentEnd FunctionPrivate Sub WriteImage()Dim commAs SqlCommand = conn.CreateCommand()comm.CommandText ="insert into images(image,type) values(@.image,@.type)"comm.CommandType = CommandType.TextDim paramAs SqlParameter = comm.Parameters.Add("@.image", SqlDbType.Image)param.Value = ReadFile()param = comm.Parameters.Add("@.type", SqlDbType.NVarChar)param.Value = GetContentType(New FileInfo(fileName).Extension.Remove(0, 1))If comm.ExecuteNonQuery() = 1ThenResponse.Write("Successful")ElseResponse.Write("Fail")End Ifconn.Close()End SubPrivate Function GetContentType(ByVal extensionAs String)As StringDim typeAs String =""If extension.Equals("jpg")OrElse extension.Equals("JPG")Thentype ="jpeg"Elsetype = extensionEnd IfReturn"image/" + typeEnd Function
Thanks.
Thanks the code above worked very well. I am having one other difficulty when I select a file with a *.wmv extension the file is not read and the script halts. Any Ideas?! and thanks again for the solution.
No comments:
Post a Comment