Click here to Skip to main content
15,867,304 members
Articles / Web Development / ASP.NET

Smart Device Login Application Using the SQL Server CE Database

Rate me:
Please Sign up or sign in to vote.
3.91/5 (16 votes)
9 Jun 2009CPOL5 min read 146K   3K   30   51
A comprehensive guide to development of .NET 2.0 Smart Clients with SQL Server CE Database

Introduction

Smart devices should be connected to the database in order improve performance and facilities. Users generally find it a bit challenging when they want to implement smart device application with database support. Using this article, I want to provide a sample demo to get in touch with smart device application with database support using Microsoft Visual Studio 2008 and MSSQL Server CE database. This article has source code in C# .NET. I used a simple data base connection and created a login page so that user can easily understand the smart device application and database connection.

Using the Code

Open Microsoft Visual Studio 2008 and Select File -> New Project -> Smart device application under Visual C# project types (as shown in Figure 1):

Image 1

Figure 1: New Project Smart Device Application.

After creating a smart device application project, immediately a second popup window will appear and gives the user an option to select “Target Platform” and “.NET Compact version”.

Target Platforms types are:

  1. Pocket PC 2003
  2. Windows CE
  3. Windows Mobile 5.0 Pocket PC SDK
  4. Windows Mobile 5.0 Smart Phone SDK
  5. Windows Mobile 6 Professional SDK

If you want to use a different emulator from the above, then use the download link of pop up window.

.NET Compact Framework types are:

  1. .NET Compact Framework version 2.0
  2. .NET Compact Framework version 3.5

I am going with Device Application as shown in figure 2 below:

Image 2

Figure 2: Select Project Type Application.

Now, smart device projects itself and gives you a default Mobile Form named Form1.cs.
If you want to create more than one form in a project, then Right click on Project and
Add --> New Item --> choose Windows Form and give an appropriate name. Click on OK.

If you want to change the Mobile Form that has to run first, then you have to use program.cs file inside the project.

C#
//Note: Create a form object instance here to run first.
static void Main()
{
    Application.Run(new Form1());
} 

Now, you have to make a proper User Interface in the project. If you want to make a simple application, then make login form using .NET smart device project. First put two labels, two text boxes and two buttons on the form. Then, give them appropriate names via setting the ID in Property windows.

I have set these IDs for controls txtuname, txtpasssword, lbluname, lblpassword, btnsubmit and btnreset.

Note: In the login form, care should be taken about the password textbox, it should not display any character. Fill with * or any symbol into password char field of textbox.

Image 3

Figure 3: Smart Device Form in Design Mode.

The last step for setting up the project is to add SqlServerCE database file into the current project. Right click on project --> Add --> New Item --> Database File. Give an appropriate name to this database file and then click OK. It adds appropriate database file into the project, you need to set Data Source Configuration wizard. At the first time of creation, we don't have any tables and dataset, so click on finish to create an empty dataset.

Image 4

Figure 4: Add New Item Database File.

Image 5

Figure 5: Data Source Configuration Wizard.

Now, go to Server Explorer window, explore your database and create a new table. This can be done using right click on table folder and selecting “Create table” option. This opens a new “Table Creation Wizard” which prompts you to enter an appropriate table name, column names, types and size.

Image 6

Figure 6: Create New Table.

Image 7

Figure 7: Choose Data Connection.

My table Structure is as follows:

SQL
Table Name: user
uname      nvarchar(100)
password   nvarchar(100)  

Reset Function

C#
/* Reset Button Code */
      private void btnreset_Click(object sender, EventArgs e)
      {
          txtuname.Text = "";
          txtpassword.Text = "";
      }

Login Function

How to connect your mobile application with database

First double click on the dataset in the Solution Explorer that we have already created. Now, add one table adapter in that dataset using the right click --> Add --> Table Adapter.
It automatically takes existing dataset as a connection for table adapter. Click on Next and Select Command Type as Use SQL Statements and click on Next. Use query builder option and select table user to select all its rows and click on Next. Select all methods to use via table adapter and click on Next. It generates a message saying that your data table and table adapter is configured successfully.

It displays data table adapter as shown in the following figure:

Image 8

Figure 8: View of Table

Add this data table adapter into your form when you are using this. In our case, we have added userdatatableadpter1 into Form1.cs design mode. You cannot see this table adapter control in your toolbox until you build the smart device project. The first step is to build your project, after which you can see a frame created in your toolbox. Drag this new toolbox control in your project.

Go to the form code window and add “using System.Data.SqlServerCe;” namespace in your project.

And add the below mentioned code into your code window:

C#
//Login button click
private void btnlogin_Click(object sender, EventArgs e)
{
    AppDatabase1DataSet ds = new AppDatabase1DataSet();
    AppDatabase1DataSet.userDataTable dt = new
          AppDatabase1DataSet.userDataTable();
    this.userTableAdapter1.Fill(dt);
    foreach (DataRow dr in dt.Rows)
    {
        if (dr[0].ToString() == txtuname.Text && dr[1].ToString() == txtpassword.Text)
        {
            txtpassword.Text = "";
            txtpassword.Text = dr[0].ToString();
            MessageBox.Show("Login Valid!");
        }
    }
} 
How to Run?
  1. Click on F5 or click on start debugging.
  2. After building project, Visual Studio pops up one selection window for user to choose target device options.
  3. Select the device and click on ok. It takes some time to load your application into device emulator.
  4. Enter the username and password and click on login. If you want to clear username and password textboxes, then click on Reset button code.

Image 9

Figure 9: USA Windows Mobile 5.0 Pocket PC R2 Emulator.

Points of Interest

While implementing smart device applications using Visual Studio 2008 with the backend SQL Server CE database, I learnt by successfully handling lot of issues with SQL Server Database and smart device application. Finally, I came to the conclusion that SQL server compact edition is a reliable SQL Server database to connect with the smart device application. Most of the users are trying to connect smart device application to the existing Microsoft SQL Server database which is not possible. The limitation of Smart Device applications is that it is not possible to connect high concurrent database with Microsoft SQL Server 2005.

Thank you for reading this article. I would really appreciate your feedback.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Graduate Student
United States United States
My LinkedIN Profile: http://www.linkedin.com/in/saurabhmsoni

Web Master at SJSU- College of Business

Developed new website for College of Business, SJSU using Web Content Management System 7.0, JavaScript, HTML, WordPress and Google Application Tool Urchin. http://www.sjsu.edu/lucasschool/

Maintain College of Business present site and Faculty Staff Directory pages in updating necessary academic information using ASP, HTML, CSS, WordPress, and RSS. http://www.sjsu.edu/cob

Software Developer, Gateway Technolabs PVT, Limited, India

Developed and implemented Web Based Admin System (ASP.net (C#), web service, XML, SQL Server 2005 and Crystal Report) for Gateway Technolabs private limited company offshore project of Preemploy.com (USA's Company which is recognized as a national leader in pre-employment screening).

Implemented and tested functionality of advertisement campaigns, revenue generation, crystal reporting, broadcast factor, user management and role management (using C#.net, web service, SQL Server 2005, VSS 2005, Infragistic) in Desktop based CAB – MEDIA project for Gateway Technolabs private limited company.

Software Development Intern, Bharat Sanchar Nigam Limited, India

Developed Fraud Management, intranet site (using ASP, Oracle 8.0 and Visual Basic 6.0) for CMTS Department, BSNL, National Telecommunication Company, India.

Comments and Discussions

 
GeneralError : Failed to retrive data for this request Pin
RagsEshu14-Jun-09 19:20
RagsEshu14-Jun-09 19:20 
GeneralRe: Error : Failed to retrive data for this request Pin
codesoftconsult15-Jun-09 12:15
codesoftconsult15-Jun-09 12:15 
GeneralJust what I was looking for! Pin
thomasdrago14-Jun-09 10:16
thomasdrago14-Jun-09 10:16 
GeneralRe: Just what I was looking for! Pin
codesoftconsult15-Jun-09 12:16
codesoftconsult15-Jun-09 12:16 
Generalmy vote of 1 Pin
Tammam Koujan9-Jun-09 7:26
professionalTammam Koujan9-Jun-09 7:26 
GeneralRe: my vote of 1 Pin
saurabhmsoni9-Jun-09 8:24
saurabhmsoni9-Jun-09 8:24 
GeneralMy vote of 1 Pin
Tammam Koujan9-Jun-09 7:24
professionalTammam Koujan9-Jun-09 7:24 
GeneralRe: My vote of 1 Pin
saurabhmsoni9-Jun-09 7:57
saurabhmsoni9-Jun-09 7:57 
GeneralGreat article thanks Pin
bijulsoni8-Jun-09 15:25
bijulsoni8-Jun-09 15:25 
Thank you
Really nice article. Very informative and easy to understand. I was trying to achieve the same thing and your article provided all the information that I needed.

Thanks again. Smile | :)
GeneralRe: Great article thanks Pin
saurabhmsoni8-Jun-09 18:24
saurabhmsoni8-Jun-09 18:24 
GeneralNice Pin
nørdic8-Jun-09 11:00
nørdic8-Jun-09 11:00 
GeneralRe: Nice Pin
saurabhmsoni8-Jun-09 12:52
saurabhmsoni8-Jun-09 12:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.