SIGN UP MEMBER LOGIN:    
ARTICLE

DataBinding with a ComboBox in FSharp

Posted by Alok Pandey Articles | F# October 31, 2011
In this article you will learn DataBinding with a ComboBox in F# in very simple way.
Reader Level:

Creating DataBase: We create a DataBase for getting records into a ComboBox. Here, in this example, the DataBase name is student_record, table name is student and Columns are Roll_No and Name. I have written code below for creating this database. 

create database student_record

use student_record

create table student
(   Roll_No int primary key,
    Name varchar(20) )

insert into student values(1,'Alok Pandey')
insert into student values(2,'Satish Kumar')
insert
into student values(3,'Amitabh Pandey')
insert
into student values(4,'Pramod Sharma')

insert
into student values(5,'Durgesh Pandey')

After creating the DataBase and table we start to a develop F# application for DataBinding with a ComboBox. We follow the following steps for this.

Step 1: Create an F# application.

step1.gif

Step 2: We go to Solution Explorer and Right Click on References.

step2.gif

Step 3: Now, Click on Add References. Then a pop-up window with caption Add Reference will open as in the below figure.

databinding with combobox in f#

Step 4: After Step-3, Click at .Net on Add Reference window and select System.Windows.Forms and System.Data with holding down Ctrl key and Click on Ok.
 
step4.gif

Step 5: Write the below F# code in the Program.fs file.

open System
open
System.Windows.Forms
open
System.Data.SqlClient
open
System.Data
 

//  cteating connection string

let
constring = @"Data Source=Server_Name;Initial Catalog=student_record;Integrated Security=True"
//Creating SqlDataAdapter

let
da = new SqlDataAdapter("select * from student",constring)
//creating DataSet

let
ds = new DataSet()
//filling the DataSet

da.Fill(ds)|>ignore

//creating Form

let
form = new Form()
//creating ComboBox

let
cmb = new ComboBox()
//Adding Control

form.Controls.Add(cmb)

//Binding ComboBox

cmb.DataSource <- ds.Tables.[0]
cmb.DisplayMember <- "Roll_No"// For displaying column values
 
Application.Run(form)

Now we run this application.

Output: 

databinding with combobox in f#

We Click on ComboBox. We look that it show all the Roll_No.

databinding with combobox in f#

In Student table, There are two columns. Suppose we want to select Roll_No of students into ComboBox and display student name in a Label or TextBox whose Roll_No is selected in the ComboBox. For this, We will Write code on SelectedValueChanged event of ComboBox. In this example, I am displaying record in Label. Write the following code.

open
System
open
System.Windows.Forms
open
System.Data.SqlClient
open
System.Data
//  cteating connection string

let
constring = @"Data Source=Server_Name;Initial Catalog=student_record;Integrated Security=True"
//creating SqlDataAdapter

let
da = new SqlDataAdapter("select * from student",constring)
//creating DataSet

let
ds = new DataSet()
//filling the DataSet

da.Fill(ds)|>ignore

//creating Form

let
form = new Form()
//creating ComboBox

let
cmb = new ComboBox()
let
lbl = new Label(Top = 50)
//Adding Control

form.Controls.Add(cmb)
form.Controls.Add(lbl)

//Binding ComboBox

cmb.DataSource <- ds.Tables.[0]
cmb.DisplayMember <- "Roll_No"// For displaying column values
cmb.ValueMember <- "Name"// for containing value
cmb.DataSource <- ds.Tables.[0]
cmb.SelectedValueChanged.Add(fun a -> lbl.Text <- cmb.SelectedValue.ToString() )
 
Application.Run(form)

Now we run this application.

Output:

databinding with combobox in f#
 

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor