ipFaces – Connecting to SQL Server Database for BlackBerry / iPad Clients

In this article you will learn how to use ipFaces Application Framework along with Microsoft .Net Framework 3.5 and Visual Studio 2008 to develop data rich applications.

In my previous blog, I wrote about some basics of how to create applications using ipFaces. Check here https://mukhtarahmedsblog.wordpress.com/2011/01/19/developing-applications-for-iphone-blackberry-and-j2me-in-asp-net/

Here you will see step-by-step guide to develop application which communicate with SQL Server 2005.

Assuming that you have installed ipFaces Framework https://mukhtarahmedsblog.wordpress.com/2011/01/19/programming-the-iphone-is-now-simple-fun-easy/

******************************************************************

 To Create ipFaces project follow the steps:

ipFaces Form Layout:

******************************************************************

<%@ Page Language=”C#” AutoEventWireup=”true” CodeBehind=”DatabaseConnectivity.aspx.cs” Inherits=”HelloWorld.DatabaseConnectivity” %>

<%@ Register Assembly=”ipfaces.net-1.2″ Namespace=”Org.IPFaces.Net.Controls” TagPrefix=”ipf” %>

<ipf:Form ID=”form1″ runat=”server” Title=”SQL Connection Test” BackColor=”Coral”>

    <ipf:Screen ID=”screen1″ runat=”server” Title=”iPFaces Form” BackColor=”Coral”>

        <ipf:Image ID=”Image1″ runat=”server” ImageUrl=”images/ipfaces.png” />

        <ipf:Label ID=”label1″ runat=”server” Text=”Hello World” FontSize=”1″ />

        <ipf:Button ID=”Button1″ Title = “Load Data”  BackColor=”#0066CC” runat=”server” OnClick=”Button1_Click” ForeColor=”Yellow” />

    </ipf:Screen>

</ipf:Form>

 ******************************************************************

Code Behind:

using System;

using System.Web;

using Org.IPFaces.Net.Controls;

using Org.IPFaces.Net.Pages;

using System.Data;

using System.Data.SqlClient;

using System.Text;

namespace HelloWorld

{

    public partial class DatabaseConnectivity : Org.IPFaces.Net.Pages.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

            using (SqlConnection con = ConnectionOpen())

            {

                try

                {

                    // Check the connection state

                    con.Open();

                    if (con.State == System.Data.ConnectionState.Open)

                    {

                        label1.Text = “Success. Connected to SQL server.”;

                        ConnectionClose(con);

                    }

                    else

                    {

                        label1.Text = “Failed. Not connected to SQL server.”;

                    }

                }

                catch (Exception)

                {

                    label1.Text = “Unknown exception.”;

                }

            }

        }

        private string ConnectionString()

        {

            // Create SQL Server 2005 connection string, change the credentials here

            StringBuilder strConn = new StringBuilder();

            strConn.Append(“Data Source = cygnus\\sql2005;”);

            strConn.Append(“uid = sa;”);

            strConn.Append(“pwd = goga;”);

            strConn.Append(“Initial Catalog = AdventureWorks”);

            return strConn.ToString();

        }

        private SqlConnection ConnectionOpen()

        {

            return new SqlConnection(ConnectionString().ToString());

        }

        private void ConnectionClose(SqlConnection xConnection)

        {

            xConnection.Close();

            xConnection.Dispose();

        }

        protected void Button1_Click(object sender, EventArgs e)

        {

            BuildList();

        }

        private void BuildList()

        {

            SqlDataAdapter sqlAdapter;

            DataSet sqlDataset;

            DataTable sqlTable = new DataTable();

            // Select SQL Query

            string _Query = “Select Top 100 FirstName From Person.Contact”;

            sqlAdapter = new SqlDataAdapter(_Query, ConnectionOpen());

            sqlDataset = new DataSet();

            sqlAdapter.Fill(sqlTable);

            for (int i = 0; i < sqlTable.Rows.Count; i++)

            {

                // Dynamicaly create label controls

                Label _label = new Label();

                _label.ID = “lblName0” + Convert.ToString(i);

                _label.Text = sqlTable.Rows[i][0].ToString();

                // Add newly created label control to Screens Control Collection

                screen1.Controls.Add(_label);

            }

            sqlAdapter.Dispose();

            sqlDataset.Dispose();

        }

    }

    }

 ******************************************************************

The Result:

 ******************************************************************

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s