Skip to content
May 14 / Guruparan Giritharan

iPhone SQLite for Absolute Beginners

Hi this post is about creating a sqlite database in iPhone and manipulating it.
This is a tutorial for absolute beginners
As this is contains a lot of coding the code is not put in the blog but its fully commented and available for download.

The Code Demonstrates the following

  • Create a Sqlite Database
  • Add values to the database
  • Search for values in the database
  • Delete values from the database
  • Show all the values in a Picker
  • show all the values in a Table

Here are the UI’s of the project

You Can download the source code

HERE

Please leave a comment.

Mar 30 / Guruparan Giritharan

Connecting Android to SQL server via WCF using JSON Part 1

Hi readers I’ve started writing after a long gap

This post is all about connecting Android to SQl server

What we are going to do is a sample app that checks a login and Validate a user based on the provided Username and password

First thing we need to do is to set up the database

For the sample you’ll need this database

CREATE DATABASE JSONSampleDB;
USE JSONSampleDB
CREATE TABLE UserLogins
(
	UserName VARCHAR(100) PRIMARY KEY,
	Password VARCHAR(10)
);

after that we’ll need to start working on the WCF service which is the Bridge between the App and the SQL server

First you’ll have to create a WCF webservice

then if you notice the solution explorer you will be able to see this file structure

as you can see there is a CS file called “IService1.cs” and another file “Service1.svc”

The “IService1.cs” is the interface which is the skeleton for the class. if you open it you’ll find some method declarations as we wont be needing them you can remove them.What is important is the “[OperationContract]” only these ones will be shown to the others.
Now we’ll need to add a reference before we continue that is “System.ServiceModel.Web”
Now what we need is a method to validate the login. After adding the code it’ll look like this

   [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "checkLogin?name={name}&pass={pass}")]
        string checkLogin(string name,string pass);
    }

Now what is important is the Webinvoke this how we are saying how the method would be accessed via web and what type of data is passes. “WebMessageFormat.Json” is what is saying that the format is JSON you can change it to XML as well.Notice that the parameter names and the UriTemplate parameter names are equal, this is how the data is passed

then we have to implement the method in “Service1.svc.cs” here also you’ll see the old methods you can remove them as well and add this code

    public string checkLogin(string name, string pass)
        {
            DataAccess dataAccess = new DataAccess();
            return dataAccess.checkLogin(name, pass);
        }

The data access is a class for accessing the database, here’s the code for that

    public class DataAccess
    {
        SqlConnection con;
        public DataAccess()
        {
            con = new SqlConnection("Data Source=localhost;Initial Catalog=JSONSampleDB;Integrated Security=True");
        }

        public string checkLogin(string userName, string password)
        {
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            SqlCommand command = new SqlCommand("SELECT * FROM UserLogins where UserName='" + userName + "' AND Password='" + password + "'", con);
            SqlDataReader reader = command.ExecuteReader();

            if (reader.Read())
            {
                return "1";
            }
            else
            {
                return "0";
            }
        }
    }

Now the Methods are set but still we have a small task to be done that is modifying the webconfig file, here i’ll put only the changing parts the attached project will have the whole file.

Now you can run the Service and test it make sure you have some sample data in the database and access the method we created  by using the url.

Your port number may be different. replace the username and password with appropriate values

You’ll get 0 or 1 according to the information you have provided.

http://localhost:51220/Service1.svc/checkLogin?name=username&pass=password

You can download the WCF service project from here

http://www.mediafire.com/?b4q1opp1u4gia6a

The Android part will be covered in Part 2

Leave a comment

Guruparan Girithran

Mar 29 / Guruparan Giritharan

C# age function

Hi readers this is a function developed to get the age on a given date.
feel free to use it.

Inputs are Date of Birth and the date to compare

if you want to get the Age as of Today set the second argument as DateTime.Today.


        public static int calculateAge(DateTime DateOfBirth,DateTime DateAsOF)
        {
            int years = DateAsOF.Year - DateOfBirth.Year-1;

            if (DateAsOF.Month > DateOfBirth.Month)
            {
                years++;
            }
            else if (DateAsOF.Month == DateOfBirth.Month)
            {
                if (DateAsOF.Day>=DateOfBirth.Day)
                {
                    years++;
                }

            }
            return years;
        }
Sep 9 / Guruparan Giritharan

C++ Date Comparison Function

Hi Readers, This is a method coded by me in my first year to compare two date values in char array and give the comparison in integer.

cmpDate(char *date1, char *date2)
{
	//date1>date2=-1 date1=date2=0 date1IndexOf("/");
	int i2=dt1->LastIndexOf("/");
	int i3=dt2->IndexOf("/");
	int i4=dt2->LastIndexOf("/");
	int dt1month=System::Convert::ToInt32(dt1->Substring(0,i1));
	int dt1day=System::Convert::ToInt32(dt1->Substring(i1+1,i2-i1-1));
	int dt1year=System::Convert::ToInt32(dt1->Substring(i2+1));
	int dt2month=System::Convert::ToInt32(dt2->Substring(0,i3));
	int dt2day=System::Convert::ToInt32(dt2->Substring(i3+1,i4-i3-1));
	int dt2year=System::Convert::ToInt32(dt2->Substring(i4+1));
	if(dt1year>dt2year)
		return -1;
	else
	{
		if(dt1year==dt1year)
		{
			if(dt1month>dt2month)
				return -1;
			else if(dt1month==dt2month)
			{
				if(dt1day>dt2day)
				{
					return -1;
				}
				if(dt1day<dt2day)
					return 1;
			}
		}
	}
	return 1;
}
Jul 17 / Guruparan Giritharan

Insertion Sort Demonstrator

Hi This is a small application i made that will show the steps of the insertion sort algorithm. Thanks to Tharindu Edirisinge who pointed out the mistakes.


DOWNLOAD

Jun 26 / Guruparan Giritharan

Making a simple Camera Application with Touchless

Hi Readers

This post will discuss how to make a simple camera Application with Touchless SDK .

Touchless SDK can be used to do many things with your webcam this is just the basic thing.

First you’ll have to download the touchless SDK.
You can get it HERE.

You’ll get a zip file which will have the touchless demo , 2 DLL files and some other files the DLL files are the one which are important to us. Try that demo to know the power of touchless

Now create a new windows forms application . Then copy the two DLL files to the project folder and add the reference of touchless.dll file to the project.

Now create the form similar to this the names of the controls are given so that it’ll be easy to understand.

You’ll have to add a filesavedialog as well.

Now the coding the comments in the code provide the line by line explanation.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

//Importing the DLL
using TouchlessLib;

namespace CameraApp
{
    public partial class Form1 : Form
    {
        //The touchless manager has the properties of the camera info etc
        TouchlessMgr manager;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //initialize the touchless manager object
            manager = new TouchlessMgr();

            //Listing the available cameras in the combobox
            foreach (Camera item in manager.Cameras)
            {
                cmbCamera.Items.Add(item);
            }
        }

        private void cmbCamera_SelectedIndexChanged(object sender, EventArgs e)
        {
            //initializing the camera to be used based on the selection
            manager.CurrentCamera = (Camera)cmbCamera.SelectedItem;

            //Setting the Event handler for the camera
            manager.CurrentCamera.OnImageCaptured += new EventHandler(CurrentCamera_OnImageCaptured);

        }

        void CurrentCamera_OnImageCaptured(object sender, CameraEventArgs e)
        {
            //Giving the feed of the camera to the picturepox
            picFeed.Image = manager.CurrentCamera.GetCurrentImage();
        }

        private void btnCapture_Click(object sender, EventArgs e)
        {
            //Displaying the Current image in the other text box
            picPreview.Image = manager.CurrentCamera.GetCurrentImage();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            //getting the current image of preview picturebox
            Bitmap b = (Bitmap)picPreview.Image;

            //Dispalying the save file dialog
            saveFileDialog1.Filter = "JPEG Image|*.jpg";
            saveFileDialog1.Title = "Save an Image File";
            saveFileDialog1.ShowDialog();

            //Saving the image
            if (saveFileDialog1.FileName != "")
                b.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
            else
                MessageBox.Show("Nmae Not specified");
        }
    }
}

If you run the application and click the Capture button you’ll be able to see the application like this

You can download a sample project HERE

Please leave a Comment.