.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> WPF >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Developing a Login Box the Simplest Way

Posted By:Hefin Dsouza       Posted Date: April 27, 2009    Points: 25    Category: WPF    URL: http://www.dotnetspark.com  
 

Developing a Login Box the Simplest Way.

 

Introduction: This article will guide you to develop your own login box in the simplest way ever. In the sample that accompanies this article I have used Visual Studio 2008 and developed the Application in WPF (Windows Presentation Foundation) with Visual Basic.Net. In the attachments I have given the whole project with functionality.

Before reading further Please make sure  that that you know how to create a WPF project using Visual Studio 2008 and you know how to connect to Access Database in Visual Basic.Net

Let us Begin.

 

Step 1. I have designed a simple Login Form using the following controls.

Control Name

Control Type

txtUserName

TextBox

txtPassword

TextBox

cmdExit

Button

cmdLogin

Button

lblUsername

Label

lblPassword

Label

lblTitle

Label

The Figure Shows How the Login Box Looks

 

Step 2. Create a table in a database which has the following fields name the Table as Login.

Column Name

DataType

Primary Key

UserName

Text

Yes

Password

Text

No

 

Step 3. Connect your Application to the Database.

For This First Create the Connection Object. As we are using Access as the backend we need to use the OLEDB objects.

First Import System.Data and System.Data.Oledb namespaces in your Code.

i.e.

Imports System.Data

Imports System.Data.OleDb

Use the following code to create an Connection object

Dim objCon As New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0 ; data source=sample.mdb")

We are using Provider Microsoft.Jet.Oledb.4.0 because we are working with one of Microsoft's Jet Databases "Access 2000".


Step 4.
Create dataset's and data adapters.

We need to create a Data Set and a Data Adapter to complete this simple problem.

 

Use the Following code to create the Dataset and DataAdapter

Dim ds As New DataSet

Dim dap As OleDbDataAdapter

  • Note I have not Initialized the OledbDataAdapter here because the whole concept of login depends on the Select Statement that we pass to the dataadapter.

 

Final Step. Complete The Login.

On the cmdLogin button Click Event Type this code.
Private Sub cmdLogin_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles cmdLogin.Click

        ds.Clear()

        dap = New OleDbDataAdapter("select * from Login where username='" & txtUserName.Text & "' and [password]='" & txtPassword.Text & "'", objCon)

        dap.Fill(ds)

 

        If ds.Tables(0).Rows.Count <> 0 Then

            MessageBox.Show("Login Successfull!!!")

        Else

            MessageBox.Show("Login Failed!!!")

        End If

End Sub

The Select statement that gets generated is Select * From Login where UserName="what ever user enters in the username text box" and password="what ever user enters in the password textbox"

The Fill method of the Adapter gets the data in the Dataset.
The If condition checks whether Rows are returned and if No Rows are returned it displays the Error Message and if Rows are returned it displays the Welcome Message.

Conclusion: This is the simplest way to achieve a login functionality in our program. This method can also be used for ASP.net.

Regards

Hefin D'souza
[Software Freelancer And Trainner]


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend