Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Imports System.Security
Imports System.Security.Cryptography
Imports System.Text

Public Class Parts
    Private _partNo As String
    Private _partDesc As String
    Private cnstr As String
    Private connection As Connection

#Region "Properties"

    Public Property partNo As String
        Get
            Return _partNo
        End Get
        Set(ByVal value As String)
            _partNo = value
        End Set
    End Property

    Public Property partDesc As String
        Get
            Return _partDesc
        End Get
        Set(ByVal value As String)
            _partDesc = value
        End Set
    End Property

#End Region

#Region "Methods"

    Public Sub New()
        cnstr = mainForm.cnstr
    End Sub

    Public Sub New(ByVal partno As String)
        connection = New Connection
        connection.server = My.Settings.server
        connection.database = My.Settings.database
        connection.username = My.Settings.username
        connection.password = My.Settings.password
        connection.dbserver = My.Settings.dbserver
        connection.isTrusted = My.Settings.isTrusted
        cnstr = connection.cnStr
        mainForm.cnstr = cnstr
        _partNo = partNo
    End Sub

    Public Sub add(ByVal kptpartno As String, ByVal kptdesc As String)
        Dim conn As New SqlConnection(cnstr)
        Dim myQuery As String

        myQuery = "INSERT INTO kparts([KPTPARTNUM],[KPTDESCRIPTION]) VALUES (" & _
                  "'" & kptpartno & "','" & kptdesc & "')"

        Dim mycommand As SqlCommand
        mycommand = New SqlCommand(myQuery, conn)

        Try
            conn.Open()
            mycommand.ExecuteNonQuery()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            conn.Close()
            conn.Dispose()
        End Try
    End Sub

    Public Sub edit(ByVal kptpartno As String, ByVal kptdesc As String)

        Dim conn As New SqlConnection(cnstr)
        Dim myQuery As String


        myQuery = "UPDATE kparts " & _
                  "SET kptpartnum = " & "'" & kptpartno & "'," & _
                  " kptdescription = " & " '" & kptdesc & "'" & _
                  " WHERE kptpartnum = " & "'" & _partNo & "'"

        Dim mycommand As SqlCommand
        mycommand = New SqlCommand(myQuery, conn)

        Try
            conn.Open()
            mycommand.ExecuteNonQuery()
            MessageBox.Show("Successfully Saved.", "System User", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            conn.Close()
            conn.Dispose()
        End Try
    End Sub

    Public Sub delete(ByVal kptpartno As String)
        Dim conn As New SqlConnection(cnstr)

        Dim myQuery As String
        myQuery = "DELETE FROM kparts " & _
                  "WHERE kptpartnum = '" & kptpartno & "'"

        Dim mycommand As SqlCommand
        mycommand = New SqlCommand(myQuery, conn)

        Try
            conn.Open()
            If (check_part() = True) Then
                mycommand.ExecuteNonQuery()
                MessageBox.Show("Successfully Deleted.", "System User", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Else
                MessageBox.Show("User does not exists in the database.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            conn.Close()
            conn.Dispose()
        End Try
    End Sub

    Public Function check_part() As Boolean
        Dim conn As New SqlConnection(cnstr)

        Dim myQuery As String
        myQuery = "SELECT * FROM kparts " & _
                  "WHERE kptpartnum = " & "'" & _partNo & "'"

        Dim mycommand As SqlCommand
        mycommand = New SqlCommand(myQuery, conn)

        Try
            conn.Open()
            Dim reader As SqlDataReader = mycommand.ExecuteReader()
            If (reader.HasRows) Then
                Return True
            Else
                Return False
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            conn.Close()
            conn.Dispose()
        End Try
    End Function

#End Region

End Class
