Click here to Skip to main content
Click here to Skip to main content

Smart Device Login Application Using the SQL Server CE Database

, 9 Jun 2009
Rate this:
Please Sign up or sign in to vote.
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):

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:

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.

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.

Figure 4: Add New Item Database File.

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.

Figure 6: Create New Table.

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:

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
          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.

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)

About the Author

saurabhmsoni
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

 
Questionwindows mobile smart device application PinmemberMember 93559715-Feb-13 19:41 
QuestionSmart Device Login Application Using the SQL Server CE Database Pinmemberedi zhu14-Oct-12 15:21 
QuestionHow to run on mobile Pinmembertheccdds30-May-12 23:57 
QuestionSmart Device Application....................... PinmemberKanakarajV31-Dec-11 2:00 
i am a new for this smart device device application.i created the small login form using below mentioned code
 

   SqlCeConnection conn = new SqlCeConnection(@"Data Source=" + @"D:\SampleDeviceApplication\SampleDeviceApplication\" + "Login.sdf; Persist Security Info=False;");
 
            private void button1_Click(object sender, EventArgs e)
            {              
                  conn.Open();
                  SqlCeCommand cmd = new SqlCeCommand("insert into Login values('" + textBox1.Text + "','" + textBox2.Text + "')", conn);
                  cmd.ExecuteNonQuery();
                  conn.Close();
                  cmd.Dispose();
                  conn.Dispose();
                  MessageBox.Show("Details Inserted Successfully");
                  textBox1.Text = ""; textBox2.Text = "";
                 
            }
 
            private void button2_Click(object sender, EventArgs e)
            {
                  Form2 f2 = new Form2();
                  f2.Show();
            }
 

 

 
but this code didn't run properly........... this code showing the following error...
 

The path is not valid. Check the directory for the database. [ Path = D:\SampleDeviceApplication\SampleDeviceApplication\Login.sdf ]
 

i am store the database in this path of
 
D:\SampleDeviceApplication\SampleDeviceApplication\Login.sdf
 

Any one help to this problem.......
QuestionThanks , A good article , I needed it Pinmembersamia ghareib20-Nov-11 1:40 
GeneralMy vote of 1 Pinmembershankar2008022814-Mar-11 19:59 
Questioni want to take this application further Pinmembernaijateddy7-Nov-10 4:21 
Generalsahar Pinmembermalakblueangel2-Aug-10 15:18 
Generalreally awesome artical god protect u :) Pinmemberai_elhakim7-Jun-10 16:59 
GeneralRe: really awesome artical god protect u :) [modified] Pinmemberai_elhakim7-Jun-10 18:32 
Generalplease help....:) Pinmembergrouchoo21-Apr-10 15:48 
GeneralRe: please help....:) Pinmembersaurabhmsoni21-Apr-10 16:18 
GeneralExcellent work...just what i wanted Pinmembergrouchoo21-Apr-10 15:46 
GeneralMy vote of 2 Pinmembershaveen9-Apr-10 19:21 
GeneralRe: My vote of 2 Pinmembersaurabhmsoni21-Apr-10 16:19 
GeneralNot seeing the user table adapter frame in the toolbox Pinmemberanuhoho12-Mar-10 23:21 
GeneralRe: Not seeing the user table adapter frame in the toolbox Pinmemberanuhoho13-Mar-10 18:21 
QuestionHow to connect Remote Database!! PinmemberVirani19-Jan-10 23:16 
AnswerRe: How to connect Remote Database!! Pinmembersaurabhmsoni30-Jan-10 18:26 
Questionhow to insert and delete data Pinmembersdastlan17-Dec-09 8:11 
AnswerRe: how to insert and delete data Pinmembersaurabhmsoni30-Jan-10 18:19 
GeneralYes, you can connect directly to an SQL Server database [modified] PinmemberEFEaglehouse7-Dec-09 17:00 
GeneralRe: Yes, you can connect directly to an SQL Server database Pinmembersaurabhmsoni9-Dec-09 19:09 
GeneralRe: Yes, you can connect directly to an SQL Server database PinmemberEFEaglehouse10-Dec-09 7:29 
GeneralThank you very much PinmemberDon Fizachi10-Sep-09 21:05 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 9 Jun 2009
Article Copyright 2009 by saurabhmsoni
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid