Advertisement
JayBeeOH

Login Demo App

Jun 1st, 2016
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 6.19 KB | None | 0 0
  1. '------------------------------------------------------------------------------------------
  2. '           Notice of My Copyright and Intellectual Property Rights
  3. '
  4. ' Any intellectual property contained within the program by Joseph L. Bolen remains the
  5. ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
  6. ' publish or provide such intellectual property to any other person or entity for any
  7. ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
  8. '
  9. '                 Copyright © 2016. All rights reserved.
  10. '        All trademarks remain the property of their respective owners.
  11. '-------------------------------------------------------------------------------------------
  12. ' Program Name:   Login Demo (This is not a secure app. Demo only!)
  13. '
  14. ' Author:         Joseph L. Bolen
  15. ' Date Created:   01 JUN 2016
  16. '
  17. ' Description:    This Login Form is from the standard Visual Studio templates.
  18. '                 The demo is to show how to use the ExecuteScalar method in
  19. '                 a separate class to return a boolean value of a success or
  20. '                 failure in finding a record in a table. The ConnectionString
  21. '                 is retrieved from the App.config file.
  22. '
  23. '                 NOTE: This is not a secure app. Demo only! Salted Password Hashing
  24. '                       should be implemented.
  25.  
  26. Imports System.Data.SqlClient
  27.  
  28. Public Class LoginForm
  29.  
  30.     Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
  31.         Handles OK.Click
  32.  
  33.         ' Validate input.
  34.         If String.IsNullOrWhiteSpace(UsernameTextBox.Text) OrElse
  35.                 String.IsNullOrWhiteSpace(PasswordTextBox.Text) Then
  36.             MessageBox.Show("Both User Name and Password values are required.",
  37.                             "Logon Error",
  38.                             MessageBoxButtons.OK,
  39.                             MessageBoxIcon.Error)
  40.             Exit Sub
  41.         End If
  42.  
  43.         Try
  44.             If UsersDB.IsUserFound(UsernameTextBox.Text, PasswordTextBox.Text) Then
  45.                 'Note: Application Shutdown Mode has been set to "When last form closes".
  46.                 Dim appForm As New MainForm
  47.                 appForm.Show()
  48.                 Me.Close()
  49.             Else
  50.                 MessageBox.Show("Incorrect User Name and/or Password values were entered.",
  51.                                 "Logon Error",
  52.                                 MessageBoxButtons.OK,
  53.                                 MessageBoxIcon.Error)
  54.                 UsernameTextBox.Select(0, 0)
  55.                 UsernameTextBox.Focus()
  56.                 Exit Sub
  57.             End If
  58.         Catch ex As DataException       ' General ADO.NET Component error.
  59.             MessageBox.Show(ex.Message,
  60.                 ex.GetType.ToString,
  61.                 MessageBoxButtons.OK,
  62.                 MessageBoxIcon.Error)
  63.         Catch ex As SqlException        ' SQLException error.
  64.             MessageBox.Show("Database error # 0x" & ex.ErrorCode.ToString("X") & " - " & ex.Message,
  65.                 ex.GetType.ToString,
  66.                 MessageBoxButtons.OK,
  67.                 MessageBoxIcon.Error)
  68.         Catch ex As Exception           ' General catch all error.
  69.             MessageBox.Show(ex.Message,
  70.                  ex.GetType.ToString,
  71.                 MessageBoxButtons.OK,
  72.                 MessageBoxIcon.Error)
  73.         End Try
  74.  
  75.     End Sub
  76.  
  77.     Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
  78.         Handles Cancel.Click
  79.  
  80.         Me.Close()
  81.     End Sub
  82.  
  83. End Class
  84.  
  85.  
  86. '-------------------------------------------------------------------------------------------
  87. ' Class Name:     UsersDB
  88. '
  89. ' Author:         Joseph L. Bolen
  90. ' Date Created:   01 JUN 2016
  91. '
  92. ' Description:    For this demonstration, the Users table from the MS SQL Express
  93. '                 database MyTestDB is being used. The database's file location
  94. '                 is retrieved from the App.config file.
  95. '
  96. Imports System.Data.SqlClient
  97.  
  98. Public Class UsersDB
  99.  
  100.     Public Shared Function IsUserFound(ByVal user As String, ByVal psw As String) As Boolean
  101.  
  102.         Dim found As Boolean = False
  103.         Dim query As String = "SELECT TOP 1 1" &
  104.                             "FROM Users " &
  105.                             "WHERE UserName = @UserName AND UserPassword = @UserPassword;"
  106.  
  107.         Try
  108.             Using con As SqlConnection = MyTestDB.GetConnection()
  109.                 Using cmd As New SqlCommand(query, con)
  110.                     cmd.Parameters.AddWithValue("@UserName", user)
  111.                     cmd.Parameters.AddWithValue("@UserPassword", psw)
  112.                     con.Open()
  113.                     found = Convert.ToBoolean(cmd.ExecuteScalar())
  114.                 End Using
  115.             End Using
  116.         Catch ex As Exception
  117.             Throw ex
  118.         End Try
  119.  
  120.         Return found
  121.     End Function
  122.  
  123. End Class
  124.  
  125. '-------------------------------------------------------------------------------------------
  126. ' Class Name:     MyTestDB
  127. '
  128. ' Author:         Joseph L. Bolen
  129. ' Date Created:   01 JUN 2016
  130. '
  131. ' Description:    For this demonstration, the Users table from the MS SQL Express
  132. '                 database MyTestDB is being used. The database's file location
  133. '                 is retrieved from the App.config file.
  134. '-------------------------------------------------------------------------------------------
  135.  
  136. 'Note: Add Reference to System.Configuration to the Project.
  137.  
  138. Imports System.Configuration
  139. Imports System.Data.SqlClient
  140.  
  141. Public Class MyTestDB
  142.  
  143.     Public Shared Function GetConnection() As SqlConnection
  144.         Dim conn As String = ConfigurationManager.ConnectionStrings("MyTestDB").ConnectionString
  145.         Return New SqlConnection(conn)
  146.     End Function
  147.  
  148. End Class
  149.  
  150. The App.Config file...
  151.  
  152. <?xml version="1.0" encoding="utf-8" ?>
  153. <configuration>
  154.     <startup>
  155.         <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
  156.     </startup>
  157.   <connectionStrings>
  158.     <add name="MyTestDB" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=MyTestDB;Integrated Security=True;"
  159.          providerName="System.Data.SQLClient"/>
  160.   </connectionStrings>
  161. </configuration>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement