Saturday, 25 September 2010

Removing Duplicate Records From a MS SQL Table – (MS SQL 2005 or above)

Have you ever been in a situation that your SQL tables contain duplicate records, where you have not defined a primary key or an auto increment field. And you need to keep one record and delete the rest.

The usual method of  doing this is to use a temporary table or to use a cursor. But there is another method of doing this using a single query in SQL 2005 or above.

To illustrate this first I will create the following table.

create table SampleTable(
id int not null,
name varchar(20) not null,
age int not null
)




Now I will insert some duplicate records to the above created table.



insert into SampleTable    (id,name,age) values (1,'John',30)
insert into SampleTable (id,name,age) values (1,'John',30)
insert into SampleTable (id,name,age) values (1,'John',30)
insert into SampleTable (id,name,age) values (1,'John',30)
insert into SampleTable (id,name,age) values (1,'John',30)
insert into SampleTable (id,name,age) values (2,'Mary',26)
insert into SampleTable (id,name,age) values (2,'Mary',26)
insert into SampleTable (id,name,age) values (2,'Mary',26)
insert into SampleTable (id,name,age) values (2,'Mary',26)
insert into SampleTable (id,name,age) values (3,'Ann',25)
insert into SampleTable (id,name,age) values (3,'Ann',25)
insert into SampleTable (id,name,age) values (3,'Ann',25)
insert into SampleTable (id,name,age) values (3,'Ann',25)
insert into SampleTable (id,name,age) values (3,'Ann',25)
insert into SampleTable (id,name,age) values (4,'James',21)



 



Using the below given query you can easily find out the duplicates (number of duplicate records).



select SUM(rec_count) as rec_count from(
select COUNT (*) - 1 as rec_count from SampleTable group by CHECKSUM(*)
)
T having COUNT(*) > 1


On the above query I have remove one record (COUNT (*) - 1), since one should be there as a valid record. And you really don’t need ‘having COUNT(*) > 1’, since non duplicate record count(*) will return 1 and count(*)-1 will be 0. It’s there for the ease of readability. So if you execute the above query you will get 11 records as the record count (Total 15 records, 4 valid records. So 15-4 = 11 records).



If you can see I have used ‘CHECKSUM(*)’. This to avoid typing all field names. Without using that the query would be like ‘group by id,name,age’.



And finally we can build the query to delete duplicates like this. First we must find the valid records, which should not be deleted. The way to do is using the function ‘ROW_NUMBER’. Using that we assign a unique row number for each record and select the maximum row number for each group. Then we will only get one record per group.



select MAX(row_num) from (
select ROW_NUMBER() over (order by checksum(*)) as row_num, CHECKSUM(*) as ChkSum
from SampleTable
) as T Group By ChkSum



And if you execute the above query you will get the following result:



Valid Records



It will return row numbers 5,9,14 and 15 as valid records which we must keep. And we must only delete records which the row number is not equal to the ones that’s been returned from the above mentioned query. First we’ll select those records (Only for checking purpose). You can select those records using the following query.



select T.* from(
select ROW_NUMBER() over (order by checksum(*)) as row_num, CHECKSUM(*)
as ChkSum from SampleTable) as T
where T.row_num not in (
select MAX(row_num) from (
select ROW_NUMBER() over (order by checksum(*)) as row_num, CHECKSUM(*)
as ChkSum from SampleTable
) as T Group By ChkSum
)



And if you execute the above query you will get the following result.



Duplicate Records



So if you see closely row numbers 5,9,14 and 15 are not there. So we can sure, that we are deleting the correct set of records. So in order to delete the duplicated we can use the following query.



    
delete T from(
select ROW_NUMBER() over (order by checksum(*)) as row_num, CHECKSUM(*)
as ChkSum from SampleTable) as T
where T.row_num not in (
select MAX(row_num) from (
select ROW_NUMBER() over (order by checksum(*)) as row_num, CHECKSUM(*)
as ChkSum from SampleTable
) as T Group By ChkSum
)



And if you query the table you will get the following result.



Result

Friday, 10 September 2010

The Null Coalescing Operator (??)

This operator can be extremely useful in situations where you have to check for the value of a variable and if it's null assign an empty string instead (or any other value).

Well you can achieve this using an if statement.

string zVar = somevalue;
if (somevalue == null) {
zVar = string.Empty;
}

 


Or you could use the ternary operator (?:)

string zVar = (somevalue != null) ? somevalue : string.Empty;

 


But we can do better using the null coalescing operator (??)

string zVar = somevalue ?? string.Empty;

Monday, 30 August 2010

Installing Android Lock Screen on iPhone 3GS – IOS4

Run “Cydia” on your iPhone

IMG_0075

Go to Manage

IMG_0076

Select Sources

IMG_0077

Select “Edit

IMG_0078

Select “Add

IMG_0079

Enter following url: http://sinfuliphonerepo.com

IMG_0080

Select “Done” and go to “SinfuL iPhone Repo

IMG_0081

Now select “AndroidLock XT Cracked

IMG_0083

Select Install and Confirm

IMG_0085

After installation go to “Settings” and setup the application

IMG_0089

 

** Please Note : If you forgot the pattern, do the following steps :

IMG_0091

Saturday, 21 August 2010

How to Add a Splash Screen to a WPF Application

Adding a Splash Screen to a WPF Application is really easy. For this I will be using VS 2010.

First prepare your image using an image editing application (Such as Photoshop). For this example I will be using the following image that I created using the MSPaint application.

Splash

 

Create a WPF application using VS 2010. And add your Splash Screen image to the project.

Add Existing

 

And change the build action of your image to “SplashScreen”.

Build Action

And when you run the application you can see the Splash Screen with a fading effect, before the Main Window.

But there are times that we want to keep the splash screen for more than the default time (300 Milliseconds). In order to do that, follow these steps.

Change the application’s build action from “Application Definition” to “Page”

Build Action 2

 

Create a constructor, and call the “InitializeComponent” method within that.

public App() {
InitializeComponent();
}







Write your own Main method. And call for the splash screen within that main method. And you can state the amount of time that you wish to keep your splash screen visible.



[STAThread]
public static void Main() {
SplashScreen sc = new SplashScreen("Splash.png");
sc.Show(true);
SplashScreenExample.App app = new SplashScreenExample.App();
sc.Close(TimeSpan.FromMilliseconds(4000));
app.Run(new MainWindow());
}


Now go to project properties and change the startup object from “Not Set” to your app class name (In this example its “SplashScreenExample.App”).


Startup Object 


Now when you run your application, you can see that the splash screen stays for 4 seconds. (Including the fading time)

Wednesday, 18 August 2010

Create table from existing table structure and insert data

Sometimes we might need to create a SQL table from an existing table and insert data from that particular table. So instead of creating the table separately using the ‘CREATE TABLE’ command, we can use the following statement.

Syntax:
select <FieldList> into new_table
from existing_table
<where condition>


Example:

select emp_id, emp_fname, emp_lname into emp_temp
from emp_master
where emp_state = 'NY'






** If  you created the table for temporary usage, make sure to drop it, after using it.

Monday, 9 August 2010

Create a Multiline TextBox Control using WPF [WPF – Multiline Text Box]

In windows applications we set the “Multiline” property to “true” in order to make a textbox multiline. But in WPF, there’s no property called “Multiline”. In order to make textbox multiline in WPF, you have to set three properties. That is :

  • TextWrapping=”Wrap”
  • VerticalScrollBarVisibility=”True”
  • AcceptsReturn=”True”

 

E.g

<TextBox Name="txtaddress" 
TextWrapping="Wrap"
VerticalScrollBarVisibility="Visible"
AcceptsReturn="True"/>







Saturday, 15 May 2010

Deploying windows applications using ClickOnce (Visual Studio 2008)

ClickOnce deployment allows you to publish Windows-based applications to a Web server or network file share for simplified installation. Visual Studio provides full support for publishing and updating applications deployed with ClickOnce. ClickOnce deployment is available for projects created with Visual Basic, Visual C#, and Visual J#, but not for Visual C++.
You can publish a ClickOnce application in three different ways: from a Web page, from a network file share, or from media such as a CD-ROM. A ClickOnce application can be installed on an end user's computer and run locally even when the computer is offline, or it can be run in an online-only mode without permanently installing anything on the end user's computer.

Open visual studio 2008 IDE and create a new windows application.
img_01_new_project

Go to Project –> <Application Name> Properties –> Publish
img_02_project_properties_publish

Publish Location
Publishing folder location can be either web site (http://www.mydomain.com/installation/), ftp (ftp://mydomain/installation) or file share. If you are using other than a file share to deployment, then you can provide a Installation Folder URL, since the end users may not have the permission to access the publishing folder directly. (Since I am using a shared path, installation folder URL will not be required)

Install Mode and Settings
If you select ‘The application is available online only’, each time it is run, it will be run from the published location and no start menu icon will be created.
But selecting ‘The application is available offline as well’, A shortcut on the Start menu will be created for the application and it will enable the application to be run when the user is disconnected from the network.

Publish Version
Here you should state the publish version (Not the application version). And if you check ‘Automatically increment revision with each publish’ will make sure that the revision number will get incremented automatically when you publish your application.

Application Files
image
Sometimes it is necessary to publish other files than the application. So this is where you state which ones to include or exclude from the deployment.

Prerequisites
img_05_prerequisites
You can create a setup file to install the prerequisites, which are required by your application in order to function correctly. And all required prerequisites are selected automatically. But if you want you can either add or remove them from your prerequisites setup.

Updates
img_04_application_update
In this window you should define how your application should be updated. Check ‘The application should check for updates’. And choose whether it should be updated before or after the application starts. If you choose to update it after running the application, you can mention whether you want to check each time it runs or can mention a time period, which it should check. But it is always advisable to check before application starts, so users will get the latest updates each time they run the application.
And mention a minimum required version, so that end user will get the mentioned version of the application, if they have an older version installed on their pc.
If the updating location is other than the publish location, please mention the path.

Save settings and go to the form designer and add a label to the form.
img_06_version1_form
Save and go to Build –> Publish <Application Name>
img_07a_publish_wizard_scr_1
If you wish do any changes to the publish settings which you completed earlier click next or click on the finish button.
img_07b_publish_wizard_scr_2
Click next
img_07c_publish_wizard_scr_3
Click next
img_07d_publish_wizard_scr_4
And the deployment setup will be created in the published path.
img_08_published_files
Run the setup.exe to install the application. And click on install button.
img_09_installing
img_10_version1_running

Now close the application and change the label to ‘Version 1.0.0.1’. And go to publish settings and on the update window change the minimum required version to 1.0.0.1.
img_04a_application_update
Click ok and save settings and publish the application.
There will be a start menu shortcut from the first installation.
img_11_startmenu_installation
Click on that to run the application. You will see an update screen and the updated application will be downloaded, installed and executed.
img_10a_version2_running
Above mentioned are the basic steps which is required to deploy a .Net windows application using ClickOnce.

For further information please click here

Sunday, 2 May 2010

Visual Studio 2010 and Smart Device Development - Disappointing

Visual Studio 2010 does not support mobile application development for versions of Windows Phone prior to Windows Phone OS 7.0.

For information about using Visual Studio 2010 to create applications for Windows Phone OS 7.0, see Windows Phone Development and Silverlight for Windows Phone.

For Visual Studio 2008 mobile developers, Microsoft released the Windows Mobile 6.5 Developer Tool Kit, which works with the Windows Mobile 6 SDK.

Because Visual Studio 2010 does not support mobile application development for Windows Phone prior to Windows Phone OS 7.0, you cannot use the following features: .NET Compact Framework projects, Visual C++ device projects, smart device CAB projects, Device Emulator and Device Emulator Manager, testing tools for device projects, and Device Security Manager.

Mobile application development is still supported in earlier versions of Visual Studio, such as Visual Studio 2008. For more information about smart device projects, see Windows Mobile Developer Center and Smart Device Development in Visual Studio 2008.

VMWare – Shutting down of guests take long time - Resolved

People who uses virtual machines, using vmware might have noticed that when shutting down the guest operating system, it takes hell of a time to shutdown.
This can be fixed by adding these to the .vmx file
prefvmx.minVmMemPct = "100"

mainMem.useNamedFile = "FALSE" 

mainMem.partialLazySave = "FALSE"

mainMem.partialLazyRestore = "FALSE"






Above mentioned filed is located, where the .vmdk file is located.

Wednesday, 28 April 2010

Problem with connecting to the SQL Server database on development machine from the Pocket PC Emulator using Visual Studio 2008 - Resolved

When developing application for Smart Devices, we usually debug it using the Emulators which was provided by the Visual Studio installation. And one of the difficulties that we come across is when we try to connect our Smart Device to the SQL Server which is in our development machine.
    ** For this example I will be using Visual Studio 2008 and SQL Server 2008
Here are the steps for resolving that issue:
1. First of all download and install the Virtual PC. Because this is required to set up the Virtual Machine Network Driver for Microsoft Device Emulator (http://go.microsoft.com/fwlink/?linkid=46859). The installation process is a pretty straight forward one and make sure that the Visual Studio IDE or the Smart Device Emulator is not running.

2. From network settings, right click on the Local Area Network (Network Connection) and select Properties and make sure that the Virtual Machine Network Services option is checked
img_01_network_properties

3. For this I will be using the sample “Adventure Works Database”, and it can be downloaded from the CodePlex community site (http://msftdbprodsamples.codeplex.com/releases/view/37109)

4. Open Visual Studio IDE. Create a new Smart Device project. Use any name (Here I have used SampleSmartDeviceApplication) and select required framework from the list (I have chosen 3.5)
img_02_new_project_visual_studio_2008

5. Choose the target platform, .NET Compact Framework and the Template from the next screen. I have chosen “Pocket PC 2003”, “.Net Compact Framework Version 2.0” and “Device Application”.
img_03_target_platform_framework


6. In Visual Studio IDE open Tools—>Options. And from the window select Device Tools—>Devices and choose your device (I have chosen Pocket PC 2003 SE Emulator)
img_05_device_tools_options

7. Click on the properties button and from the device properties window, select “TCP Connect Transport” as the Transport and click on the Configure… button.
img_06_smart_device_emulator_properties
8. And use select the option “Use specific IP address” and type an appropriate IP address. (I have typed 192.168.0.190 since my development machine IP address is 192.168.0.198)
img_07_configure_tcp_ip_transport

9. Click ok and close the TCP/IP Transport window. Click on the Emulator button. And from the Emulator Properties window select the Network tab and check on the “Enable NE2000 PCMCIA network adapter and bind to” and select your network adapter.
img_08_configure_emulator_properties
10. Click the ok button and close the window. And click on the ok button and close the Smart Device Emulator properties window. Click on the OK button and close the Options window.
11. Add a button (button1) and a Grid (dataGrid1) to the form. And add the “System.Data.SqlClient” reference to the project.
img_04a_controls_added_to_form
12. Double click on the button and add the following code.


    string zCon = "Password=blogpassword;Persist Security Info=True;
    User ID=bloguser;Initial Catalog=AdventureWorks;
    Data Source=192.168.0.198\\sql2k8";
    SqlConnection objCon = new SqlConnection(zCon);
    objCon.Open();
    SqlCommand objCommand = new SqlCommand("select ProductID,Name,ProductNumber from Production.Product", objCon);
    DataSet dsData = new DataSet();
    SqlDataAdapter da = new SqlDataAdapter(objCommand);
    da.Fill(dsData);
    dataGrid1.DataSource = dsData.Tables[0];
    objCon.Close();



13. Run the application and select Pocket PC 2003 SE Emulator and click on deploy.


img_04b_run


14. Click on the “button1”. Then you get the following results.


img_04b_run_a