1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

save image in sql database from picturebox in vb 2008

Discussion in 'SQL Server' started by Mahaveer Sharma, Sep 7, 2010.

  1. Mahaveer Sharma

    Mahaveer Sharma New Member

    Joined:
    Sep 7, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    hi,
    i want to save an image from picturebox to sql database. for it i have written the code as under:
    Code:
    Imports System.Data.SqlClient
    Imports System.IO
    Imports System.Data
    Imports System.Data.SqlTypes
    Imports System.Drawing.Imaging
    
    Public Class Form1
        Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
            OpenFileDialog1.Title = "Get Image"
            With OpenFileDialog1
                .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.None
            End If
    
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Try
                Dim txt1 As String = TextBox1.Text
                Dim txt2 As String = TextBox2.Text
                Dim flag As Integer
                Dim ms As New MemoryStream
                PictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
                Dim bytes() As Byte = ms.ToArray
                Dim img As String = Convert.ToBase64String(bytes)
                Dim con As New OleDb.OleDbConnection("provider=sqloledb; server=MICROSOF-E56695\SQLEXPRESS; database=mahaveer; userid=; password=;Trusted_Connection=yes;")
                con.Open()
                Dim cmd As New OleDb.OleDbCommand("insert CheckStaff values('" + txt1 + "','" + txt2 + "','" + img + "')", con)
                flag = cmd.ExecuteNonQuery
                If flag = 1 Then
                    MsgBox("Saved..........")
                End If
            Catch ex As Exception
                MsgBox("Exception.........")
                MsgBox(ex.ToString)
            End Try
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Me.Visible = False
            Form2.Visible = True
        End Sub
    End Class
    -------------------------------------------------------------------------
    and i also want to get image back on picturebox from sql database , for it i have written this code:
    Imports System.Data
    Imports System.Data.SqlTypes
    Imports System.Drawing.Imaging
    Public Class Form2
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Try
                Dim txt1 As String = TextBox1.Text
                Dim ds As New DataSet
                Dim dt As New DataTable
    
                Dim ms As New MemoryStream
                Dim bytes() As Byte = ms.ToArray
                Dim img As String = Convert.ToBase64String(bytes)
                Dim rbytes() As Byte = Convert.FromBase64String(img)
                Dim rms As New MemoryStream(rbytes, 0, rbytes.Length)
    
                Dim con2 As New OleDb.OleDbConnection("provider=sqloledb; server=MICROSOF-E56695\SQLEXPRESS; database=mahaveer; userid=; password=;Trusted_Connection=yes;")
                con2.Open()
                Dim cmd As New OleDb.OleDbDataAdapter("select*from CheckStaff where id=1001", con2)
                cmd.Fill(ds, "CheckStaff")
                TextBox2.Text = ds.Tables(0).Rows(0)("id")
                TextBox3.Text = ds.Tables(0).Rows(0)("name")
                PictureBox1.Image = Image.FromStream(ms)
                'PictureBox1.Image = ds.Tables(0).Rows(0)("image")
            Catch ex As Exception
                MsgBox("Exception in getting Records")
                MsgBox(ex.ToString)
            End Try
    
        End Sub
    End Class
    ---------------------------------------
    i am trying to get image back in my picturebox... but there is an exception .....
    please help me
     
    Last edited by a moderator: Sep 8, 2010

Share This Page