Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to upload and retrieve images from SQL database using vb.net

form1_case 1. Images must be selected by using a Button in form1
form1_case 2. After selection of images Im gonna use a save button to upload it to SQL database
form1_case3. There are two columns in SQL database table. (1) image_id (int), picture(image).

form2_case 1. From a Second form2 Im gonna search the id of the images that is uploaded to SQL database.
form2_care2. That image will be placed in a picturebox1.image.

Note. Please provide an easier method.

What I have tried:

VB
Imports System.IO
Imports System.Data.SqlClient

Public Class fac_reg

    Dim connection As SqlConnection
    Dim command As SqlCommand
 
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Me.Close()
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        connection = New SqlConnection("HIDDEN")
        Dim reader As SqlDataReader
        Try
            connection.Open()
            Dim defText As String = "Not Modified".ToString
            Dim getDate As Date = Date.Now.ToString(" yyyy-MM-dd ")
            Dim facDOB As Date = DateTimePicker1.Value.ToString(" yyyy-MM-dd ")
            Dim query As String
            query = "INSERT into faculty(id,dept,sub,pre,name,dob,sex,addr,city,state,pin,country,nat,mob,email,uname,pword,sec,ans,image,reg,mod)values('" & TextBox1.Text & "','" & ComboBox5.Text & "','" & ComboBox4.Text & "','" & ComboBox1.Text & "','" & TextBox2.Text & "','" & facDOB & "','" & ComboBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "','" & TextBox14.Text & "','" & TextBox5.Text & "','" & TextBox8.Text & "','" & TextBox9.Text & "','" & TextBox6.Text & "','" & TextBox7.Text & "','" & TextBox10.Text & "','" & TextBox12.Text & "','" & ComboBox3.Text & "','" & TextBox13.Text & "','@image','" & getDate & "','" & defText & "')"
            Dim ms As New MemoryStream
            PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat)
            command.Parameters.Add("@image", SqlDbType.Image).Value = ms.ToArray
            command = New SqlCommand(query, connection)
            reader = command.ExecuteReader
            MsgBox("Faculty registration completed")
            connection.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            TextBox1.Clear() : TextBox2.Clear() : TextBox3.Clear() : TextBox4.Clear() : TextBox5.Clear() : TextBox6.Clear() : TextBox7.Clear() : TextBox8.Clear() : TextBox9.Clear() : TextBox10.Clear() : TextBox11.Clear() : TextBox12.Clear() : TextBox13.Clear() : TextBox14.Clear()
            ComboBox1.Text = String.Empty
            ComboBox2.Text = String.Empty
            ComboBox3.Text = String.Empty
            ComboBox4.Text = String.Empty
            ComboBox5.Text = String.Empty
            PictureBox1.Image = Nothing
        End Try
        connection.Dispose()
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim openbrowser As New OpenFileDialog
        openbrowser.Filter = "Choose Image(*.jpg;*.png)|*.jpg;*.png"
        If openbrowser.ShowDialog = Windows.Forms.DialogResult.OK Then
            PictureBox1.Image = Image.FromFile(openbrowser.FileName)
        End If
    End Sub
End Class
Posted
Updated 22-Feb-18 1:29am
v2

1 solution

try this blog-
[^]


string filePath = Server.MapPath("APP_DATA/TestDoc.docx");

string filename = Path.GetFileName(filePath);

 

FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);

BinaryReader br = new BinaryReader(fs);

Byte[] bytes = br.ReadBytes((Int32)fs.Length);

br.Close();

fs.Close();
 
Share this answer
 
Comments
Babai JermenKeller Sasmal 22-Feb-18 7:43am    
Your answer not clear to me. Will you please brief whatever you provide ?
Babai JermenKeller Sasmal 22-Feb-18 7:43am    
I think you should have look at the question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900