Save and retrive images from the gridview.

 

    Private Sub btnUpload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpload.Click

        Try

            'save Images

            If Trim(txtImg.Text) = "" Then

                MsgBox("Please select a image.")

                Exit Sub

            End If

            Dim fs As New FileStream(Trim(txtImg.Text), FileMode.Open)

            Dim Data() As Byte = New [Byte](fs.Length) {}

            fs.Read(Data, 0, fs.Length)

 

            Dim con As New System.Data.SqlClient.SqlConnection("data source=mt5;initial catalog=master;user id=sa;password=mms")

            con.Open()

            Dim cmd As New System.Data.SqlClient.SqlCommand("insert into SampleImageTable values(@imgsamgetimage)",con);

 

            cmd.Parameters.Add("@imgsamgetimage", Data)

            cmd.ExecuteNonQuery()

            con.Close()

            fs.Close()

        Catch ex As System.Data.SqlClient.SqlException

            MsgBox(ex.Message)

        End Try

    End Sub

 

 

 

 

    Private Sub btnDownload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDownload.Click

 

‘ download and display in picturebox

        Dim con As New System.Data.SqlClient.SqlConnection("data source=mt5;initial catalog=master;user id=sa;password=mms")

        con.Open()

        Dim cmd As New System.Data.SqlClient.SqlCommand("select * from SampleImageTable")

        cmd.Connection = con

        cmd.CommandType = CommandType.Text

        Dim da As New System.Data.SqlClient.SqlDataAdapter(cmd)

        Dim ds As New DataSet()

        da.Fill(ds)

        Dim bits As Byte() = CType(ds.Tables(0).Rows(0).Item(0), Byte())

        Dim memorybits As New MemoryStream(bits)

        Dim bitmap As New Bitmap(memorybits)

        PictureBox1.Image = bitmap

    End Sub