Hi!
I'm trying to save a jpg pictures to a SQL database. The application first opens a dialog box to select the file (picture) I want to store in the database. When selected and pressing button no.2 it should be saved into the databade, but...
I'm not sure what I'm doing wrong, but when I do the debugging I get the next error messages:
1. The ConnectionString property has not been initialized.
Here is the code I'm using:
Option
Strict OnImports
System.Data.SqlClientImports
System.IOPublic
Class Form1 Inherits System.Windows.Forms.FormPrivate Sub PhotoBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Me.Validate() Me.PhotoBindingSource.EndEdit() Me.PhotoTableAdapter.Update(Me.ForestphotoDataSet1.photo) End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'TODO: This line of code loads data into the 'ForestphotoDataSet1.photo' table. You can move, or remove it, as needed.
End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click With OpenFileDialog1
.InitialDirectory =
"C:\".Filter =
"All Files|*.*|Bitmaps|*.bmp|GIFs|*.gif|JPEGs|*.jpg".FilterIndex = 2
End With If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then With PictureBox1.Image = Image.FromFile(
Me.OpenFileDialog1.FileName).SizeMode = PictureBoxSizeMode.CenterImage
End With End If Me.Label1.Text = Me.OpenFileDialog1.FileName.ToString End Sub#
Region "Save" Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Dim arrFilename() As String = Split(Label1.Text, "\")Array.Reverse(arrFilename)
Dim ms As New MemoryStream()PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat)
Dim arrImage() As Byte = ms.GetBufferms.Close()
Dim isConnecting As Boolean = True While isConnecting
Dim forestphotoConnection As New SqlClient.SqlConnection() Dim strSQL As String = _"INSERT INTO Picture (Filename, Picture)" & _"VALUES (@Filename, @Picture)" Dim cmd As New SqlCommand(strSQL, forestphotoConnection) With cmd Parameters.Add(New SqlParameter("@name", _
SqlDbType.NChar, 50)).Value = arrFilename(0)
.Parameters.Add(
New SqlParameter("@photo", _SqlDbType.Image)).Value = arrImage
End With forestphotoConnection.Open()cmd.ExecuteNonQuery()
forestphotoConnection.Close()
End While End Sub
#
End Region Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click End SubEnd
Class

How to save images to sql?
Ed Landau
In your Button3_Click handler, you create a forestphotoConnection object, but don't tell it how to connect to the database. Try changing the following line Substituting MySqlConnectionString as appropriate.
Dim forestphotoConnection As New SqlClient.SqlConnection(MySqlConnectionString)
Jim Wooley
http://devauthority.com/blogs/jwooley/default.aspx
kalpesh Sr.Software Engineer
OK! Thank you for your help. I changed the code a bit, so now I enter the name of the image into a TextBox and than save it to the SQL database!
Jeff Lucovsky
....
Parameters.Add(New SqlParameter("@name", SqlDbType.NChar, 50)).Value = arrFilename
I thing that it must be something lake this (I'm thing that first element is zero):
Markku
twostars
....
Parameters.Add(New SqlParameter("@name", SqlDbType.NChar, 50)).Value = arrFilename
I thing that it must be something lake this:
Markku
SkipperS
Thank you for the tip!
Now I think I solved this one, but still I get an error message saying "Failed to convert parameter value from a String[] to a String."
In the SQL table I defined the field (where the image should be stored to) as an "image" data type.
So, what I'm I (still) doing wrong. Here is the complete code:
Option
Strict OnImports
System.Data.SqlClientImports
System.IOPublic
Class Form1 Inherits System.Windows.Forms.Form
Private Sub PhotoBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Me.Validate() Me.PhotoBindingSource.EndEdit() Me.PhotoTableAdapter.Update(Me.ForestphotoDataSet1.photo) End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'TODO: This line of code loads data into the 'ForestphotoDataSet1.photo' table. You can move, or remove it, as needed.
End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click With OpenFileDialog1.InitialDirectory =
"C:\".Filter =
"All Files|*.*|Bitmaps|*.bmp|GIFs|*.gif|JPEGs|*.jpg".FilterIndex = 2
End With If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then With PictureBox1.Image = Image.FromFile(
Me.OpenFileDialog1.FileName).SizeMode = PictureBoxSizeMode.CenterImage
End With End If Me.Label1.Text = Me.OpenFileDialog1.FileName.ToString End Sub#
Region "Save" Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Dim arrFilename() As String = Split(Label1.Text, "\")Array.Reverse(arrFilename)
Dim ms As New MemoryStream()PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat)
Dim arrImage() As Byte = ms.GetBuffer ms.Close() Dim phID As Integer = 0
Dim forphoconn As New SqlClient.SqlConnectionforphoconn.ConnectionString =
My.Settings.forestphotoConnectionString Dim strSQL As String = "INSERT INTO Photo(photoID, name, photo)" & "VALUES (@photoID, @name, @photo)" Dim cmd As New SqlCommand(strSQL, forphoconn) With cmd .Parameters.Add(New SqlParameter("@photoID", SqlDbType.Int)).Value = phID.Parameters.Add(
New SqlParameter("@name", SqlDbType.NChar, 50)).Value = arrFilename.Parameters.Add(
New SqlParameter("@photo", SqlDbType.Image)).Value = arrImage End With forphoconn.Open()cmd.ExecuteNonQuery()
forphoconn.Close()
End Sub#
End Region Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click End SubEnd
Class