Login Form in F#

Introduction

In this article you will see how to create a Login Form in a Windows Forms application. Before making the Login Form I have already made a Registration Form. If you want to see just it then click on Registration Form. In this form is a database and Stored Procedure for the User Login details.

First if you want to see the UserLogin details in database then just write the query in SQL Server where you have submitted all the records. Registraion is the database and userlogindetails is the table in the SQL Server database.

use Registration

select * from userlogindetails

database.jpg

 

How to Create a Login Form in F#

Now let's use the following procedure.

Step 1:

Open Visual Studio then select "Create New Project" --> "F# Console Application".

Createapp.jpg

Step 2:

Now go the Solution Explorer on the right side of the application. Right-click on "References" and select "Add references".

select-ref.jpg

Right-click on "References".

add-ref.jpg


Step 3:

After selecting "Add References", in the framework template you need to select "System.Windows.Forms", "System.Data", "System.Data.SqlClient" and "System.Drawing" while holding down the Ctrl key then click on "Ok."

import.jpg

Step 4:

Provide the following code for the F# application:

open System

open System.Drawing

open System.Data.SqlClient

open System.Windows.Forms

open System.Data

 

let constring = @"Data Source=ServerName;Initial Catalog=Registration;User ID=UserId; Password="

let userloginform=new Form(Text="Display Login Form",FormBorderStyle=FormBorderStyle.FixedSingle)

userloginform.BackColor<-Color.Azure

let txtun = new TextBox(Top = 30, Left = 120,Height=40,Width=140)

let txtpwd = new TextBox(Top = 70, Left = 120,Height=40,Width=140)

txtpwd.Text <- ""         

txtpwd.PasswordChar <-'*'

txtpwd.MaxLength <- 14  

let lbl1 = new Label(Top = 30, Left = 0, Height = 20)

let lbl2 = new Label(Top = 70, Left = 0, Height = 20)

lbl1.Text <- "Enter UserName:"

lbl2.Text <- "Enter Password:"

let loginbutton=new Button(Text="Login",Top=120,Left=40,AutoSize=true)

let fexitbutton=new Button(Text="Cancel",Top=120,Left=130,AutoSize=true)

userloginform.Controls.Add(txtun) 

userloginform.Controls.Add(txtpwd)

userloginform.Controls.Add(lbl1)

userloginform.Controls.Add(lbl2)

userloginform.Controls.Add(loginbutton)

userloginform.Controls.Add(fexitbutton)

loginbutton.Click.Add(fun nxtform->

if(txtun.Text="" || txtpwd.Text="")then

 MessageBox.Show("Fields can not be empty","Validate Textboxes",MessageBoxButtons.OK, MessageBoxIcon.Information)|>ignore

 userloginform.Hide()

let con = new SqlConnection(constring)

let com1=new SqlCommand()

con.Open()

com1.Connection <- con

com1.CommandText <- " select * from UserLoginDetails where UserName = '"+txtun.Text+"' and Password='"+txtpwd.Text+"' "

let dr = com1.ExecuteReader()

while dr.Read() do

let loginform=new Form(Text="My Account Form")

loginform.BackColor<-Color.Azure

let lblname=new Label(Text="Welcome"+" "+dr.Item(2).ToString(),Top = 30, Left = 10, Height = 180,Width=130)

let logoutbutton=new Button(Text="Logout",Top=0,Left=200)

loginform.Show()

loginform.Controls.Add(logoutbutton)  

loginform.Controls.Add(lblname)

logoutbutton.Click.Add(fun prevform->

userloginform.Show()

txtun.Text<-""

txtpwd.Text<-""

loginform.Hide()))

Application.Run(userloginform)

 

Step 5:

Debug the application by pressing F5 and the following result will appear from the application as in the figure below.

after-debug.jpg

Step 6:

Enter the login details for the first user as in the figure below.

loginentry.jpg

The Welcome page for Pankaj:

welocome-pankaj.jpg

Step 6:

Enter the login details for Second user as in the figure below.

nimitlogin.jpg

The Welcome page for Nimit.

welcome nimit.jpg

Step 8:

If without giving login details you click on the login button then the textboxes validates.

validate-inputs.jpg

Step 9:

If you want to see the details of a user login in the database then see the figure given below.

database.jpg

Summary

In this article you saw how to make a user Login Form. Before making the Login Form you need to be registered first. For the registration form just click on Registraion Form.


Similar Articles