After selecting "Add References", in the framwork template you need to select "System.Windows.Forms", "System.Drawing", "System.Xml" and "System.Data" while holding down the Ctrl key and click on "Ok".
//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 , Height =30, Width = 120)
let lblName = new Label(Top =50 , Height =30, Width = 120)
let lblAddress = new Label(Top =80 , Height =30, Width = 120)
let lblPammount = new Label(Top =110 , Height =30, Width = 120)
let lblbfinsert = new Label(Top =190 ,Left=30, Height =20, Width = 80)
let lblafinsert = new Label(Top =190 , Left=250, Height =20, Width = 80)
// TextBoxes
let txtID = new TextBox(Top =20, Left =120)
let txtName = new TextBox(Top =50, Left =120)
let txtAddress = new TextBox(Top =80, Left =120)
let txtPamount = new TextBox(Top =110,Left=120)
lblId.Text <- "Customer ID:"
lblName.Text <- "Customer Name :"
lblAddress.Text <- "Customer Address :"
lblPammount.Text <- "Paid Ammount"
lblbfinsert.Text <- "Before Insert"
lblafinsert.Text <- "After Insert"
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)
btnSave.Text <- "SUBMIT"
btnTable1.Text <- "TABLE-1"
btnTable2.Text <- "TABLE-2"
//Adding Controls to Form
MyForm.Controls.Add(lblId)
MyForm.Controls.Add(lblName)
MyForm.Controls.Add(lblAddress)
MyForm.Controls.Add(lblPammount)
MyForm.Controls.Add(lblbfinsert)
MyForm.Controls.Add(lblafinsert)
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)
//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()
let ds = new DataSet()
//Insert record into DataBase using Stored procedure
btnSave.Click.Add(fun _->
con.Close()
com1.Connection <- con
con.Open()
com1.CommandType <- CommandType.StoredProcedure
com1.CommandText <- "InsertData" // 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 Saved in Table-1..")|>ignore
txtID.Clear()
txtName.Clear()
txtAddress.Clear()
txtPamount.Clear()
txtID.Focus() |>ignore
)
btnTable1.Click.Add(fun _ ->
let adapter = new SqlDataAdapter("select * from Table1",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 Table2",con)
let dt = new DataTable()
adapter.Fill(dt) |>ignore
let gridview = new DataGridView(Top=250,Left=250,Width=380,Height=150)
MyForm.Controls.Add(gridview)
gridview.DataSource <- dt
)
Application.Run(MyForm)
In this article you saw how to create an "after insert" trigger on a table. First we explained the trigger and how to use the "after insert" trigger and why we use the triggers.