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

Friday 26 February 2010

Unable to add data connection. The DataConnectionDialog could not be initialized. - Resoved

When you try to add data connection (MS SQL Server) through the server explorer, sometimes you get the following error.


In order to resolve this you need to do a change in the windows registry.
Close visual studio IDE and open the registry editor and navigate to 'HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\8.0' and change it to 'HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\8.0Bak' (What ever you want).

Restart visual studio IDE.

Wednesday 15 July 2009

The type initializer for 'crystaldecisions.crystalreports.engine.reportdocument' threw an exception - Resolved

Today I got this error, when I tried to deploy an application developed using Visual Studio 2005 and Crystal Reports 10.

I googled the problem and found out many solutions which worked for many people. But only one solution worked for me. Anyway I will post all the things that I have tried.


Solution #1

Run ‘CRRedist2005_x86.msi’. This file can be found on ‘C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\BootStrapper\Packages\CrystalReports’. And if you have already installed this you will be prompted to Repair or Uninstall. But none is required if it’s already installed.

Solution #2

Uninstall the Crystal Reports for Visual Studio 2005 using the Visual Studio Instalation DVD and Reinstall it again.

Steps to uninstall the Crystal Reports for Visual Studio 2005

  1. Insert the Setup DVD

  2. Select ‘Change or Remove Visual Studio 2005’ (Then the setup will run on Maintenance Mode)

  3. Select ‘Add or Remove Features’

  4. Uncheck the ‘Crystal Reports For Visual Studio’ checkbox

  5. Click on ‘Update’ button

  6. Afterwards do the above steps and on step 4 select the ‘Crystal Reports For Visual Studio’ checkbox and click on ‘Update’ button


Solution #3

Create a setup using the Crystal Reports Merge Module, It can be downloaded from http://support.businessobjects.com/downloads/merge_modules.asp. And run it on the deployment machine


Solution #3 worked for me..

Hope this will be helpful to you …………

Thursday 21 May 2009

Get number of days in a month using SQL

Here is a sample to get the number of days in a month, for a given month and a year

declare @zMonth as varchar(2), @zYear as varchar(4)
set @zMonth = '02'
set @zYear = '2009'

select datediff(day,cast(('01-' + @zMonth + '-' + @zYear) as datetime),dateadd(day,-1,dateadd(month,1,cast(('01-' + @zMonth + '-' + @zYear) as datetime)))) + 1


**Please note: The date format of the machine that is running SQL should be 'dd/MM/yyyy'. Or please change the order in query prior using it. Or use the following Code. (Thanks to Rohan for pointing that out)
  
declare @zMonth as varchar(3), @zYear as varchar(4)
set @zMonth = 'Feb'
set @zYear = '2009'

select datediff(day,cast(('01-' + @zMonth + '-' + @zYear) as datetime),dateadd(day,-1,dateadd(month,1,cast(('01-' + @zMonth + '-' + @zYear) as datetime)))) + 1