User-137998610 posted
May this might be of any help:
Protected Sub Upload_Click(ByVal sender
As Object,
ByVal e As System.EventArgs)
Handles Upload.Click
Dim fileUpload1
As FileUpload = CType(Me.FindControl("fileUpload1"), FileUpload)
'Make sure a file has been successfully uploaded
If fileUpload1.PostedFile
Is Nothing
OrElse String.IsNullOrEmpty(fileUpload1.PostedFile.FileName)
OrElse fileUpload1.PostedFile.InputStream
Is Nothing Then
Label1.Text =
"Please Upload Valid picture file"
Exit Sub
End
If
'Make sure we are dealing with a JPG or GIF file
Dim extension
As String = System.IO.Path.GetExtension(fileUpload1.PostedFile.FileName).ToLower()
Dim MIMEType
As String =
Nothing
Select
Case extension
Case ".gif"
MIMEType =
"image/gif"
Case ".jpg",
".jpeg", ".jpe"
MIMEType =
"image/jpeg"
Case ".png"
MIMEType =
"image/png"
Case Else
'Invalid file type uploaded
Label1.Text =
"Not a Valid file format"
Exit Sub
End
Select
'Connect to the database and insert a new record into Products
Using myConnection
As New SqlConnection(ConfigurationManager.ConnectionStrings("ImageGalleryConnectionString").ConnectionString)
Const SQL As String =
"INSERT INTO [Pictures] ([Title], [MIMEType], [Image]) VALUES (@Title, @MIMEType, @ImageData)"
Dim myCommand As
New SqlCommand(SQL, myConnection)
myCommand.Parameters.AddWithValue("@Title", TextBox1.Text.Trim())
myCommand.Parameters.AddWithValue("@MIMEType", MIMEType)
'Load FileUpload's InputStream into Byte array
Dim imageBytes(fileUpload1.PostedFile.InputStream.Length)
As Byte
fileUpload1.PostedFile.InputStream.Read(imageBytes, 0, imageBytes.Length)
myCommand.Parameters.AddWithValue("@ImageData", imageBytes)
myConnection.Open()
myCommand.ExecuteNonQuery()
myConnection.Close()
End
Using
End
Sub
Mark it as answer if it helps you.