Thursday, February 24, 2011

Class File - Holds the executable file's directory

I usually put subfolders in the directory of my executable file. Usually the following:
  • Root\Reports - for the crystal reports and other reports that my system needs
  • Root\Logs - for the audit trail of my system (if i dont include them in my database)
  • More
There always comes a time that I would have to get the directory of my executable file and add the folder name and the filename to locate/open the file. It would involve more processes if i would have to get the path of my executable file everytime a access an external file. Thus what I did were the following (given that the user has already accessed the system - means user is validated upon log-in):

Create a class that would hold the path of the executable file

Public Class clsExecfile
       Public ApplicationPath as string = ""
       Public ReportPath as string = ""
       Public LogPath as string = ""
       ' you may add more if there are still others


       Public Sub New()
                  objFile As New System.IO.FileInfo(Application.ExecutablePath)
                  ApplicationPath = objFile.Directoryname
                  ApplicationPath = iif(strings.right(ApplicationPath,1) = "\", ApplicationPath, ApplicationPath & "\")
                   ReportPath = ApplicationPath & "Reports\"
               LogPath = ApplicationPath & "Logs\"
               objFile = nothing
       End Sub

End Class

Declare the object

                Public objApplication as new clsExecFile()

Use the object
  • to assign the fullpath of a report that you want to access
dim strReport as string = objApplication.ReportPath & "rptMyReport.rpt"
  • to get the directory of the executable file
                    objApplication.ApplicationPath


You will see here that if you dont create a class that retrieves the directory of your eecutable file, you will have to write the above code every time you want to retrieve your exec files directory. Instead, by just creating a class, you have minimzed your other codes into 1 or 2 lines.

Hope this helps
              

VB Programming - My first step creating User object

I am in the middle of my work and i got bored in a way. So I thought of visiting my blog. Then in a sudden I thought of writing about what I am doing in my work. Yes, why not. In some way, I could share my processes and at the same time, who knows, an expert might visit my page and give/suggest better ideas.


My Program


My system always checks for validation especially the user's access level. There are sub processes in the system that is only accessible by an admin or supervisory account. If I dont make an object of my logged user, and if the access level is needed, I would have to search through my database to retrieve the user's access level every time the the system goes through that process. Thus what I did was, when the user has been granted access to the system, the system creates an object that would hold the information about the user - name, password, accesslevel,etc. But before you can create an object you would have first to create a class of the user.


Create a user class - this is the class that would hold the information about the logged user that you would want to be easily retrieved:




Class clsUser
      Public UserName as string = ""
      Public FullName as string = ""
      Public Designation as string =""
      Public AccessLevel as Integer = 5     'Read-Only User


      Public Sub New(Byval UserName1 as string)
              'I have a shorter version here but since that this is the beginning we will go through th e longer version; 

             
                

                Dim cn As New SqlClient.SqlConnection("Data Source=" & strServername & ";Initial Catalog=" & strDBasename & ";Integrated Security=True")
                Dim cmd As New SqlClient.SqlCommand("SELECT * FROM systemusers WHERE Username = '" & Username1 & "'", cn)    

                Dim myReader As SqlClient.SqlDataReader

                cn.Open()
                                                         
               myReader = cmd.ExecuteReader
                                      
               If myReader.HasRows Then
                       MyReader.Read
                       UserName = myReader("Username").ToString
                       fullName = myReader("Fullname").ToString
                       Designation = myReader("Designation').ToString
                       AccessLevel = iif(myReader("AccessLevel").ToString = "",0,CInt(myReader("AccessLevel").ToString)
               End If
                myReader.Close()
                myReader = Nothing
                cmd = Nothing
                cn.Close()
      End Sub      
End Class

Create a user object - this would hold the information of the logged user

I added this inmy Public Module
             Public objLoggedUser as clsUser

Initialize the user object

       objLoggedUser = new clsUser(txtUsername.text)

Use the user object

I use the user object everytime I check for the access level say in the accessing of the Admin Menu (Level 0)
      

Sub cmdLogs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLogs.Click    
            If objLoggedUser.AccessLevel = 0 then
                   'allow access
            else
                   MsgBox ("User do not have access to this module.",MsgBoxStyle.Information,"Information")
            End If
End Sub


or to add a record in the database

Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click    
            If objLoggedUser.AccessLevel < 5 then
                   'allow access
            else
                   MsgBox ("User do not have access to this module.",MsgBoxStyle.Critical,"Error")
            End If
End Sub



I also use this object in other modules/methods e.g. change of passwords, edit personal information, etc.


Hope this could be of help to anyone. More ports to come. Ciao
Privatecn As New SqlClient.SqlConnection(connectionString)