Reputation: 23
I am having trouble in browse & then save an image to SQL Server.ALso I want to retrieve that image.
This is the code to browse image & show it in PictureBox1 Control
Public Sub SelectImage()
With OpenFileDialog1
'.InitialDirectory = "C:\"
.Filter = "All Files|*.*|Bitmaps|*.bmp|GIFs|*.gif|JPEGs|*.jpg"
.FilterIndex = 4
End With
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
PictureBox1.BorderStyle = BorderStyle.Fixed3D
End If
I have created a image feild in a table & this is my stored procedure
Public Sub Insert_Update_Personal()
Dim ds As DataSet = New DataSet()
Dim cmd As SqlCommand = New SqlCommand("sp_Insert_Update_Personal", con)
con.Open()
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@picture", UploadImage)
LastPID = Convert.ToInt32(cmd.ExecuteScalar())
cmd.Dispose()
con.Close()
But don't know how to pass it to image parameter & Please suggest .Also is there any way that I can save image on Local drive & just save the url /location/name in database?
Which method would be better
Thanks
Upvotes: 0
Views: 4858
Reputation: 195
Just another approach, try this to save your image file localy and load them without storeing them in database. Note: picPath is the path of the image file, you can use openfiledialog or other method to set it in lblPhoto.text
Private Sub SavePhoto()
Try
Dim picPath As String = ""
Dim picName As String = ""
picPath = lblPhoto.Text
If lblPhoto.Text = "" Then
picName = (txtimage.Text + ".tus")
End If
If picPath <> "" And (File.Exists(AppDomain.CurrentDomain.BaseDirectory + "\\Photo\\" + picName) = True) Then
File.Delete(AppDomain.CurrentDomain.BaseDirectory + "\\Photo\\" + picName)
Else
picName = (txtimage.Text + ".tus")
File.Copy(picPath, AppDomain.CurrentDomain.BaseDirectory + "\\Photo\\" + picName, True)
End If
Catch ex As Exception
End Try
End Sub
Now to load the saved photo do the following
If (File.Exists(AppDomain.CurrentDomain.BaseDirectory + "Photo\" + txtimage.Text + ".tus") = True) Then
PictureBox1.Image = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "Photo\" + txtPolicy_No.Text + ".tus", True)
End If
Upvotes: 1