Chapter 3


Click Here if you want to buy low price iPad!

  • Welcome to the 3rd chapter of my book ( vb .net - working with database ).
  • In this chapter i shall discuss these points :
    1. Adding\inserting photos into database table
    2. retrieving\displaying photo from database table
    3. displaying photos into DataGridView Control
    4. Using PictureBox DragDrop Method to display photo
  • The example remains (My Phone Book) so we will just resume our previous work.
Modifications - Code sippets
  1. General Declaration area:
    Imports System.Drawing
  2. The form's declaration area:
    Dim CDL As New OpenFileDialog
    Dim MyStream As New IO.MemoryStream
    Dim IMG As Drawing.Image
    Dim Pic As Byte()
  3. Sub DataGrid_Work
    Used to display info & photo from database, you will have to replace the whole old code with the new one.
    Dim MyTable As New DataTable
    CMD.Connection = CN
    CMD.CommandText = "Select * From Phone1"
    DataR = CMD.ExecuteReader
    Dim I As Integer
    For I = 0 To MyTable.Rows.Count - 1
    DataR.Read()
    Next
    MyTable.Load(DataR)
    With DG1
    .DataSource = MyTable
    'Chapter#3
    'The new column that display the photo must be (DataGridViewImageColumn Type)
    Dim ImgColumn As New DataGridViewImageColumn
    'Column Header's name
    ImgColumn.HeaderCell.Value = ("Photo")
    'Column's Data from table
    ImgColumn.DataPropertyName = ("Lphoto")
    'Resize to fit in the DataGrid
    ImgColumn.ImageLayout = DataGridViewImageCellLayout.Zoom
    'Add the new ImageColumn
    .Columns.Add(ImgColumn)
    'Update the column's name everytime you use the Sub.
    .Columns("Fname").HeaderCell.Value = ("First Name")
    .Columns("Lname").HeaderCell.Value = ("Last Name")
    .Columns("Country").HeaderCell.Value = ("Country")
    .Columns("City").HeaderCell.Value = ("City")
    .Columns("Phone").HeaderCell.Value = ("Phone")
    .Columns("Mobile").HeaderCell.Value = ("Mobile")
    .Columns("Lemail").HeaderCell.Value = ("E-Mail")
    'Hide the Original (Non-DataGridViewImageColumn Type) that is loaded by default.
    .Columns("Lphoto").Visible = False
    'Show the newly created Column for the photo displaying.
    Dim Ncnt As Integer
    Ncnt = MyTable.Columns.Count
    If .Columns.Count > Ncnt + 1 Then .Columns.Remove(ImgColumn)
    End With
    DataR.Close()
  4. Add\Save Button:
    Used to insert info & photo to database, you will have to replace the whole old code with the new one.
    If Tfnm.Text = vbNullString Then
    Label11.Text = ("Please Choose a picture And Enter First Name")
    Exit Sub
    End If
    'Saving Image.
    'If no photo was loaded into the picturebox.
    If PicLocation = vbNullString Then
    'Save the current (No Photo Default).
    MyPic.Image.Save(Tfnm.Text.ToString + ".JPG", Imaging.ImageFormat.Jpeg)
    'Reload the (No Photo Default) to get a path's value.
    PicLocation = Application.StartupPath + "\" + Tfnm.Text + (".jpg")
    End If
    'We Set Img Object to Value
    IMG = (Nothing)
    IMG = Image.FromFile(PicLocation)
    'Fill (MyStream) with Img's Binary Data
    IMG.Save(MyStream, Drawing.Imaging.ImageFormat.Jpeg)
    'Save Method
    SqlStr = Nothing
    SqlStr = "Insert Into Phone1 (Fname,Lname,Phone,Mobile,Country,City,Lemail,Lphoto)"
    SqlStr = SqlStr + " Values ('" & Tfnm.Text & "', '" & Tlnm.Text & "', '" & Tphon.Text & "', "
    SqlStr = SqlStr + "'" & Tmob.Text & "', '" & Tcntry.Text & "', '" & Tcity.Text & "', "
    SqlStr = SqlStr + "'" & Tmail.Text & "', @img)"
    With CMD
    .Connection = CN
    .CommandType = CommandType.Text
    .CommandText = SqlStr
    End With
    'Get an array of Bytes from the stream and assign to the parameter.
    CMD.Parameters.AddWithValue("@img", OleDbType.VarBinary).Value = MyStream.GetBuffer()
    SqlH = CMD.ExecuteNonQuery
    Label11.Text = ("( ") & SqlH & (" ) Friend Saved")
    ClearAll()
    'Show changes on DataGridView
    DataGrid_Work()
    'Delete The Saved photo
    Kill(PicLocation)
  5. Search Button:
    Used to search for info by (Fname) criteria, replace the old lines of code lies right after [End With] directly with these lines.
    'Retrieve Photo From DataBase
    IMG = Nothing
    DataR = CMD.ExecuteReader
    DataR.Read()
    If DataR.HasRows = False Then
    Label11.Text = ("Friend's First Name doesn't exist")
    DataR.Close()
    ClearAll()
    Exit Sub
    End If
    If IsDBNull(DataR("Lphoto")) = True Then
    DataR.Close()
    Exit Sub
    Else
    Pic = DirectCast(DataR("Lphoto"), Byte())
    MyStream = New MemoryStream(Pic)
    IMG = Image.FromStream(MyStream)
    MyStream.Close
    DataR.Close()
    End If
    DataR.Close()
    'Search Method
    DataR = CMD.ExecuteReader
    DataR.Read()
    If DataR.HasRows = True Then
    Tfnm.Text = DataR!Fname
    Tlnm.Text = DataR!Lname
    Tcntry.Text = DataR!Country
    Tcity.Text = DataR!City
    Tmail.Text = DataR!Lemail
    Tphon.Text = DataR!Phone
    Tmob.Text = DataR!Mobile
    MyPic.Image = IMG
    DataR.Close()
    Else
    Label11.Text = ("Friend's First Name doesn't exist")
    ClearAll()
    DataR.Close()
    End If
    DataR.Close()
  6. Edit Button:
    Used to search for info by (Fname) criteria and edit\Update the returned record, replace the whole old code with the new one. Please read the comments '' between code lines.
    'Edit
    IMG = (Nothing)
    IMG = New Drawing.Bitmap(MyPic.Image)
    'Fill (MyStream) with Img's Binary Data
    MyStream = New MemoryStream
    IMG.Save(MyStream, Drawing.Imaging.ImageFormat.Jpeg)
    SqlStr = Nothing
    SqlStr = "Update Phone1 Set Fname = '" & Tfnm.Text & "', Lname = '" & Tlnm.Text & "', "
    SqlStr = SqlStr + "Country = '" & Tcntry.Text & "', City = '" & Tcity.Text & "', "
    SqlStr = SqlStr + "Phone = '" & Tphon.Text & "', Mobile = '" & Tmob.Text & "', "
    SqlStr = SqlStr + "Lemail = '" & Tmail.Text & "', Lphoto = @img"
    SqlStr = SqlStr + " Where Fname = '" & SearchBox & "'"
    With CMD
    .Connection = CN
    .CommandType = CommandType.Text
    .CommandText = SqlStr
    End With
    CMD.Parameters.AddWithValue("@img", OleDbType.VarBinary).Value = MyStream.GetBuffer()
    sqlh = CMD.ExecuteNonQuery
    MsgBox(SqlH & " friend(s) edited")
    'Close the Command in order to be using it again.
    'That actually solves the problem of not effecting the returned record from search.
    'If you did not close the CMD everytime, you won't be able to correctly edit\Update records.
    CMD.Dispose()
    'Close the MemoryStream
    MyStream.Close()
    MyStream = Nothing
    'Display changes on the DataGridView
    DataGrid_Work()
  7. First Button.
    Used to display the 1st record from the table, replace the codes lies right after (DataR.Read) directly with these lines.
    'Retrieve Photo From DataBase
    IMG = Nothing
    Pic = DirectCast(DataR("Lphoto"), Byte())
    MyStream = New MemoryStream(Pic)
    IMG = Image.FromStream(MyStream)
    Tfnm.Text = DataR!Fname
    Tlnm.Text = DataR!Lname
    Tcntry.Text = DataR!Country
    Tcity.Text = DataR!City
    Tphon.Text = DataR!Phone
    Tmob.Text = DataR!Mobile
    Tmail.Text = DataR!Lemail
    MyPic.Image = IMG
    Onh = 1
    Label11.Text = ("First Record")
    DataR.Close()
    'Search for FirstName
    SearchBox = Tfnm.Text
  8. MyPic (PictureBox) DragDrop
    Used to insert photo into PictureBox control using Drag&Drop method. This is a new code.
    Try
    MyPic.Image = Image.FromFile(CType(e.Data.GetData(DataFormats.FileDrop), Array).GetValue(0).ToString)
    Catch ex As Exception
    Label11.Text = ("Error In Drag/Drop")
    End Try
  9. MyPic (PictureBox) DragEnter event
    If (e.Data.GetDataPresent(DataFormats.FileDrop)) Then
    e.Effect = DragDropEffects.Copy
    End If
  10. MyPic (PictureBox) Click event
    Used to open (OpenDialogBox) to pick a photo from it and display it in the MyPic (PictureBox) Control.
    With CDL
    .Filter = ("Image File (*.jpg;*.bmp;*.gif)|*.jpg;*.bmp;*.gif")
    End With
    If CDL.ShowDialog = DialogResult.OK Then
    PicLocation = CDL.FileName
    Else
    PicLocation = ""
    Exit Sub
    End If
    MyPic.Image = Drawing.Image.FromFile(PicLocation)
  • The modifications for the rest of the navigation system (Next, Previous, Last) would be found in the source code uploaded on MediaFire.com Link.
  • The modifications memo that help understanding the codes are in the chapter#3 book which will be found on MediaFire.com Link.
  • Notice that a last mintue modifications to the code took place to Debug some lines generated errors like :
    A Generic Error Occured In GDI+

Evry1falls

Back


All contents are CopyRight © ® ™ to Evry1falls

Free Web Hosting