Guest User

Untitled

a guest
Jun 1st, 2018
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.84 KB | None | 0 0
  1. Imports System.Text
  2. Imports System.Data.SQLite
  3.  
  4. Public Class UserClass
  5. #Region "Properties"
  6.     Protected _UserID As String
  7.     Friend Property UserID() As String
  8.         Get
  9.             Return _UserID
  10.         End Get
  11.         Set(ByVal value As String)
  12.             _UserID = value
  13.  
  14.         End Set
  15.     End Property
  16.     Protected _UserFullName As String
  17.     Friend Property UserFullName() As String
  18.         Get
  19.             Return _UserFullName
  20.         End Get
  21.         Set(ByVal value As String)
  22.             _UserFullName = value
  23.         End Set
  24.     End Property
  25.     Protected _LastName As String
  26.     Friend Property LastName() As String
  27.         Get
  28.             Return _LastName
  29.         End Get
  30.         Set(ByVal value As String)
  31.             _LastName = value
  32.         End Set
  33.     End Property
  34.     Protected _MI As String
  35.     Friend Property MI() As String
  36.         Get
  37.             Return _MI
  38.         End Get
  39.         Set(ByVal value As String)
  40.             _MI = value
  41.         End Set
  42.     End Property
  43.     Protected _UserPassword As String
  44.     Friend Property UserPassword() As String
  45.         Get
  46.             Return _UserPassword
  47.         End Get
  48.         Set(ByVal value As String)
  49.             _UserPassword = value
  50.         End Set
  51.     End Property
  52.     Protected _UserName As String
  53.     Friend Property UserName() As String
  54.         Get
  55.             Return _UserName
  56.         End Get
  57.         Set(ByVal value As String)
  58.             _UserName = value
  59.         End Set
  60.     End Property
  61.     Protected _UserLevel As String
  62.     Friend Property UserLevel() As String
  63.         Get
  64.             Return _UserLevel
  65.         End Get
  66.         Set(ByVal value As String)
  67.             _UserLevel = value
  68.         End Set
  69.     End Property
  70.  
  71.     Protected _pic As PictureBox
  72.     Friend Property pic() As PictureBox
  73.         Get
  74.             Return _pic
  75.         End Get
  76.         Set(ByVal value As PictureBox)
  77.             _pic = value
  78.         End Set
  79.     End Property
  80.     Dim _LastLogin As String
  81.     Friend Property LastLogin() As String
  82.         Get
  83.             Return _LastLogin
  84.         End Get
  85.         Set(ByVal value As String)
  86.             _LastLogin = value
  87.         End Set
  88.     End Property
  89.     Dim _DateAdded As String
  90.     Friend Property DateAdded() As String
  91.         Get
  92.             Return _DateAdded
  93.         End Get
  94.         Set(ByVal value As String)
  95.             _DateAdded = value
  96.  
  97.         End Set
  98.     End Property
  99.  
  100. #End Region
  101.  
  102.     Friend Function ResetPass(ByVal _xUserID As String)
  103.         Dim bReturn As Boolean
  104.         Dim xSQL As New System.Text.StringBuilder
  105.         xSQL.AppendLine("UPDATE MasterUser ")
  106.         xSQL.AppendLine("SET UserPass=@UserPass ")
  107.         xSQL.AppendLine("WHERE UserID=@UserID")
  108.         Try
  109.             Using SQLconnect As New SQLite.SQLiteConnection(g_constring)
  110.                 SQLconnect.Open()
  111.                 Using SQLcommand As New SQLiteCommand(SQLconnect)
  112.                     SQLcommand.CommandText = xSQL.ToString
  113.                     SQLcommand.Parameters.Clear()
  114.                     Dim xClass As New clsCrypto
  115.                     Dim key = "37ecd4b85bff4c71b5011fd24ada01ad"
  116.                     Dim EncryptedPass As String = xClass.EncryptString128Bit(_UserPassword, key)
  117.                     SQLcommand.Parameters.Add(New SQLiteParameter("@UserPass", EncryptedPass))
  118.                     SQLcommand.Parameters.Add(New SQLiteParameter("@UserID", _xUserID))
  119.                     Dim n As Integer = SQLcommand.ExecuteNonQuery
  120.                     If n <> 0 Then bReturn = True
  121.                     MessageBox.Show("Password Successfully Updated", "", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
  122.  
  123.                 End Using
  124.  
  125.             End Using
  126.         Catch ex As Exception
  127.             MessageBox.Show(ex.Message.ToString, "Reset Failed", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
  128.             bReturn = False
  129.         End Try
  130.         Return bReturn
  131.     End Function
  132.     Friend Function SaveUser()
  133.         Dim bResult As Boolean = False
  134.         Dim DateAdded As String = System.DateTime.Now.Date
  135.         Dim xClass As New clsCrypto
  136.         Dim EncryptedPass As String
  137.  
  138.         Dim SQLcmd As New System.Text.StringBuilder
  139.         SQLcmd.AppendLine("INSERT INTO MasterUser ")
  140.         SQLcmd.AppendLine("(UserID,UserName,UserPass,UserFullName,LastName,MI,UserLevel,Date_Added) ")
  141.         SQLcmd.AppendLine("VALUES (@UserID,@UserName,@UserPass,@UserFullName,@LastName,@MI,@UserLevel,@Date_Added) ")
  142.         Try
  143.             Using SQLconnect As New SQLite.SQLiteConnection(g_constring)
  144.                 SQLconnect.Open()
  145.                 Using SQLCommand As New SQLiteCommand(SQLconnect)
  146.                     SQLCommand.CommandText = SQLcmd.ToString
  147.                     SQLCommand.Parameters.Clear()
  148.                     Dim key = "37ecd4b85bff4c71b5011fd24ada01ad"
  149.                     EncryptedPass = xClass.EncryptString128Bit(_UserPassword, key)
  150.                     Dim UserCode As String = CreateNewTranNo(System.DateTime.Now.Date)
  151.  
  152.                     SQLCommand.Parameters.Add(New SQLiteParameter("@UserID", UserCode))
  153.                     SQLCommand.Parameters.Add(New SQLiteParameter("@UserName", _UserName))
  154.                     SQLCommand.Parameters.Add(New SQLiteParameter("@UserPass", EncryptedPass))
  155.                     SQLCommand.Parameters.Add(New SQLiteParameter("@UserFullName", _UserFullName))
  156.                     SQLCommand.Parameters.Add(New SQLiteParameter("@LastName", _LastName))
  157.                     SQLCommand.Parameters.Add(New SQLiteParameter("@MI", _MI))
  158.                     SQLCommand.Parameters.Add(New SQLiteParameter("@UserLevel", _UserLevel))
  159.                     SQLCommand.Parameters.Add(New SQLiteParameter("@Date_Added", DateAdded))
  160.  
  161.                     Dim n As Integer = SQLCommand.ExecuteNonQuery
  162.                     If n <> 0 Then bResult = True
  163.                     MessageBox.Show("Data Succesfully Added", "", MessageBoxButtons.OK, MessageBoxIcon.Information)
  164.  
  165.                 End Using
  166.             End Using
  167.         Catch Ex As Exception
  168.             Throw Ex
  169.         End Try
  170.         Return bResult
  171.     End Function
  172.     Friend Function UpdateUser(ByVal _UserID As String)
  173.         Dim bReturn As Boolean
  174.         Dim SQLcmd As New System.Text.StringBuilder
  175.         SQLcmd.AppendLine("UPDATE MasterUser SET ")
  176.         SQLcmd.AppendLine("UserName=@UserName,UserLevel=@UserLevel,UserFullName=@UserFullName,LastName=@LastName,MI=@MI")
  177.         SQLcmd.AppendLine("WHERE UserID=@UserID ")
  178.         Try
  179.             Using SQLconnect As New SQLiteConnection(g_constring)
  180.                 SQLconnect.Open()
  181.                 Using SQLcommand As New SQLiteCommand(SQLconnect)
  182.                     SQLcommand.CommandText = SQLcmd.ToString
  183.                     SQLcommand.Parameters.Clear()
  184.  
  185.                     SQLcommand.Parameters.Add(New SQLiteParameter("@UserID", _UserID))
  186.                     SQLcommand.Parameters.Add(New SQLiteParameter("@UserName", _UserName))
  187.                     SQLcommand.Parameters.Add(New SQLiteParameter("@UserFullName", _UserFullName))
  188.                     SQLcommand.Parameters.Add(New SQLiteParameter("@LastName", _LastName))
  189.                     SQLcommand.Parameters.Add(New SQLiteParameter("@MI", _MI))
  190.                     SQLcommand.Parameters.Add(New SQLiteParameter("@UserLevel", _UserLevel))
  191.  
  192.                     Dim n As Integer = SQLcommand.ExecuteNonQuery
  193.                     If n <> 0 Then bReturn = True
  194.                     MessageBox.Show("Data Succesfully Updated", "", MessageBoxButtons.OK, MessageBoxIcon.Information)
  195.  
  196.                 End Using
  197.             End Using
  198.         Catch ex As Exception
  199.             Throw ex
  200.         End Try
  201.         Return bReturn
  202.     End Function
  203.     Friend Function SavePhoto(ByVal PhotoId As String, ByVal PhotoFile As String) As Boolean
  204.         Dim bReturn As Boolean
  205.  
  206.         Try
  207.             Using SQLConnect As New SQLite.SQLiteConnection(g_constring)
  208.                 Dim photo() As Byte = FileImageToByte(PhotoFile)
  209.                 SQLConnect.Open()
  210.                 If SQLConnect.State = ConnectionState.Open Then
  211.                     Dim SQLcommand As New SQLiteCommand(SQLConnect)
  212.                     SQLcommand = SQLConnect.CreateCommand
  213.                     SQLcommand.CommandText = "DELETE FROM Photos WHERE PhotoID = '" & PhotoId & "'"
  214.                     SQLcommand.ExecuteNonQuery()
  215.                     SQLcommand.Parameters.Clear()
  216.  
  217.                     SQLcommand.CommandText = "INSERT INTO Photos(PhotoID, Photo) VALUES(@EmployeeID, @Photo1)"
  218.  
  219.                     Dim SQLparmID As New SQLiteParameter("@EmployeeID", PhotoId)
  220.                     SQLparmID.DbType = DbType.String
  221.                     SQLparmID.Value = PhotoId
  222.                     SQLcommand.Parameters.Add(SQLparmID)
  223.  
  224.                     Dim SQLparm As New SQLiteParameter("@Photo1", photo)
  225.                     SQLparm.DbType = DbType.Binary
  226.                     SQLparm.Value = photo
  227.                     SQLcommand.Parameters.Add(SQLparm)
  228.  
  229.                     SQLcommand.ExecuteNonQuery()
  230.  
  231.                     bReturn = True
  232.                 Else
  233.                     bReturn = False
  234.                 End If
  235.             End Using
  236.         Catch eX As System.Exception
  237.             MessageBox.Show(eX.Message.ToString, "Error in database", MessageBoxButtons.OK, MessageBoxIcon.Error)
  238.             bReturn = False
  239.         End Try
  240.         Return bReturn
  241.     End Function
  242.     Friend Function ViewPhoto_Resize(ByVal EmployeeID As String, ByVal picPhoto As DevExpress.XtraEditors.PictureEdit)
  243.         Dim bReturn As Boolean
  244.         Dim sSQL As String = "SELECT Photo FROM Photos WHERE PhotoID='" & EmployeeID & "'"
  245.         Try
  246.             Using SQLConnect As New SQLite.SQLiteConnection(g_constring)
  247.                 picPhoto.Image = Nothing
  248.                 SQLConnect.Open()
  249.                 If SQLConnect.State = ConnectionState.Open Then
  250.                     Dim SQLcommand As New SQLiteCommand(SQLConnect)
  251.                     SQLcommand = SQLConnect.CreateCommand
  252.                     SQLcommand.CommandText = sSQL
  253.                     SQLcommand.ExecuteNonQuery()
  254.  
  255.                     Using SQLreader As SQLiteDataReader = SQLcommand.ExecuteReader()
  256.                         While SQLreader.Read()
  257.                             picPhoto.Image = ResizeImageWithAspect(ByteToImage(SQLreader("Photo")), picPhoto.Width)
  258.                         End While
  259.                     End Using
  260.                    bReturn = True
  261.                 Else
  262.                     bReturn = False
  263.                 End If
  264.             End Using
  265.         Catch eX As System.Exception
  266.             MessageBox.Show(eX.Message.ToString, "Error in database", MessageBoxButtons.OK, MessageBoxIcon.Error)
  267.             bReturn = False
  268.  
  269.         End Try
  270.         Return bReturn
  271.     End Function
  272.     Friend Function UserFile(ByVal _UserID As String) As DataTable
  273.         Dim dt As DataTable = Nothing
  274.         Dim sSQL As String = "SELECT UserID,UserName,UserFullName,LastName,MI,UserLevel,Last_Login,Date_Added FROM MasterUser WHERE UserID='" & _UserID & "'"
  275.         Try
  276.             Using SQLconnet As New SQLiteConnection(g_constring)
  277.                 SQLconnet.Open()
  278.                 Using SQLAdapter As New SQLiteDataAdapter(sSQL, SQLconnet)
  279.                     Dim ds As New DataSet
  280.                     SQLAdapter.Fill(ds)
  281.                     dt = ds.Tables(0)
  282.                     Dim dr As DataRow
  283.                     If dt.Rows.Count <> 0 Then
  284.                         dr = dt.Rows(0)
  285.                         _UserID = dr.Item("UserID").ToString.Trim
  286.                         _UserName = dr.Item("UserName").ToString.Trim
  287.                         _UserFullName = dr.Item("UserFullName").ToString.Trim
  288.                         _LastName = dr.Item("LastName").ToString.Trim
  289.                         _MI = dr.Item("MI").ToString.Trim
  290.                         _UserLevel = dr.Item("UserLevel").ToString.Trim
  291.                         _LastLogin = dr.Item("Last_Login").ToString.Trim
  292.                         _DateAdded = dr.Item("Date_Added").ToString.Trim
  293.  
  294.                     End If
  295.                 End Using
  296.             End Using
  297.         Catch ex As Exception
  298.             Throw ex
  299.         End Try
  300.         Return dt
  301.     End Function
  302.  
  303.     Protected Friend Function CreateNewTranNo(ByVal xTranDate As String) As String
  304.         Dim sReturn As String = xTranDate & ".00000"
  305.         Try
  306.             Using SQLconnect As New SQLite.SQLiteConnection(g_constring)
  307.                 SQLconnect.Open()
  308.                 Dim n As Integer = 0
  309.                 Dim zSQL As New System.Text.StringBuilder
  310.                 zSQL.AppendLine("SELECT UserID   ")
  311.                 zSQL.AppendLine("FROM     MasterUser ")
  312.                 zSQL.AppendLine("WHERE    Date_Added = @TranDate ")
  313.                 zSQL.AppendLine("ORDER BY Date_Added, UserID DESC LIMIT 1 ")
  314.  
  315.                 Dim SQLcommand As New SQLiteCommand(SQLconnect)
  316.                 SQLcommand = SQLconnect.CreateCommand
  317.                 SQLcommand.CommandText = zSQL.ToString
  318.                 SQLcommand.Parameters.Add(New SQLiteParameter("@TranDate", xTranDate))
  319.                 Dim ending As String() = Split(xTranDate, "/")
  320.                 Dim x As String = ending(0) + ending(1) + ending(2)
  321.                 Dim sTranNo As String = ""
  322.                 sTranNo = SQLcommand.ExecuteScalar
  323.                 If sTranNo Is Nothing OrElse sTranNo.Trim.Length = 0 Then
  324.                     'If sTranNo = 0 Then
  325.                    sTranNo = "UNo." & x & "." & "00001"
  326.                Else
  327.                    sTranNo = sTranNo.Substring(sTranNo.LastIndexOf(".") + 1, 5)
  328.                    sTranNo = CType(sTranNo, Int32) + 1
  329.                    sTranNo = "UNo." & x & "." & sTranNo.PadLeft(5, "0")
  330.                End If
  331.                sReturn = sTranNo
  332.  
  333.            End Using
  334.        Catch ex As Exception
  335.            Throw ex
  336.        End Try
  337.        Return sReturn
  338.    End Function
  339. End Class
Add Comment
Please, Sign In to add comment