Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 5.84 KB | None | 0 0
  1. Public Class form_Login
  2.  
  3.     Dim ConnectedDBLogin As New OleDb.OleDbConnection
  4.     Dim Username As String
  5.     Dim FirstCharacter As String
  6.     Dim SecondCharacter As String
  7.     Dim ThirdCharacter As String
  8.     Dim ThreeCharacters As String
  9.     Dim Age As Integer
  10.     Dim AutoNumber As Integer
  11.     Dim Password As String
  12.  
  13.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  14.  
  15.         label_LoginInvalid.Text = ""
  16.         label_RegisterInvalid.Text = ""
  17.  
  18.         Try
  19.             ConnectedDBLogin = New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source = C:\Users\Brad\Documents\Visual Studio 2017\Projects\NEA Quiz\NEA Quiz Database.accdb;Persist Security Info=False;")
  20.             Try
  21.                 Call ConnectedDBLogin.Open()
  22.             Catch ex As Exception
  23.                 label_LoginInvalid.Text = "Cannot connect to database."
  24.             End Try
  25.  
  26.             If ConnectedDBLogin.State = ConnectionState.Open Then
  27.                 Dim SQL As New OleDb.OleDbCommand("SELECT * FROM table_Logins", ConnectedDBLogin)
  28.                 Dim DataAdapter As New OleDb.OleDbDataAdapter(SQL)
  29.                 Dim DataTable As New DataTable("Login")
  30.                 DataAdapter.Fill(DataTable)
  31.                 dataGridView_Login.DataSource = DataTable
  32.             End If
  33.  
  34.         Catch ex As Exception
  35.             label_LoginInvalid.Text = "Cannot connect to database."
  36.         End Try
  37.     End Sub
  38.  
  39.     Private Sub button_Login_Click(sender As Object, e As EventArgs) Handles button_Login.Click
  40.  
  41.         Try
  42.             ConnectedDBLogin = New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source = C:\Users\Brad\Documents\Visual Studio 2017\Projects\NEA Quiz\NEA Quiz Database.accdb;Persist Security Info=False;")
  43.             Try
  44.                 Call ConnectedDBLogin.Open()
  45.             Catch ex As Exception
  46.                 label_LoginInvalid.Text = "Cannot connect to database."
  47.             End Try
  48.  
  49.             If ConnectedDBLogin.State = ConnectionState.Open Then
  50.                 Dim SQL As New OleDb.OleDbCommand("SELECT * FROM table_Logins WHERE Username = '" & textBox_Username.Text & "'", ConnectedDBLogin)
  51.                 Dim DataAdapter As New OleDb.OleDbDataAdapter(SQL)
  52.                 Dim DataTable As New DataTable("Login")
  53.                 DataAdapter.Fill(DataTable)
  54.                 dataGridView_Login.DataSource = DataTable
  55.             End If
  56.  
  57.         Catch ex As Exception
  58.             label_LoginInvalid.Text = "No usernames found."
  59.         End Try
  60.  
  61.         Me.dataGridView_Login.CurrentCell = Me.dataGridView_Login(1, 0)
  62.         textBox_UsernameFromDB.Text = Me.dataGridView_Login.CurrentCell.FormattedValue
  63.  
  64.         Me.dataGridView_Login.CurrentCell = Me.dataGridView_Login(2, 0)
  65.         textBox_PasswordFromDB.Text = Me.dataGridView_Login.CurrentCell.FormattedValue
  66.  
  67.         If textBox_Username.Text = textBox_UsernameFromDB.Text And textBox_Password.Text = textBox_PasswordFromDB.Text Then
  68.             Me.Hide()
  69.  
  70.         Else
  71.             label_LoginInvalid.Text = "Invalid username and/or password."
  72.         End If
  73.  
  74.     End Sub
  75.  
  76.     Private Sub button_Register_Click(sender As Object, e As EventArgs) Handles button_Register.Click
  77.  
  78.         If textBox_Forename.Text = "" Then
  79.             label_RegisterInvalid.Text = "Please complete all fields."
  80.         Else
  81.             If textBox_CreatePassword.Text = "" Then
  82.                 label_RegisterInvalid.Text = "Please complete all fields."
  83.             Else
  84.                 If textBox_Age.Text = "" Then
  85.                     label_RegisterInvalid.Text = "Please complete all fields."
  86.                 Else
  87.                     If textBox_Year.Text = "" Then
  88.                         label_RegisterInvalid.Text = "Please complete all fields."
  89.                     Else
  90.  
  91.                         FirstCharacter = textBox_Forename.Text.Substring(0, 1)
  92.                         SecondCharacter = textBox_Forename.Text.Substring(0, 2)
  93.                         ThirdCharacter = textBox_Forename.Text.Substring(0, 3)
  94.  
  95.                         ThreeCharacters = FirstCharacter & SecondCharacter & ThirdCharacter
  96.  
  97.                         Username = ThreeCharacters & Age
  98.  
  99.                         Me.dataGridView_Login.CurrentCell = Me.dataGridView_Login(0, 0)
  100.                         AutoNumber = Me.dataGridView_Login.CurrentCell.FormattedValue + 1
  101.  
  102.                         Try
  103.                             ConnectedDBLogin = New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source = C:\Users\Brad\Documents\Visual Studio 2017\Projects\NEA Quiz\NEA Quiz Database.accdb;Persist Security Info=False;")
  104.                             Try
  105.                                 Call ConnectedDBLogin.Open()
  106.                             Catch ex As Exception
  107.                                 label_RegisterInvalid.Text = "Cannot connect to database."
  108.                             End Try
  109.  
  110.                             If ConnectedDBLogin.State = ConnectionState.Open Then
  111.                                 Dim SQL As New OleDb.OleDbCommand("INSERT INTO table_Logins (AutoNumber, Username, Password) VALUES (" & [AutoNumber] & ", '" & [Username] & "', '" & [Password] & "',)", ConnectedDBLogin)
  112.                                 Dim DataAdapter As New OleDb.OleDbDataAdapter(SQL)
  113.                                 Dim DataTable As New DataTable("Login")
  114.                                 DataAdapter.Fill(DataTable)
  115.                                 dataGridView_Login.DataSource = DataTable
  116.                             End If
  117.  
  118.                         Catch ex As Exception
  119.                             label_RegisterInvalid.Text = "Cannot insert into database."
  120.                             MessageBox.Show(ex.ToString)
  121.                         End Try
  122.  
  123.                     End If
  124.                 End If
  125.             End If
  126.         End If
  127.  
  128.     End Sub
  129. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement