Wrox Programmer Forums
VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET 2002/2003 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old June 24th, 2004, 03:12 AM
Authorized User
Join Date: Jun 2004
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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()

        'This call is required by the Windows Form Designer.

        '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
            End If
        End If
    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.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 = ""
        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 = ""
        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"
        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"
        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"
        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
        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"

    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

            oConn.ConnectionString = "server=D401L11J;" _
            & "database=HumanResources;" _
            & "userId=sa;Password=manutd;"
            Dim oDA As SqlDataAdapter = New SqlDataAdapter("select employeeId from userTable where (username ='" & TextBox1.Text & "' and password = '" & TextBox2.Text & "'", oConn)

            For Each oRow In oTable.Rows
                varemployeeId = CInt(oRow("employeeid"))
        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 = ""
                LoginSuccess = True
            End If

Old June 24th, 2004, 05:04 AM
Friend of Wrox
Join Date: Jul 2003
Posts: 142
Thanks: 0
Thanked 2 Times in 2 Posts

I hope you changed the server name/ system admin username and password before showing it here - I think displaying the real ones on the internet is a bad idea.

Your code looks ok, so what I'd suggest is using SQL Query Analyser to make sure that your query is ok. To get the closest match, step through the code and once the query is built, cut and paste it into query analyser and run it - see what you get back. Use query analyser to modify the query if it's not quite right, then change your code.

Old September 12th, 2004, 10:07 PM
Registered User
Join Date: Sep 2004
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts

Is it the missing closing bracket in the SQL string?

Similar Threads
Thread Thread Starter Forum Replies Last Post
what's wrong with this code? hertendreef Visual Basic 2005 Basics 0 March 2nd, 2007 10:29 AM
Whats wrong? Agentofnight Beginning PHP 3 April 17th, 2005 04:11 AM
Whats wrong with the GUI display Sandz Java GUI 1 February 5th, 2005 08:28 AM
Whats wrong with this code? Thomas82 Classic ASP Databases 6 February 26th, 2004 09:40 AM
whats wrong with this code ? qwprince Classic ASP Basics 5 July 27th, 2003 07:46 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.