AllExperts > Experts 
Search      

VB.NET

Volunteer
Answers to thousands of questions
 Home · More Questions · Answer Library  · Encyclopedia ·
More VB.NET Answers
Question Library

Ask a question about VB.NET
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About Stephen Jackson
Expertise
I can help with questions regarding VB.NET syntax and object references, with data interfaces and with the design and creation of robust, data aware object classes. I can also be very helpful with creating distributable applications and provide tricks and tips on .msi creation. I also have extensive experience in designing SQL Server Databases and interfacing them with VB.NET. I try to avoid web specific questions, as that is an area of expertise all its own.

Experience

Experience in the Area:
I have been a programmer in Visual Basic since version 1.0 and have worked with VB.NET (which is infinitely more powerful than previous versions) since its initial release and SQL Server, both as a corporate IT professional and professional consultant. I first wrote Basic in 1976 on a TRS 80 and have worked in Visual Basic 1.0 and every subsequent release of Microsoft Visual Basic. I worked for over 7 years as a Senior Level Consultant in the area and currently hold a Project Manager position in IT.

Education and Credentials:
MBA in Econometrics, 1983, University of Memphis. BBA in Financial Management, 1982, Fogelman College of Business and Economics, University of Memphis.
Microsoft Certified Professional

Areas of Special Expertise:
My specialty is the design of Object Oriented Solutions with robust, data aware object classes. I generally avoid the classic ‘Three Tier’ model as I find it redundant and cumbersome to maintain. I also specialize in the creation of ‘User Friendly’ User Interfaces which help lessen the need for user training and help prevent user error. I work best with Windows Forms based applications, and while I do work in C# as well, I prefer to limit my questions here to Windows Forms based applications created in Visual Basic.NET and SQL Server. I wil also address questions relating to the distribution and installation of Windows Forms based applications created in VB.NET.
 
   

You are here:  Experts > Computing/Technology > Basic > VB.NET > VB.NET 2008. Default login form not functioning

Topic: VB.NET



Expert: Stephen Jackson
Date: 4/23/2008
Subject: VB.NET 2008. Default login form not functioning

Question
QUESTION: Hi Stephen, how are you?

May you help me with this little task please?

I have done the default login form in VB.NET 2008 and the code has no errors but whenever I run the form, i always get for whatever action the messsagebox " you must enter a username" , besides the cancel button is not functionning.


Imports System.Windows.Forms

Imports System.Data.SqlClient
Imports System.Security
Imports System.Security.Cryptography

Public Class LoginForm
   Inherits System.Windows.Forms.Form

   ' TODO: Insert code to perform custom authentication using the provided username and password
   ' (See http://go.microsoft.com/fwlink/?LinkId=35339).  
   ' The custom principal can then be attached to the current thread's principal as follows:
   '     My.User.CurrentPrincipal = CustomPrincipal
   ' where CustomPrincipal is the IPrincipal implementation used to perform authentication.
   ' Subsequently, My.User will return identity information encapsulated in the CustomPrincipal object
   ' such as the username, display name, etc.

   Private Sub LoginForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

       InitializeComponent()


   End Sub
   Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click

       Dim AuthenticationReader As System.Data.SqlClient.SqlDataReader

       'Intitalizes connection object
       Dim SqlConnection As New SqlClient.SqlConnection
       SqlConnection.ConnectionString = "Server=owner;database=***"

       'Initializes command object to execute statements.
       Dim AuthenticationCommand As New SqlClient.SqlCommand
       AuthenticationCommand.Connection = SqlConnection

       'Declarations
       Dim DBusername As String
       Dim DBuserpassword
       Dim DBuserID As Integer
       Dim DBlogggedname As String
       Dim DBprivilege As String
       
       If UsernameTextBox.Text.Length = 0 Then


           MessageBox.Show("You must enter a username", "Invalid Username", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
           UsernameTextBox.Focus()



       ElseIf PasswordTextBox.Text.Length = 0 Then

           MessageBox.Show("You must enter a password", "Invalid Password", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
           PasswordTextBox.Focus()
       Else

           DBusername = Me.UsernameTextBox.Text
           DBuserpassword = Me.PasswordTextBox.Text


           'Executes command
           Dim AuthenticationString As String
           AuthenticationString = "select *  from *** where UserName=  '" & UsernameTextBox.Text & "'"

           AuthenticationCommand.CommandText = AuthenticationString
           SqlConnection.Open()
           AuthenticationCommand.ExecuteNonQuery()

           'Encrypt Password
           Dim md5hasher As New MD5CryptoServiceProvider
           Dim hashedBytes As Byte()
           Dim encoder As New System.Text.UTF8Encoding

           hashedBytes = md5hasher.ComputeHash(encoder.GetBytes(PasswordTex  tBox.Text))


          

           AuthenticationReader = AuthenticationCommand.ExecuteReader()
           If AuthenticationReader.HasRows Then
               While (AuthenticationReader.Read())
                   DBusername = AuthenticationReader("UserName")
                   DBuserpassword = AuthenticationReader("Password")
                   DBuserID = AuthenticationReader("UserID")
                   DBlogggedname = AuthenticationReader("Name")
                   DBprivilege = Trim(AuthenticationReader("Privilege"))
               End While
           Else

               MessageBox.Show("Invalid username!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
               UsernameTextBox.Focus()
               Exit Sub
           End If
           AuthenticationReader.Close()

           SqlConnection.Close()



           If bHashEquals(hashedBytes, DBuserpassword) Then


               Dim Frame As New Frame()
               Frame.Show()
               Me.Hide()

           Else

               MessageBox.Show("Invalid password!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
               PasswordTextBox.Focus()
           End If
       End If




   End Sub
   Private Function bHashEquals(ByVal byHash1 As Byte(), ByVal byHash2() As Byte) As Boolean
       bHashEquals = False
       If byHash1.Length = byHash2.Length Then
           Dim i As Integer
           Do While (i < byHash1.Length) AndAlso (byHash1(i) = byHash2(i))
               i = i + 1
           Loop
           If i = byHash1.Length Then
               bHashEquals = True
           End If
       End If

   End Function

   Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
       Me.Close()
   End Sub

End Class


Regards


ANSWER: Carolina,

I sent myself a reminder to look up the answer to this tomorrow .. please forgive me forgetting today as I had planned!

You are most kind with your reviews; thank you! If you had a feather, you would have tickled me to death :c)

Stephen

Carolina,

OK, I am looking this over .. it may take a bit of back and forth to find the issue.  I see that this code logs into a SQL Server ... correct?  It looks as though this code has no reference to a specific instance or database.  This may be why it does not work ... do you have SQL Server?  If so, then you will need to reference the server and the database in the 'SqlConnection.ConnectionString = "Server=owner;database=***"' line.  Now if you  do not, then we know the problem :c)

Just bear with me; we will get to the bottom of this!

Stephen

---------- FOLLOW-UP ----------

QUESTION: Thank u Stephen, u r very kind and i am honored to meet u.
In fact i would like to tell u to not bother yourself with this issue anymore cause i resolved it, the problem was in the configuration of SQL.

If I had further questions, i would like to ask u about them.

Best Regards

Answer
Carolina,

That is great! I am glad you got it taken care of and hope I at least helped a little (sometimes you only need a little help).  It is a pleasure to meet you, too, and always feel free to ask anything you like ....

Sincerely,

Stephen Jackson

Add to this Answer    Ask a Question



  Rate this Answer
   Was this answer helpful?
Not at allDefinitely              
   12345  

     
About Us | Advertise on This Site | User Agreement | Privacy Policy | Help
Copyright  © 2008 About, Inc. About and About.com are registered trademarks of About, Inc. The About logo is a trademark of About, Inc. All rights reserved.