Imports System.Data.OleDb
Imports System.IO
Public Class Profile
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Load_GridView()
End Sub
Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
OpenFileDialog1.FileName = ""
OpenFileDialog1.Filter = "JPEG files (*.jpg)|*.jpg|GIF files (*.gif)|*.gif|All files (*.*)|*.*"
OpenFileDialog1.ShowDialog()
Me.Label1.Text = OpenFileDialog1.FileName
Me.PictureBox1.ImageLocation = Me.Label1.Text
End Sub
Private Sub btnAddNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddNew.Click
Me.PictureBox1.ImageLocation = ""
Me.txtDescription.Text = ""
Me.txtDescription.Tag = 0
Me.Label1.Text = ""
End Sub
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
Me.Close()
End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Save_Image()
Form1_Load(sender, e)
End Sub
Private Sub Save_Image()
Dim conn As New OleDbConnection
Dim sConnString As String
Dim cmd As New OleDbCommand
Dim sSQL As String = String.Empty
Dim arrImage() As Byte
Dim myMs As New IO.MemoryStream
Dim bSaveImage As Boolean = False
Dim strImg As String = String.Empty
'Try
If Not IsNothing(Me.PictureBox1.Image) Then
Me.PictureBox1.Image.Save(myMs, Me.PictureBox1.Image.RawFormat)
arrImage = myMs.GetBuffer
bSaveImage = True
Else
arrImage = Nothing
bSaveImage = False
End If
If bSaveImage = True Then
If Microsoft.VisualBasic.Right(Application.StartupPath, 1) = "\" Then
sConnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "trial.mdb;Persist Security Info=False;"
Else
sConnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\trial.mdb;Persist Security Info=False;"
End If
conn = New OleDbConnection(sConnString)
conn.Open()
cmd.Connection = conn
cmd.CommandType = CommandType.Text
If Me.txtDescription.Tag = 0 Then
cmd.CommandText = "INSERT INTO client ( firstname,lastname, [pictures],address,mobile ) VALUES(@firstname,@lastname, @img,@address,@mobile)"
Else
cmd.CommandText = "UPDATE client set firstname = @firstname,lastname=@lastname,[pictures]= @img, address=@address,mobile=@mobile where id=@id"
End If
cmd.Parameters.Add("@firstname", OleDbType.VarChar).Value = Me.txtDescription.Text
cmd.Parameters.Add("@lastname", OleDbType.VarChar).Value = Me.txtlastname.Text
cmd.Parameters.Add("@img", OleDb.OleDbType.Binary).Value = arrImage
cmd.Parameters.Add("@address", OleDbType.VarChar).Value = Me.txtaddress.Text
cmd.Parameters.Add("@mobile", OleDbType.VarChar).Value = Me.txtmobile.Text
cmd.Parameters.Add("@id", OleDbType.Integer).Value = Me.txtid.Text
cmd.ExecuteNonQuery()
MsgBox("Image has been save.")
Else
MsgBox("Please add an image")
End If
'Catch ex As Exception
MsgBox(ErrorToString)
'Finally
conn.Close()
' End Try
End Sub
Private Sub Load_Image(ByVal iIMageID As Long)
Dim conn As New OleDbConnection
Dim sConnString As String
Dim cmd As New OleDbCommand
Dim dr As OleDbDataReader
Dim arrImage() As Byte
Dim myMS As New IO.MemoryStream
Try
If Microsoft.VisualBasic.Right(Application.StartupPath, 1) = "\" Then
sConnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "trial.mdb;Persist Security Info=False;"
Else
sConnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\trial.mdb;Persist Security Info=False;"
End If
conn = New OleDbConnection(sConnString)
conn.Open()
cmd.Connection = conn
cmd.CommandType = CommandType.Text
cmd.CommandText = "select id, firstname as description,lastname, [pictures],address,mobile from client where id= " & iIMageID
dr = cmd.ExecuteReader
If dr.HasRows Then
While dr.Read
Me.txtDescription.Tag = dr("id")
Me.txtDescription.Text = dr("description")
Me.txtlastname.Text = dr("lastname")
arrImage = dr("pictures")
Me.txtaddress.Text = dr("address")
Me.txtmobile.Text = dr("mobile")
For Each ar As Byte In arrImage
myMS.WriteByte(ar)
Next
'
Me.PictureBox1.Image = System.Drawing.Image.FromStream(myMS)
End While
End If
Catch ex As Exception
MsgBox(ErrorToString)
Finally
conn.Close()
End Try
End Sub
Private Sub Load_GridView()
Dim conn As New OleDbConnection
Dim sConnString As String
Dim cmd As New OleDbCommand
Dim da As New OleDbDataAdapter
Dim dt As New DataTable
Try
If Microsoft.VisualBasic.Right(Application.StartupPath, 1) = "\" Then
sConnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "trial.mdb;Persist Security Info=False;"
Else
sConnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\trial.mdb;Persist Security Info=False;"
End If
conn = New OleDbConnection(sConnString)
conn.Open()
cmd.Connection = conn
cmd.CommandType = CommandType.Text
cmd.CommandText = "select id, firstname as description,lastname,address,mobile from client"
da.SelectCommand = cmd
da.Fill(dt)
Me.DataGridView1.DataSource = dt
Catch ex As Exception
MsgBox(ErrorToString)
Finally
conn.Close()
End Try
End Sub
Private Sub DataGridView1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView1.DoubleClick
Load_Image(Me.DataGridView1.Item(0, Me.DataGridView1.CurrentRow.Index).Value)
End Sub
End Class