Contacts.vb

 'Programmed by    :   Rey Sean Adorio Cañeso
'Address          :   28 I.B. Upper Calarian, Zamboanga City, Philippines
'Cell No          :   +639154684127
'
'Contacts.vb
'No. of Lines     :   82
'Date Modified    :   03-12-2011
'

Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Windows.Forms
Imports System.Data.OleDb
Imports System.Data
Imports System.Drawing
Imports System.IO

Public Class Contacts
    Inherits Persons

#Region "Objects/Variables"

    Private command As OleDbCommand = New OleDbCommand()
    Private dataAdapter As OleDbDataAdapter = New OleDbDataAdapter()
    Private dataReader As OleDbDataReader
    Private listViewItem As ListViewItem
    Private fileStream As FileStream

    Private _id As Integer = 0
    Private _image As Image
    Private _fileName As String = ""
    Private _fileSize As Integer = 0
    Private _fileRaw As Byte()

    Private sqlString As String = ""

#End Region

#Region "Constructors"

    ''' <summary>
    ''' A default contructor
    ''' </summary>
    Public Sub New()

    End Sub

    ''' <summary>
    ''' Sets new values for the object
    ''' </summary>
    ''' <param name="_firstName">Represents the first name</param>
    ''' <param name="_middleName">Represents the middle name</param>
    ''' <param name="_lastName">Represents the last name</param>
    ''' <param name="_cellNo">Represents the cell no</param>
    ''' <param name="_fileName">Represents the file name</param>
    ''' <param name="_fileSize">Represents the file size</param>
    ''' <param name="_fileRaw">Represents the byte file</param>
    Public Sub New(ByVal _firstName As String, ByVal _middleName As String, ByVal _lastName As String, ByVal _cellNo As String, ByVal _fileName As String, ByVal _fileSize As Integer, ByVal _fileRaw As Byte())

        Me.FirstName = _firstName
        Me.MiddleName = _middleName
        Me.LastName = _lastName
        Me.CellNo = _cellNo
        Me._fileName = _fileName
        Me.fileSize = _fileSize
        Me._fileRaw = _fileRaw
    End Sub

    ''' <summary>
    ''' Retrieve details on a form
    ''' </summary>
    ''' <param name="ID">The primary key</param>
    ''' <param name="pictureBox">The picture of an employee</param>
    ''' <param name="isEntry">True if it is an entry; otherwise false to retrieve only the picture of an employee</param>
    Public Sub New(ByVal ID)
        Me.sqlString = "SELECT * FROM Contacts " & _
                       "WHERE " & _
                       "ID=" & ID & ";"

        Main.SetConnection()

        Me.command = New OleDbCommand(Me.sqlString, Main.conn)
        Me.command.CommandType = System.Data.CommandType.Text
        Me.dataReader = Me.command.ExecuteReader()
        Me.dataReader.Read()

        If Me.dataReader.HasRows Then
            Me.ID = Convert.ToInt32(Me.dataReader("ID").ToString())
            Me.FirstName = Me.dataReader("FirstName").ToString()
            Me.MiddleName = Me.dataReader("MiddleName").ToString()
            Me.LastName = Me.dataReader("LastName").ToString()
            Me.CellNo = Me.dataReader("CellNo").ToString()
            'Default Values
            Me.FileName = ""
            Me.FileSize = 0
            Me.FileRaw = New Byte(0) {}
            'Check if there is an image file
            If Me.dataReader("FileName").ToString() <> "" And Me.dataReader("FileSize").ToString() <> "" Then
                Me.FileName = Me.dataReader("FileName").ToString()
                Me.FileSize = Convert.ToInt32(Me.dataReader("FileSize"))
                Me.FileRaw = New Byte(Me.FileSize) {}
                Me.dataReader.GetBytes(dataReader.GetOrdinal("FileRaw"), 0, Me.FileRaw, 0, Me.FileSize)
                Me.fileStream = New FileStream(Application.StartupPath & "\Pictures" & Me.ID.ToString() & ".pix", FileMode.OpenOrCreate, FileAccess.ReadWrite)
                Me.fileStream.Write(Me.FileRaw, 0, Me.FileSize)
                Me.fileStream.Close()
                Me.fileStream = New FileStream(Application.StartupPath & "\Pictures" & Me.ID.ToString() & ".pix", FileMode.Open)
                Me._image = Image.FromStream(Me.fileStream)
                Me.fileStream.Close()
            Else
                Me._image = My.Resources.NoPix
            End If
        End If
        Main.conn.Close()
        Main.conn.Dispose()

    End Sub

#End Region

#Region "Properties"

    ''' <summary>
    ''' Gets or sets the ID (PK)
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property ID() As Integer
        Get
            Return Me._id
        End Get
        Set(ByVal value As Integer)
            Me._id = value
        End Set
    End Property

    ''' <summary>
    ''' Gets full name
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property FullName() As String
        Get
            Return Me.FirstName & " " & Me.MiddleName & " " & Me.LastName
        End Get
    End Property

    ''' <summary>
    ''' Gets or sets picture
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property Picture() As Image
        Get
            Return Me._image
        End Get
        Set(ByVal value As Image)
            Me._image = value
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets file name of an image
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property FileName() As String
        Get
            Return Me._fileName
        End Get
        Set(ByVal value As String)
            Me._fileName = value
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets the file size of an image
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property FileSize() As Integer
        Get
            Return Me._fileSize
        End Get
        Set(ByVal value As Integer)
            Me._fileSize = value
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets the binary file of an image
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property FileRaw() As Byte()
        Get
            Return Me._fileRaw
        End Get
        Set(ByVal value As Byte())
            Me._fileRaw = value
        End Set
    End Property

#End Region

#Region "SQL Transactions"

    ''' <summary>
    ''' Displays the data on a form
    ''' </summary>
    ''' <param name="listView">Represents the list view</param>
    ''' <param name="linkLabel">Represents link label</param>
    ''' <param name="showAll">True if show all list; Otherwise False to display only found items</param>
    ''' <param name="searchString">Represents the search item</param>
    Public Sub ShowData(ByVal listView As ListView, ByVal linkLabel As LinkLabel, ByVal showAll As Boolean, ByVal searchString As String)
        Cursor.Current = Cursors.WaitCursor

        If showAll = True Then
            Me.sqlString = "SELECT * FROM Contacts " & _
                           "ORDER BY FirstName;"
        Else
            Me.sqlString = "SELECT * FROM Contacts " & _
                         "WHERE " & _
                         "FirstName LIKE '%" & searchString & "%' OR " & _
                         "MiddleName LIKE '%" & searchString & "%' OR " & _
                         "LastName LIKE '%" & searchString & "%' OR " & _
                         "CellNo LIKE '%" & searchString & "%' " & _
                         "ORDER BY FirstName;"
        End If

        Main.SetConnection()
        Me.command = New OleDbCommand(Me.sqlString, Main.conn)
        Me.command.CommandType = CommandType.Text
        Me.dataReader = Me.command.ExecuteReader()
        listView.Items.Clear()
        Dim no As Integer = 0

        If dataReader.HasRows = True Then
            linkLabel.Visible = False
            While Me.dataReader.Read()
                Me.listViewItem = listView.Items.Add(Convert.ToString(no + 1), 0)
                Me.listViewItem.SubItems.Add(Me.dataReader("ID").ToString())
                Me.listViewItem.SubItems.Add(Me.dataReader("FirstName").ToString() & " " & Me.dataReader("MiddleName").ToString() & " " & Me.dataReader("FirstName").ToString())
                Me.listViewItem.SubItems.Add(Me.dataReader("CellNo").ToString())
                Dim c As Customize = New Customize(listView, Me.listViewItem.Index)
                no += 1
            End While
            listView.TopItem.Selected = True
        Else
            linkLabel.Visible = True
        End If

        Main.conn.Close()
        Main.conn.Dispose()
        Cursor.Current = Cursors.Default

    End Sub

    ''' <summary>
    ''' Inserts new data
    ''' </summary>
    Public Sub InsertData()
        Me.sqlString = "INSERT INTO Contacts (" & _
                         "FirstName, " & _
                         "MiddleName, " & _
                         "LastName, " & _
                         "CellNo, " & _
                         "FileName, " & _
                         "FileSize, " & _
                         "FileRaw" & _
                         ") VALUES (" & _
                         "@FirstName, " & _
                         "@MiddleName, " & _
                         "@LastName, " & _
                         "@CellNo, " & _
                         "@FileName, " & _
                         "@FileSize," & _
                         "@FileRaw" & _
                         ");"

        Main.SetConnection()
        Me.command = New OleDbCommand(Me.sqlString, Main.conn)
        Me.setParameterValues()
        Me.command.ExecuteNonQuery()
        Main.conn.Close()
        Main.conn.Dispose()
        'Refresh List
        FormPhoneBook.ShowContacts(True, "")
        'Dispose entry form
        FormEntry.Dispose()
        MessageBox.Show(Me.FullName & InfoMessage.M_SUCCESSFULLY_ADDED, InfoMessage.C_SUCCESSFULLY_ADDED, MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

    ''' <summary>
    ''' Updates data
    ''' </summary>
    Public Sub EditData()

        Me.sqlString = "UPDATE Contacts SET " & _
                         "FirstName=@FirstName, " & _
                         "MiddleName=@MiddleName, " & _
                         "LastName=@LastName, " & _
                         "CellNo=@CellNo, " & _
                         "FileName=@FileName, " & _
                         "FileSize=@FileSize, " & _
                         "FileRaw=@FileRaw " & _
                         "WHERE " & _
                         "ID=" & Me.ID & ";"
        Main.SetConnection()
        Me.command = New OleDbCommand(Me.sqlString, Main.conn)
        Me.setParameterValues()
        Me.command.ExecuteNonQuery()
        Main.conn.Close()
        Main.conn.Dispose()
        'Delete picture file if the picture box is set to none
        If (Me.FileName = "" Or Me.FileSize = 0) Then Me.DeleteImage()
        'Refresh List
        FormPhoneBook.ShowContacts(True, "")
        'Dispose entry form
        FormEntry.Dispose()
        MessageBox.Show(Me.FullName & InfoMessage.M_SUCCESSFULLY_ADDED, InfoMessage.C_SUCCESSFULLY_ADDED, MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

    ''' <summary>
    ''' Deletes data
    ''' </summary>
    ''' <param name="temp">The item to be deleted (Set to null if the requested query is either insert or update)</param>
    Public Sub DeleteData(ByVal temp As String)
        Me.sqlString = "DELETE FROM Contacts " & _
                         "WHERE " & _
                         "ID=" & Me.ID & ";"
        Main.SetConnection()
        Me.command = New OleDbCommand(Me.sqlString, Main.conn)
        Me.command.ExecuteNonQuery()
        Main.conn.Close()
        Main.conn.Dispose()
        'Delete Image
        Me.DeleteImage()
        'Refresh List
        FormPhoneBook.ShowContacts(True, "")
        MessageBox.Show(temp & InfoMessage.M_SUCCESSFULLY_ADDED, InfoMessage.C_SUCCESSFULLY_ADDED, MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

    ''' <summary>
    '''A method to permanently delete the picture file
    '''</summary>
    Private Sub DeleteImage()
        Try
            System.IO.File.Delete(Application.StartupPath + "\Pictures" + Me.ID.ToString() + ".pix")
        Catch e As IOException
            MessageBox.Show(e.Message, InfoMessage.UNEXPECTED_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Public Sub ShowImage(ByVal ID As Integer)
        If Not System.IO.Directory.Exists(Application.StartupPath & "\Pictures") Then
            System.IO.Directory.CreateDirectory(Application.StartupPath & "\Pictures")
        End If

        If System.IO.File.Exists(Application.StartupPath & "\Pictures" & ID.ToString() & ".pix") Then
            Me.fileStream = New FileStream(Application.StartupPath & "\Pictures" & ID.ToString() & ".pix", FileMode.Open, FileAccess.Read)
            Me.Picture = Image.FromStream(Me.fileStream)
            Me.fileStream.Close()
        Else
            Me.Picture = My.Resources.NoPix
        End If
    End Sub

    ''' <summary>
    ''' Sets the parameter values to be passed to the query
    ''' </summary>
    Private Sub setParameterValues()
        Me.command.Parameters.AddWithValue("@FirstName", Me.FirstName)
        Me.command.Parameters.AddWithValue("@MiddleName", Me.MiddleName)
        Me.command.Parameters.AddWithValue("@LastName", Me.LastName)
        Me.command.Parameters.AddWithValue("@CellNo", Me.CellNo)
        Me.command.Parameters.AddWithValue("@FileName", Me.FileName)
        Me.command.Parameters.AddWithValue("@FileSize", Me.FileSize)
        Me.command.Parameters.AddWithValue("@FileRaw", Me.FileRaw)

    End Sub


#End Region

End Class

Project Homepage: