whats wrong with this code?
hi guys i have a windows form on it has a username text field, password text field,a button and a label. When the user enters their password and username their employee ID number should appear on the label. The code i have is shown below, the problem is basically when i enter a username and password which is in the database i still get message saying incorrect login!
Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlClient
Imports System.Data.SqlTypes
Imports System.Diagnostics
Imports System.ComponentModel
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
Friend WithEvents username As System.Windows.Forms.Label
Friend WithEvents Password As System.Windows.Forms.Label
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Label1 As System.Windows.Forms.Label
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.TextBox1 = New System.Windows.Forms.TextBox()
Me.TextBox2 = New System.Windows.Forms.TextBox()
Me.username = New System.Windows.Forms.Label()
Me.Password = New System.Windows.Forms.Label()
Me.Button1 = New System.Windows.Forms.Button()
Me.Label1 = New System.Windows.Forms.Label()
Me.SuspendLayout()
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(120, 24)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(104, 20)
Me.TextBox1.TabIndex = 0
Me.TextBox1.Text = ""
'
'TextBox2
'
Me.TextBox2.Location = New System.Drawing.Point(120, 56)
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.Size = New System.Drawing.Size(104, 20)
Me.TextBox2.TabIndex = 1
Me.TextBox2.Text = ""
'
'username
'
Me.username.Location = New System.Drawing.Point(8, 24)
Me.username.Name = "username"
Me.username.Size = New System.Drawing.Size(96, 16)
Me.username.TabIndex = 2
Me.username.Text = "Username"
'
'Password
'
Me.Password.Location = New System.Drawing.Point(16, 56)
Me.Password.Name = "Password"
Me.Password.Size = New System.Drawing.Size(96, 16)
Me.Password.TabIndex = 3
Me.Password.Text = "password"
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(192, 192)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(88, 32)
Me.Button1.TabIndex = 4
Me.Button1.Text = "login in"
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(24, 264)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(96, 24)
Me.Label1.TabIndex = 5
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(368, 309)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label1, Me.Button1, Me.Password, Me.username, Me.TextBox2, Me.TextBox1})
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim varUser_Name As String
Dim varPassword As String
Dim varemployeeId As Int16
Dim LoginSuccess As String
Dim oConn As SqlConnection = New SqlConnection()
Dim oTable As DataTable = New DataTable()
Dim oRow As DataRow
varUser_Name = TextBox1.Text
varPassword = TextBox2.Text
Try
oConn.ConnectionString = "server=D401L11J;" _
& "database=HumanResources;" _
& "userId=sa;Password=manutd;"
oConn.Open()
Dim oDA As SqlDataAdapter = New SqlDataAdapter("select employeeId from userTable where (username ='" & TextBox1.Text & "' and password = '" & TextBox2.Text & "'", oConn)
oDA.Fill(oTable)
oConn.Close()
For Each oRow In oTable.Rows
varemployeeId = CInt(oRow("employeeid"))
Next
Catch ex As Exception
Debug.WriteLine(ex.Message & ": " & ex.StackTrace)
Label1.Text = varemployeeId
If varemployeeId = 0 Then
LoginSuccess = False
MsgBox("Incorrect Login.", MsgBoxStyle.OKOnly, "Login Error")
TextBox1.Text = ""
TextBox2.Text = ""
TextBox1.Focus()
Else
LoginSuccess = True
End If
|