Guest User

Untitled

a guest
Feb 1st, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. Imports System.Data
  2. Imports System.Data.SqlClient
  3.  
  4. Public Class frmLogin
  5. Dim userName As String
  6. Dim extrainfo As String
  7.  
  8.  
  9. Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
  10. If ConnectToSQL() Then
  11.  
  12. Dim frm As New frmQuality(userName, extrainfo)
  13. Me.Hide()
  14. frm.Show()
  15. Me.Close()
  16.  
  17. End If
  18.  
  19.  
  20.  
  21. End Sub
  22.  
  23.  
  24.  
  25. Private Function ConnectToSQL() As Boolean
  26.  
  27. Dim con As New SqlConnection
  28. Dim cmd As New SqlCommand
  29. Dim dbPassword As String
  30. Dim enteredPasswrd As String
  31. ' Dim userName As String
  32.  
  33. Try
  34.  
  35.  
  36.  
  37. con.ConnectionString = "Data Source "
  38.  
  39. con.Open()
  40.  
  41. cmd.Connection = con
  42.  
  43. cmd.CommandText = " SELECT UserName, Password, extrainfo FROM users WHERE (UserName like '" & "%" & txtUsername.Text.ToLower() & "%" & "' )"
  44.  
  45.  
  46.  
  47. Dim lrd As SqlDataReader = cmd.ExecuteReader()
  48. If lrd.HasRows Then
  49. While lrd.Read()
  50.  
  51. 'Do something here
  52. dbPassword = lrd("Password").ToString()
  53. userName = lrd("UserName").ToString()
  54. extrainfo = lrd("assigned_ei").ToString()
  55.  
  56.  
  57. enteredPasswrd = txtPassword.Text()
  58.  
  59. If dbPassword.Trim() = enteredPasswrd And userName.TrimEnd(vbCrLf.ToCharArray).ToLower() = txtUsername.Text.ToLower() Then
  60.  
  61.  
  62. Return True
  63.  
  64. 'Clear all fields
  65. txtPassword.Text = ""
  66. txtUsername.Text = ""
  67. Else
  68. MsgBox("Incorrect Password")
  69. End If
  70.  
  71. End While
  72.  
  73. Else
  74. MessageBox.Show("Username and Password do not match..", "Authentication Failure", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  75. 'Clear all fields
  76. txtPassword.Text = ""
  77. txtUsername.Text = ""
  78. End If
  79.  
  80. Return False
  81.  
  82. Catch ex As Exception
  83. MessageBox.Show("Error while connecting to SQL Server." & ex.Message)
  84.  
  85. Finally
  86. con.Close() 'Whether there is error or not. Close the connection.
  87.  
  88. End Try
  89.  
  90. End Function
  91.  
  92. Private Sub frmLogin_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  93. txtUsername.Focus()
  94.  
  95. End Sub
  96.  
  97. End Class
  98.  
  99. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  100.  
  101. Dim con As New SqlConnection
  102. Dim cmd As New SqlCommand
  103.  
  104. Try
  105.  
  106.  
  107. con.ConnectionString = "Data Source = location"
  108.  
  109. con.Open()
  110.  
  111. cmd.Connection = con
  112.  
  113. cmd.CommandText = " SELECT * FROM table WHERE adduser = '" & user & "'"
  114.  
  115. Dim lrd As SqlDataReader = cmd.ExecuteReader()
  116. If lrd.HasRows Then
  117. While lrd.Read()
  118.  
  119. End While
  120. End If
  121.  
  122.  
  123.  
  124. Catch ex As Exception
  125. MessageBox.Show("Error while connecting to SQL Server." & ex.Message)
  126.  
  127. Finally
  128. con.Close() 'Whether there is error or not. Close the connection.
  129.  
  130. End Try
  131.  
  132.  
  133.  
  134. End Sub
Add Comment
Please, Sign In to add comment