Saturday, March 21, 2009

How to Create Charts in Asp.Net 2.0 using Microsoft Chart .Net 3.5

Hi,

Today, I would like to share my view on how to create chart in Asp.net 2.0. As we all know traditionally microsoft asp.net didnt had support for Charts from its own factory. But, after Microsoft Took over Dundas chart, Microsoft is providing the Drag and Drop Chart functionality for visual studio 2008 or .Net framework 3.5.

To start with, You will need to download the Following  :

Microsoft .Net Framework 3.5

Microsoft .Net Framework 3.5 SP1

Microsoft Chart for .Net 3.5

You will need to install them in the order mentioned. Because the Chart control is a part of .Net Framework 3.5 SP1.

Once the installation is done, you are ready to create stunning Charts in Asp.net without using any third party tool.

Step 1: Create a New Website in Visual Studio 2005.

Step 2: Look for a Chart Control in the Toolbox. Drag and drop it to proceed. (if you are not able to find chart control, right click toolbox Add tab, give it a name Chart. Right click Chart Tab –> Add Items, Browse to Program Files –> Microsoft Chart controls –> Assemblies –> System.Web.DataVisualization.dll. Once you add this dll, you will have chart control in your toolbox)

Step 3: Add SqlDataSource and configure it to Northwind database and the simple query as shown in the image below.

datasource

Step 4: Click on the Smart tag of Chart control and select SqlDataSource1 for the Datasource.

Step 5: Now lets do some basic configuration. there are basically 2 important properties you need to know to start creating Charts in Asp.net 2.0

1) ChartAreas: Click the (…) button next to ChartAreas in property window of Chart1. Change the Property values related to Appearance of the chart like its background color, to suit your website color themes.

->  Locate the Axis Property in the ChartArea Collection Editor, click on the (…) button to open the Axis collection editor, Select X Axis from the left pane, and select Title property from the right. Type in ‘Countries’ in Title property

-> Select Y (value) Axis from left pane and select Title from right pane, enter ‘Count’ in Title Property. Click OK to clost the window.

-> Select Axis Property from left pane, find X Axis, LabelStyle Section, Modify the Angle Property to –90 and Interval to 1, This is required to display all the values in X axis.

2) Series: Select the Chart control, from properties, select Series property and click on (…) button to open up the Series Collection Editor.

-> In the left pane Series1 will be selected by default, now select X Value Member property from right pane, and select the appropriate column name coming from database. In our case select ‘country’.

-> Again, Select Y Value Member property in right pane and select ‘Count’ column.

That is it, We are ready to display the chart, before we execute it, let me tell you about the 3D view of the Chart. Select the ChartAreas Property collection, Look for Area3DStyle in the right pane, expand it by clicking on the + sign and set (Enable3D) to True. This will help us to display the chart in 3D angle.

This is how it look likes when you run it.

column

 

And ya, to change the chart to any other format you like just follow simple step,

-> Select Series collection from property window, Select different chart type from ChartType property, see the image below to get better idea.

charttype

 

Below are the few types of chart we can create using Microsoft Chart control.

Bubble

bubble

 

Doughnut

doughnut

Pyramid

pyramid

I hope i have done justice to this article, my next article will discuss about some common errors you may face as a beginner to Chart control.

Do comment if you have any doubts.

Happy programming

Source : Here

Read more!

Friday, March 13, 2009

Solution to The conversion of a char data type to a datetime data type resulted in an out-of-range datetime

Hi,

I would like to share my opinion on one of the problems i faced while working with datetime format in Asp.net 2.0 and sql server 2000 on windows server 2003.

I encountered an error 'The conversion of a char data type to a datetime data type resulted in an out-of-range datetime'. while trying to save a date to the datetime column in database.

The Reason : Sql server supports the datetime format depending upon the regional datetime setting of the database server. so if your development server supposet MM/dd/yyyy , not necessarily your production or testing database server will support the same format. As a result you might have done coding to save it in a particular format & the same code may not work on another server.

Solution : After trying various things i came to a conclusion the best way to handle this error is to have precautionary steps while dealing with them.

Consider a case, where you just want to store the date.. without timestamp. as sql server doesnt have any specific date format you need to save the date in datetime dtatype column in database.
You can first create a stored procedure with the insert statement

CREATE PROCEDURE [dbo].[spInsert_]
@dt datetime
AS
BEGIN
Insert into IDtable (dt) values (@dt);
GO
END

Now call the above stored procedure from front end asp.net 2.0 code.

Dim dt As DateTime = DateTime.Now.ToString("yyyy-MM-dd")
MyCmd.Connection = MyCon
MyCmd.CommandType = Data.CommandType.StoredProcedure
MyCmd.CommandText = "spInsert_"
MyCmd.Parameters.Add("@dt", SqlDbType.DateTime)
MyCmd.Parameters("@dt").Value = dt
MyCmd.Transaction = trans
Dim res As Integer = MyCmd.ExecuteNonQuery()

Here, I would like to point out, the universal format supported by sql server is yyyy-MM-dd hh:nn:ss where nn is minutes...

so if you make a practice of storing your data in the said format, you will never encounter this error...

second important thing is to use Store Procedure with parameters, you can just pass the date value as parameter to store procedure, it will do the necessary casting.

Other solution :
SET DATEFORMAT dmy
before executing any sql statement. (i havent tried this one)

Source : http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataproviders/thread/095eb7a7-222a-4a15-8301-2a2acca2ca3f/

if you find my article useful, do leave a comment.

Cheers,
happy programming
Idu

Add to Technorati Favorites Read more!

Wednesday, March 4, 2009

error The type initializer for 'CrystalDecisions.CrystalReports.Engine.ReportDocument' while asp.net 2.0, crystal report 10 deployment

Hi,

I have developed a web application using Asp.net 2.0, Crystal report 10, Ajax, Sql server 2k.
Crystal report work fine on development machine. but once i tried to deploy it in production environment ( windows server 2003, iis 6.0).

I came across following error while executing the Crystal report on production server

error The type initializer for 'CrystalDecisions.CrystalReports.Engine.ReportDocument'.

after googling it for quite some time.. i came across one of the post on msdn forum.

The solution is :

Open the asp.net 2.0 web application to be deployed in visual studio 2005

Add New project -> select the Setup and deployment from left pane and WebSetup from right pane.

click on the setup project and then click on properties.

From there click on the prerequisite button.

Check Crystal Reports for .Net Framework 2. (and other necessary dependencies like MDAC 2.8)

I then selected download prerequsites from the same location as my application.

Rebuild the setup project.

Go to the WebSetup Project folder, you will find CRRedist2005_x86.msi file under CrystalReport Folder (under Release or Debug folder, which ever u selected prior to building the websetup) .

I ran this CRRedist2005_x86.msi file on the Production machine and it worked like charm

I hope it helps.
Cheers
Happy Programming
Source : msdn.microsoft.com

Add to Technorati Favorites Read more!