//Importing Namespaces
open System
open System.Windows.Forms
open System.Drawing
open System.Data.SqlClient
open System.Data
// Creating Form & User Controls
let MyForm = new Form(Height =400, Width = 500)
// Labels
let lblId = new Label(Top =20 ,Left=0, Height =30, Width = 120)
let lblName = new Label(Top =50 ,Left=0, Height =30, Width = 120)
let lblAddress = new Label(Top =80 ,Left=0, Height =30, Width = 120)
let lblPammount = new Label(Top =110 ,Left=0, Height =30, Width = 120)
let lblafupdate = new Label(Top =190 ,Left=30, Height =20, Width = 80)
let lblbafupdate = new Label(Top =190 , Left=250, Height =20, Width = 120)
// TextBoxes
let txtID = new TextBox(Top =20, Left =120,BorderStyle=BorderStyle.FixedSingle)
let txtName = new TextBox(Top =50, Left =120,BorderStyle=BorderStyle.FixedSingle)
let txtAddress = new TextBox(Top =80, Left =120,BorderStyle=BorderStyle.FixedSingle)
let txtPamount = new TextBox(Top =110,Left=120,BorderStyle=BorderStyle.FixedSingle)
let txtdeleteid = new TextBox(Top =300,Left=80,BorderStyle=BorderStyle.FixedSingle)
lblId.Text <- " Enter Customer ID:"
lblName.Text <- "Customer Name :"
lblAddress.Text <- "Customer Address :"
lblPammount.Text <- "Paid Ammount:"
lblafupdate.Text <- "After Update"
lblbafupdate.Text <- "Before/After Update"
let btnSave = new Button(Top =150, Left =80, Height =20, Width =60)
let btnTable1 = new Button(Top =210, Left =30, Height =20, Width =60)
let btnTable2 = new Button(Top =210, Left =250, Height =20, Width =60)
let btnsearch = new Button(Top =20, Left =250, Height =20, Width =80)
btnSave.Text <- "Update"
btnTable1.Text <- "TABLE-1"
btnTable2.Text <- "TABLE-2"
btnsearch.Text <- "Search"
//Adding Controls to Form
MyForm.Controls.Add(lblId)
MyForm.Controls.Add(lblName)
MyForm.Controls.Add(lblAddress)
MyForm.Controls.Add(lblPammount)
MyForm.Controls.Add(lblafupdate)
MyForm.Controls.Add(lblbafupdate)
MyForm.Controls.Add(txtID)
MyForm.Controls.Add(txtName)
MyForm.Controls.Add(txtAddress)
MyForm.Controls.Add(txtPamount)
MyForm.Controls.Add(btnSave)
MyForm.Controls.Add(btnTable1)
MyForm.Controls.Add(btnTable2)
MyForm.Controls.Add(btnsearch)
//Database Connectivity
let constr = @"Data Source=MCNDESKTOP34;Initial Catalog=DemoTriggers; User Id=sa; Password=mcn@123"
let con = new SqlConnection(constr)
let com1 = new SqlCommand()
btnsearch.Click.Add(fun _->
con.Close()
com1.Connection <- con
con.Open()
com1.CommandText <- " select * from FirstTable where CustID = "+txtID.Text+" "
let dr = com1.ExecuteReader()
while dr.Read() do
txtID.Text <- dr.Item(0).ToString()
txtName.Text <- dr.Item(1).ToString()
txtAddress.Text <- dr.Item(2).ToString()
txtPamount.Text <- dr.Item(3).ToString())|>ignore
//Update record into DataBase using Stored procedure
btnSave.Click.Add(fun _->
con.Close()
com1.Connection <- con
con.Open()
com1.CommandType <- CommandType.StoredProcedure
com1.CommandText <- "UpdateData" //Updatedata is a stored procedure
com1.Parameters.AddWithValue("@cid",txtID.Text) |> ignore
com1.Parameters.AddWithValue("@cname",txtName.Text ) |> ignore
com1.Parameters.AddWithValue("@cadd",txtAddress.Text) |> ignore
com1.Parameters.AddWithValue("@pammount",txtPamount.Text) |> ignore
com1.ExecuteNonQuery()|> ignore
con.Close()
MessageBox.Show("Records updated in both tables")|>ignore
txtID.Clear()
txtName.Clear()
txtAddress.Clear()
txtPamount.Clear()
txtID.Focus() |>ignore
)
btnTable1.Click.Add(fun _ ->
let adapter = new SqlDataAdapter("select * from FirstTable",con)
let ds = new DataSet()
adapter.Fill(ds) |>ignore
let gridview = new DataGridView(Top=250,Width=420, Height=150)
MyForm.Controls.Add(gridview)
gridview.DataSource <- ds.Tables.[0]
)
btnTable2.Click.Add(fun _ ->
let adapter = new SqlDataAdapter("select * from SecondTable",con)
let dt = new DataTable()
adapter.Fill(dt) |>ignore
let gridview = new DataGridView(Top=250,Left=280,Width=400,Height=150)
MyForm.Controls.Add(gridview)
gridview.DataSource <- dt
)
Application.Run(MyForm)