Linking to an Ms-Access DataSource using OledbDataAdapter in Visual F#

To link to an Ms-Access data source, the first thing that you’ll need to do is to create an object variable based on the OleDbConnection class. OleDbConnection specifies the name of the Data Provider and the Location of the database file that you wish to link to. To create an OleDbConnection object use the following syntax:
let Oledbconnectionobjvariable = new System.Data.OleDb.OleDbConnection("Provider=DataProviderName;
Data Source=Url and filename of the database file")
For instance:
let Oledbconnectionobjvariable = new System.Data.OleDb.OleDbConnection("Provider= Microsoft.Jet.OLEDB.4.0;
Data Source=C:\Mydatabasefile.mdb")
Microsoft.Jet.OLEDB.4.0 is a data provider used to link to Ms-Access 2003 or higher database files.
After creating an OledbConnectionObject, the next thing that you’ll need to do is to add an OleDbDataAdapter control, to specify how the data will be displayed on your form and the information about the connection. This can be done by using the following syntax:
let oledbdataadapterobjvariable= new System.Data.OleDb. OleDbDataAdapter("SQL statement", ConnectionString)
ConnectionString refers to the information about the connection and is saved in the Oledbconnection object.

Lastly, youll need to create a storage location for your table values. This can be done by creating a Dataset. To create a dataset, use the following syntax:
Let Datasetobjvariable=new Dataset()
For a simple example on using OleDbDataAdapter to an AccessDataSource, follow these steps:

Before the Database Connection and Binding process I want you to make an Ms-Access database file named “dbEmployee” containing a table named “tblEmployee”. Use the following specifications:

Field NameData Type Description
chrempno text Handles employee id
chrfnametext Handles employee’s name
chrlname text Holds employee’s last name


After designing the structure of your table, you can enter appropriate values for each field. For instance:

chrempnochrfnameChrlname
2010AJohn Doe
2010BJean Doe


Now that we are done creating a table, we can now link to it by using OleDbDataAdapter in Visual F#:


1. Click Start>All Programs>Microsoft Visual Studio 2008>Microsoft Visual Studio 2008.

2. Click File>New>Project>Select Visual F# in the project types>Select F# application in the Visual Studio installed templates category.

3. Click the Project menu>Add reference>Click the .Net tab>Locate then double-click System.Windows.Forms. Do step 3 again and this time, select System.Drawing and System.Data from the .Net tab.

4. Enter the following code after the line “// Learn more about F# at http://fsharp.net “:

// Learn more about F# at http://fsharp.net
//specifies the memory location of the class files
//that will be needed in our application
open System
open System.Windows.Forms
open System.Data
open System.Drawing
//creates a font
let ffont=new Font("Verdana", 9.75F,FontStyle.Italic, GraphicsUnit.Point)  
//creates a connection object
let oleconn = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=C:\Documents and Settings\station 2\My Documents\dbEmployee.mdb")
//creates an OleDbDataAdapter
let dataadpter = new System.Data.OleDb.OleDbDataAdapter("Select * from tblEmployee", oleconn)
//generates a dataset
let dataset11 = new DataSet()
//fills the dataset with recod values
dataadpter.Fill(dataset11,"tblEmployee")|>ignore
//creates a form
let dataform = new Form(Text="Use OleDbDataAdapter",AutoScaleDimensions=new System.Drawing.SizeF(60.0F, 13.0F),ClientSize=new System.Drawing.Size(300, 200),StartPosition=FormStartPosition.CenterScreen)
//creates an exit button
let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(190, 170))  
//creates a gridview control
let datagrid = new DataGridView(Dock=DockStyle.None)
//opens the connection to the datasource
oleconn.Open()
//assign our table
//as a datagrid datasource
datagrid.DataSource <- dataset11.Tables.["tblEmployee"]
//add the controls to our form
dataform.Font<-ffont
dataform.Controls.Add(datagrid)
dataform.Controls.Add(exitbutton)
//when the exit button is clicked
exitbutton.Click.Add(fun exit->
//close the form and dataconnection
dataform.Close()
oleconn.Close())

//executes our application
dataform.Show()
Application.Run(dataform)


5. Click the run icon to execute your application. You should now see an output similar to the following screen shot:


Wrapping the contents submitted from a textarea in PHP

By default, the texts submitted from a textarea in PHP are displayed in an unbroken single line. For instance, if you have entered the following lorem ipsum text:



It will be displayed as:



This simply tells us that PHP ignores the breaklines posted from a textarea control. This glitch cannot be solved by simply using the WRAP attribute of the textarea tag. Fortunately, PHP provide us with several string functions that can use to solve this problem. One of these functions is the wordwrap function which wraps text depending on the number of character specified by the user. The wordwrap function has the following syntax:

wordwrap(“text to wrap”, columnwidth ,breakline);
For instance:

wordwrap(“Hello world”,5,"<br/>")
For a sample usage of wordwrap in solving the textarea wrapping problem, follow these steps:

1. Start your text editor (Notepad, Notepad++, or Programmers Notepad).
2. Enter the following:

<HTML>
<body>

<!--if the submit button is not yet clicked-->
<?php
if(!$_POST['Submitted'])
:
?>

<!--display our form-->
<FORM ACTION="<?php echo $_SERVER['PHP_SELF']; ?>" Method="POST" >
<br/>
  Enter your message:<br>
<TEXTAREA  WRAp="hard" ROWS="8" COLS="30" NAME="usermsg"></TEXTAREA><br>
<INPUT TYPE="Submit" VALUE="Submit" Name="Submitted"></INPUT> <INPUT TYPE="Reset" VALUE="Clear">
</FORM>


</BODY>
</HTML>


<!--otherwise-->
<?php
else
:
?>


<!--display the text from the textarea-->
<?php
$msg=$_POST['usermsg'];
$wrapmsg=wordwrap($msg,30,"<br/>");
echo "$msg";
endif;
?>
?>

3. Save it as sample.php in the htdocs folder.
4. Launch your web browser.
5. Type the following in the address bar:
http://localhost/sample.php
6. You should now see the following:



7. Try entering a long text in the textarea to see the wordwrap effect. For more information on using the wordwrap string function visit http://www.php.net. That's all!