Question uploading Image

Bhutanesedude

New member
Joined
Jun 12, 2013
Messages
4
Programming Experience
Beginner
Help needed. I am newbie on VB.NET and looking to upload Image to DB converting to Byte.

I have a BD where in a table named "tbemp" with fields "eid, name, image".

My application have a Database Access file DBAccess.vb with the following code:
VB.NET:
Imports System.Data.SqlClientImports System.Data
Imports Microsoft.VisualBasic
Public Class DBAccess
    Shared cs As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=" & Application.StartupPath.Replace("\bin\Debug", "") & "\eis.mdf;Integrated Security=True;User Instance=True")
    Public Shared Function SaveData(ByVal qry As String) As Boolean


        Dim cmd As New SqlCommand(qry, cs)


        Try
            cs.Open()
            cmd.ExecuteNonQuery()
            Return True
        Catch ex As Exception
            Return False
        Finally
            cs.Close()
        End Try
    End Function


    Public Shared Function FetchData(ByVal qry As String) As DataSet


        Dim cmd As New SqlCommand(qry, cs)


        Dim ds As New DataSet


        Dim adp As New SqlDataAdapter(cmd)


        adp.Fill(ds)


        Return ds
    End Function
    Public Shared Function saveGrid(ByVal query As String, ByVal ds As DataSet) As Boolean
        Try
            Dim da As New SqlDataAdapter(query, cs)
            Dim cb As New SqlCommandBuilder(da)
            da.InsertCommand = cb.GetInsertCommand
            da.UpdateCommand = cb.GetUpdateCommand
            da.DeleteCommand = cb.GetDeleteCommand
            da.Update(ds)
            Return True
        Catch ex As Exception
            Return False
        End Try




    End Function


End Class

Now I also have a function to convert the image file to Byte (Got it from google) as:
VB.NET:
Public Function convertimage(ByVal myImage As Image) As Byte()        Dim mstream As New MemoryStream()                               
        myImage.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg)  


        Dim myBytes(mstream.Length - 1) As Byte                         


        mstream.Position = 0                                            


        mstream.Read(myBytes, 0, mstream.Length)                        
        Return myBytes


    End Function

Now when I click on save button, I usually use this way of code to save data into Data Base:
VB.NET:
Dim qry As String
        qry = "INSERT INTO tbemp values ('" + eid.Text + "','" + name.Text + "');"
        If DBAccess.SaveData(qry) Then
            MessageBox.Show("Data Saved", "Congratulations", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Me.Hide()
        Else
            MessageBox.Show("Error saving", "Error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error)
        End If

Now my question is how can I save the image file (converted into Byte from the function above) into Database using the above like qry? Please help me.
 
First of all, if you're using an Access database then you need to be using OleDb rather than SqlClient, which is for SQL Server specifically.

As for the question, you treat binary data in exactly the same way as you treat any other data. The problem is that you're treating all your data in correctly. Follow the Blog link in my signature below and check out my post on Parameters In ADO.NET to learn how to properly insert values into SQL code.
 
Thank you jmcilhinney, but I am using SQL Database. Should I understand that I am treating all my Data "incorrectly"? More insights would be appreciated.
 
Thank you jmcilhinney, but I am using SQL Database.
You mean that you're using SQL Server. I took this:
My application have a Database Access file DBAccess.vb
to mean that you were using an Access database but now I see that you actually meant that your data access code was in that source file.
Should I understand that I am treating all my Data "incorrectly"?
I said that you're treating all your data incorrectly. Where I'm from, that means that you're treating all your data incorrectly. I'm not aware of any other interpretation.
More insights would be appreciated.
Then why haven't you done as instructed and read my blog post? I didn't direct you to it for my health.
 

Latest posts

Back
Top