Registration Form in F#

Introduction

In this article you will see how to create a Registration Form in Windows Forms application. The user login details are stored in a SQL Server database.

Create Database & Tables

 

create database Registration

use Registration

create table UserLoginDetails(

UserName nvarchar(max),

Password nvarchar(max),

FirstName varchar(max),

LastName varchar(max),

Address nvarchar(max),

City varchar(max),

Gender varchar(max)

)

 

To execute the schema of the table write the query and press F5.

SELECT * FROM EmployeeInfo

dbschema.jpg


 

Stored Procedure

The following is the Stored Procedure for the User Login Details:

create proc InsertData

@un nvarchar(max),

@pwd nvarchar(max),

@fn varchar(max),

@ln varchar(max),

@add nvarchar(max),

@cty varchar(max),

@gn varchar(max)

as

begin

if not exists(select UserName from UserLoginDetails where UserName=@un)

insert into UserLoginDetails values(@un,@pwd,@fn,@ln,@add,@cty,@gn)

end

 

Now let's use the following procedure.

Step 1:

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

createpapp.jpg

Step 2:

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

selct-ref.jpg


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 and click on "Ok." 

import.jpg

Step 4:

The following is the code for the F# application:

open System

open System.Drawing

open System.Data.SqlClient

open System.Windows.Forms

open System.Data

 

let form = new Form()

form.BackColor<-Color.Azure

//connection string

let constring = @"Data Source=MCNDESKTOP34;Initial Catalog=Registration;User ID=sa; Password=mcn@123"

//Creating user controls

let lblmsg=new Label(Top=0,Left=80)

let txtun = new TextBox(Top = 30, Left = 120)

let txtpwd = new TextBox(Top = 70, Left = 120)

txtpwd.Text <- ""         

txtpwd.PasswordChar <-'*'

txtpwd.MaxLength <- 14   

let txtfn = new TextBox(Top = 120, Left = 120)

let txtln = new TextBox(Top = 170, Left = 120,Height=20)

let txtadd = new TextBox(Top = 220, Left = 120)

let txtcity = new TextBox(Top = 270, Left = 120)

let radio1=new RadioButton(Text="Male",Top=320,Left=130) 

let radio2=new RadioButton(Text="Female",Top=340,Left=130)

let btn1 = new Button(Top = 380,Left=80)

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

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

let lbl3 = new Label(Top = 120, Left = 0, Height = 20)

let lbl4 = new Label(Top = 170, Left = 0, Height = 20)

let lbl5 = new Label(Top = 220, Left = 0, Height = 20)

let lbl6 = new Label(Top = 270, Left = 0, Height = 20)

let lbl7 = new Label(Top = 320, Left = 0, Height = 20)

form.Text <- "Registration Form"

lblmsg.Text<-"Registration Form"

btn1.Text <- "Submit"

lbl1.Text <- "Enter UserName"

lbl2.Text <- "Enter Password"

lbl3.Text <- "Enter First Name"

lbl4.Text <- "Enter Last Name"

lbl5.Text <- "Address"

lbl6.Text <-"City"

lbl7.Text <-"Gender"

//Adding user control

form.Controls.Add(txtun)

form.Controls.Add(txtpwd)

form.Controls.Add(txtfn)

form.Controls.Add(txtln)

form.Controls.Add(txtadd)

form.Controls.Add(txtcity)

form.Controls.Add(lblmsg)

form.Controls.Add(lbl1)

form.Controls.Add(lbl2)

form.Controls.Add(lbl3)

form.Controls.Add(lbl4)

form.Controls.Add(lbl5)

form.Controls.Add(lbl6)

form.Controls.Add(lbl7)

form.Controls.Add(btn1)

form.Controls.Add(radio1) 

form.Controls.Add(radio2) 

//creating SqlConnection

let con = new SqlConnection(constring)

//open connection

con.Open()

let com = new SqlCommand()

com.Connection <- con

com.CommandType <- CommandType.StoredProcedure

com.CommandText <- "InsertData"

radio1.Click.Add(fun r1msg-> 

radio1.Checked<-true

radio2.Checked<-false)

radio2.Click.Add(fun r2msg-> 

radio2.Checked<-true 

radio1.Checked<-false

btn1.Click.Add( fun _ ->

com.Parameters.AddWithValue("@un", txtun.Text ) |> ignore

com.Parameters.AddWithValue("@pwd", txtpwd.Text ) |> ignore

com.Parameters.AddWithValue("@fn", txtfn.Text ) |> ignore

com.Parameters.AddWithValue("@ln", txtln.Text ) |> ignore

com.Parameters.AddWithValue("@add", txtadd.Text ) |> ignore

com.Parameters.AddWithValue("@cty", txtcity.Text ) |> ignore

com.Parameters.AddWithValue("@gn", radio1.Text ) |> ignore

com.ExecuteNonQuery() |> ignore //ExecuteNonQuery

con.Close()

MessageBox.Show("Congratutaliton Account Created Successfully..") |> ignore

txtun.Clear()

txtpwd.Clear()

txtfn.Clear()

txtln.Clear()

txtadd.Clear()

txtcity.Clear()

txtcity.Focus() |> ignore)

Application.Run(form)


Step 5:

Debug the application by pressing F5 and the results will be in the application as shown in the following figure.

after-debug.jpg

Step 6:

Fill in the user details in the usercontrols.

detailsentry.jpg

Step 7:

Now the details are submitted successfully in the database; see the figure given below.

registersuccesfully.jpg

Step 8:

Now you can see the userlogin details in the database.

database.jpg

Summary

In this article you saw how to make a user registration form and the user login details are stored in the database using a Stored Procedure.


Similar Articles