Click here to Skip to main content
15,063,944 members
Articles / Web Development / ASP.NET
Posted 8 Jun 2009


30 bookmarked

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
A comprehensive guide to development of .NET 2.0 Smart Clients with SQL Server CE Database


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.

//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:

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

Reset Function

/* 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:

//Login button click
private void btnlogin_Click(object sender, EventArgs e)
    AppDatabase1DataSet ds = new AppDatabase1DataSet();
    AppDatabase1DataSet.userDataTable dt = new
    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.


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


About the Author

Software Developer Graduate Student
United States United States
My LinkedIN Profile:

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.

Maintain College of Business present site and Faculty Staff Directory pages in updating necessary academic information using ASP, HTML, CSS, WordPress, and RSS.

Software Developer, Gateway Technolabs PVT, Limited, India

Developed and implemented Web Based Admin System ( (C#), web service, XML, SQL Server 2005 and Crystal Report) for Gateway Technolabs private limited company offshore project of (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, 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

Questionwindows mobbile application Pin
Member 115893217-May-15 0:08
MemberMember 115893217-May-15 0:08 
Questionwindows mobile smart device application Pin
Member 93559715-Feb-13 19:41
MemberMember 93559715-Feb-13 19:41 
QuestionSmart Device Login Application Using the SQL Server CE Database Pin
edi zhu14-Oct-12 15:21
Memberedi zhu14-Oct-12 15:21 
QuestionHow to run on mobile Pin
theccdds30-May-12 23:57
Membertheccdds30-May-12 23:57 
QuestionSmart Device Application....................... Pin
KanakarajV31-Dec-11 2:00
MemberKanakarajV31-Dec-11 2:00 
QuestionThanks , A good article , I needed it Pin
samia ghareib20-Nov-11 1:40
Membersamia ghareib20-Nov-11 1:40 
GeneralMy vote of 1 Pin
shankar2008022814-Mar-11 19:59
Membershankar2008022814-Mar-11 19:59 
Questioni want to take this application further Pin
naijateddy7-Nov-10 4:21
Membernaijateddy7-Nov-10 4:21 
Generalsahar Pin
malakblueangel2-Aug-10 15:18
Membermalakblueangel2-Aug-10 15:18 
Generalreally awesome artical god protect u :) Pin
Ashraf ELHakim7-Jun-10 16:59
MemberAshraf ELHakim7-Jun-10 16:59 
GeneralRe: really awesome artical god protect u :) [modified] Pin
Ashraf ELHakim7-Jun-10 18:32
MemberAshraf ELHakim7-Jun-10 18:32 
Generalplease help....:) Pin
grouchoo21-Apr-10 15:48
Membergrouchoo21-Apr-10 15:48 
GeneralRe: please help....:) Pin
saurabhmsoni21-Apr-10 16:18
Membersaurabhmsoni21-Apr-10 16:18 
GeneralExcellent work...just what i wanted Pin
grouchoo21-Apr-10 15:46
Membergrouchoo21-Apr-10 15:46 
GeneralMy vote of 2 Pin
shaveen9-Apr-10 19:21
Membershaveen9-Apr-10 19:21 
GeneralRe: My vote of 2 Pin
saurabhmsoni21-Apr-10 16:19
Membersaurabhmsoni21-Apr-10 16:19 
GeneralNot seeing the user table adapter frame in the toolbox Pin
anuhoho12-Mar-10 23:21
Memberanuhoho12-Mar-10 23:21 
GeneralRe: Not seeing the user table adapter frame in the toolbox Pin
anuhoho13-Mar-10 18:21
Memberanuhoho13-Mar-10 18:21 
QuestionHow to connect Remote Database!! Pin
Virani19-Jan-10 23:16
MemberVirani19-Jan-10 23:16 
AnswerRe: How to connect Remote Database!! Pin
saurabhmsoni30-Jan-10 18:26
Membersaurabhmsoni30-Jan-10 18:26 
Questionhow to insert and delete data Pin
sdastlan17-Dec-09 8:11
Membersdastlan17-Dec-09 8:11 
AnswerRe: how to insert and delete data Pin
saurabhmsoni30-Jan-10 18:19
Membersaurabhmsoni30-Jan-10 18:19 
GeneralYes, you can connect directly to an SQL Server database [modified] Pin
EFEaglehouse7-Dec-09 17:00
MemberEFEaglehouse7-Dec-09 17:00 
GeneralRe: Yes, you can connect directly to an SQL Server database Pin
saurabhmsoni9-Dec-09 19:09
Membersaurabhmsoni9-Dec-09 19:09 
GeneralRe: Yes, you can connect directly to an SQL Server database Pin
EFEaglehouse10-Dec-09 7:29
MemberEFEaglehouse10-Dec-09 7:29 

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.