Saturday, March 06, 2010

Web Service Vs WCF (Window Communication Foundation)

Web Service Vs WCF (Window Communication Foundation)

WCF is a set of .NET Framework-based technologies for building and running services. WCF is the next step in the evolutionary path that started with COM, COM+, and MSMQ, and continues through .NET Remoting, ASMX, System.Messaging, and .NET Enterprise Services. WCF combines these technologies and offers a unified programming model for developing services using any CLR-compliant language.


Feature

ASP.NET Web Service

WCF

Performance

Slower compared to WCF

The main benifit of the design of the DataContractSerializer is better performance over XML serialization

File Extension

File extention is asmx

File extension is .svc

Data Transformation

To and from Data Transition is done through XML Serializer

DataContractSerializer is used for data transition

Webmethods vs DataContract Attributes

ASP.NET WebService uses Webmethods to to translate .NET FW types in to XML.

The WCF uses the DataContractAttribute and DataMemeberAttribute to translate .NET FW types in to XML.

Limitations

Only Public fields or Properties of .NET types can be translated into XML.Only the classes which implement IEnumerable interface, ICollection interface can be serializable

Public/Private fields or properties of .NET types can be translated

IDictionary Interface class

Classes that implement the IDictionary interface, such as Hash table can not be serialized.

The DataContractSerializer can translate the Hash Table into XML. Hence using WCF we can even translate HashTable into XML

Know which fields /properties

XMLSerialization does not indicate the which fields or properties of the type are serialized into XML

DataCotratSerializer Explicitly shows the which fields or properties are serialized into XML

Exception handling

In ASP.NET Web services, Unhandled exceptions are returned to the client as SOAP faults.

In WCF Services, unhandled exceptions are not returned to clients as SOAP faults. A configuration setting is provided to have the unhandled exceptions returned to clients for the purpose of debugging.

Example

[WebService]
public class Service : System.Web.Services.WebService
{
[WebMethod]
public string Demo(string strDemog)
{
return strDemo;

}
}

[ServiceContract]
public interface ITest
{
[OperationContract]
string Demo (string strDemo);
}
public class Service : ITest
{
public string Demo(string strDemo)
{
return strDemo;
}
}


Learn more about WCF
http://www.xvpj.net/2008/03/08/wcf-step-by-step-tutorial/
http://www.15seconds.com/Issue/061130.htm

Wednesday, February 03, 2010

Multiple rows getting selected on mouse move when I am closing ContextMenuStrip by clicking datagridview row

Multiple rows getting selected on mouse move when I am closing ContextMenuStrip by clicking datagridview row

Note: Before looking solution for this issues you have to understand how to do drag and drop from one datagridview to another, See here http://riteshk.blogspot.com/2009/06/how-to-change-mouse-icon-when-drag_23.html

This issue is related to DataGridView DragDropEffects.Move,When you open sub menu from datagridview mouse down event and you have code to activate drag drop event too then on click of datagridview row, control in activating dagDropEffects.Move and when you move the mouse after closing the sub menu, mouse cousor having same effect (Move) and it will select multiple rows on the mouse move event.

Solution

To overcome this problem we have to ignore activating "DragDropEffects.Move" by putting some condition when sub menu get pops up

In the below code I have placed one boolean flag (isSubMenuClicked) and setting true when sub menu display otherwise its false

and check if "columnIndex > -1 and isSubMenuClicked = False" then only control should go and activate the datagridview dragdrop

DataGridView MouseDoubleClick not getting fire

Another problem here on the mouse double click "DataGridView1.DoDragDrop" should not get activate, other wise datagridview mouse double click will not get fire

Solution

To overcome this problem you have to check whether mouse button clicked once or twiced for that I have placed one condition in below code

its "If e.Clicks = 1 Then"

Source Code

Private Sub DataGridView1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseDown

Dim gridColumnIndex As Integer

'Get the Index of Row which is being Dragged

'We would use this Index on Drop to identify which Row was dragged and get the values from that row

If e.Button = Windows.Forms.MouseButtons.Left Then

gridColumnIndex = DataGridView1.HitTest(e.X, e.Y).RowIndex

'For Mouse Single Click Only

If e.Clicks = 1 Then

'If Grid have any row and sub menu not display

If gridColumnIndex > -1 And isSubMenuClicked = False Then

DataGridView1.DoDragDrop(gridColumnIndex, DragDropEffects.Move)

Else

DataGridView1.ClearSelection()

End If

End If

End If

Dim hitRowIndex As Integer

hitRowIndex = DataGridView1.HitTest(e.X, e.Y).RowIndex

'Open Sun Menu only on Mouse Right click and

'Avoid sub menu to display on datagridview header click

If (e.Button = Windows.Forms.MouseButtons.Right And hitRowIndex > -1) Then

ContextMenuStrip1.Show(DataGridView1, New Point(e.X, e.Y))

isSubMenuClicked = True

Else

isSubMenuClicked = False

End If

End Sub

Thursday, December 31, 2009

How to check which version of the dotnet framework is installed

1) Go To Start and click on Run
2) Type --> "%windir%\Microsoft.NET\Framework\"
You will see all the version installed folder name (Ex: v1.0.3705, v1.1.4322, v2.0.50727, v3.0, v3.5)

Friday, November 13, 2009

How to Display/Hide Crystal Report Viewer Tab and Status Bar at run time using VB.NET

How to Display/Hide Crystal Report Viewer Tab and Status Bar at run time using VB.NET


I am working with Visual Studio 2005 and faced some issues while working with crystal report, my requirement is to display and hide tab and status bar control from crystal report viewer, event tab control should display customized name in the tab text. I found the way to do this, below the code in vb.net

Display/Hide crystal report viewer tab control and rename the tab control


''' <summary>

''' This method will make tab controls visibile True/False

''' </summary>

''' <remarks>Ritesh</remarks>

Private Sub CrystalReportViewerTab(ByVal rptViewer As CrystalDecisions.Windows.Forms.CrystalReportViewer, ByVal isVisible As Boolean)

Dim reportTabs As TabControl = rptViewer.Controls(0).Controls(0)

'if tab visible

If isVisible Then
With reportTabs

.ItemSize = New Size(70, 18)

.SizeMode = TabSizeMode.FillToRight

.Appearance = TabAppearance.Normal

'Here you can give customize name to tab

.TabPages(0).Text = "My Tab Text"

End With

Else 'if tab hide

With reportTabs

.ItemSize = New Size(0, 1)

.SizeMode = TabSizeMode.Fixed

.Appearance = TabAppearance.Buttons

End With

End If

End Sub

Display/Hide crystal report viewer status bar


''' This method will make status bar visibile True/False

Public Sub CrystalReportViewerStatusBar(ByVal rptViewer As CrystalDecisions.Windows.Forms.CrystalReportViewer, ByVal isVisible As Boolean)

Dim ctl As Control

For Each ctl In rptViewer.Controls

If ctl.GetType().Name.ToString() = "StatusBar" Then

ctl.Visible = isVisible

End If

Next

End Sub

Wednesday, November 11, 2009

Datagridview CellDoubleClick event not getting fires with Datagridview drag drop functionality in VB.NET Window form

Differentiate between DataGridView Mousedown and CellDoubleclick events

OR

Datagridview CellDoubleClick event not getting fires with Datagridview drag drop functionality in VB.NET Window form


Problem


In VB.NET with Visual Studio 2005 I face a problem with working Datagridview drag and drop functionality. When I write a code to implement drag and drop functionality in Datagridview MouseDown event

Ex: DataGridView1.DoDragDrop(Index, DragDropEffects.Move)


And trying to fire CellDoubleClick event for same datagrid then this event is not getting fire, problem is when you do double click then first MouseDown event will fire then CelldoubleClick, but if control goes to MouseDown event then this line of code "DataGridView1.DoDragDrop(Index, DragDropEffects.Move)" canel CellDoubleClick event and CellDoubleClick event not getting fire.


Solution


To work with Both events togather we have to differenciate this by "Clicks" property in MouseDown event

Ex:

Private Sub DataGridView1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseDown


' 'Clicks' property can be used to solve this issue

If e.Clicks = 1 Then

' Write Code for Single Click

Else

'Write code for Double Click

End If

End Sub


Source code


So finally cell double and drag drop code should be like below


Private Sub DataGridView1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseDown


'Get the Index of Row which is being Dragged

'We would use this Index on Drop to identify which Row was dragged and get the values from that row

Dim Index As Integer

If e.Button = Windows.Forms.MouseButtons.Left Then

Index = DataGridView1.HitTest(e.X, e.Y).RowIndex

If e.Clicks = 1 Then ' For Single Click

If Index > -1 Then

'Pass the Index as "Data" argument of the DoDragDrop 'Function,

'Instead of Index as "Data" we can pass some data like 'array, object or images,

'That can be collected DataGridView2_DragOve event like

'e.Data.GetData(GetType(System.String))

DataGridView1.DoDragDrop(Index, DragDropEffects.Move)

End If

End If

End If

End Sub


Private Sub DataGridView1_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick

MessageBox.Show("DGV1_CellDoubleClick")

End Sub


Private Sub DataGridView2_DragOver(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DataGridView2.DragOver

e.Effect = DragDropEffects.Move

End Sub


Private Sub DataGridView2_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DataGridView2.DragDrop

'Write your code do operation in DataGridView2

End Sub

How to implements Drag Drop functionality with DataGridView : click here

Thursday, November 05, 2009

How to write SQL query to generate XML string, How to get XML as an output parameter from stored procedure

How to write SQL query to generate XML string, How to get XML as an output parameter from stored procedure

You can very well write SQL query to generate XML string or write stored procedure to give output as XML string

For Oracle Database
1) http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14259/xdb13gen.
htm#i1029599

For Microsoft SQL Server Database
1) http://msdn.microsoft.com/en-us/library/ms345137(SQL.90).aspx

Monday, November 02, 2009

Problem to display date in the MaskedTextBoxControl in dotnet

 

When you try to display date value to MaskedTextBox then it show different date in the field especially when you have single digit on the date. Even though MaskedTextBox control "Mask" property set it to "00/00/0000".

 

When you try to assign the date into this field like:

 

Problem

 

MaskTextBox.Text = "1/12/2009", it will display in the field like "11/22/009_"

 

Solution

 

To overcome this issue you have to send your date with proper format like below

 

MaskedTextBox.Text = Format(Convert.ToDateTime("1/12/2009 5:58:00 PM"), "MM/dd/yyyy")

OR

MaskedTextBox.Text = Format(Convert.ToDateTime(Date.Now), "MM/dd/yyyy")

 

Now it will display in the field as a correct date format "1/12/2009".

 


Tuesday, October 27, 2009

Parent node image getting selected when click on others child nodes in the TreeView

Parent node image getting selected when click on others child nodes in the TreeView
 
While working with tree view control having some images, I faced following issue.
My treeview control has some text value and image in the parent nodes and corresponding child nodes. I bind treeview control with imagelist control to display image in the treeview. Image got binded properly and working fine, BUT when I clicks on the child nodes or some other nodes, very first parent node image get replaced with existing node image.
Solution: while binding the image into treeview nodes use "SelectedImageIndex" property along with "ImageIndex" property with same image index number.
 
Example:
parentNode.ImageIndex = 0
parentNode.SelectedImageIndex = 0
 
VB.NET Source Code
 
Below the vb.net source code to bind image with the treeview controls
 
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
'Load the Imagelist with desired images
Dim customImageList As New ImageList
customImageList.Images.Add("AMandatory", Bitmap.FromFile("C:\AMandatory.ico"))
customImageList.Images.Add("BDiscretionary", Bitmap.FromFile("C:\BDiscretionary.ico"))
customImageList.Images.Add("AAMandatory", Bitmap.FromFile("C:\AAMandatory.ico"))
customImageList.Images.Add("BBDiscretionary", Bitmap.FromFile("C:\BBDiscretionary.ico"))
 
'Bind ImageList with TreeView
TreeView1.ImageList = customImageList
 
'Create a ParentNode
Dim rootNodeMandatory As TreeNode = TreeView1.Nodes.Add("Mandatory Text")
'Bind first image from imagelist
rootNodeMandatory.ImageIndex = 0
rootNodeMandatory.SelectedImageIndex = 0
 
'Create the child Node
Dim childNodeMandatory As TreeNode
childNodeMandatory = rootNodeMandatory.Nodes.Add("Mandatory Child Text")
'Bind third image from imagelist
childNodeMandatory.ImageIndex = 2
childNodeMandatory.SelectedImageIndex = 2
 
'Create another ParentNode
Dim rootNodeDiscretionary As TreeNode = TreeView1.Nodes.Add("Discretionary Text")
'bind second image from imagelist
rootNodeDiscretionary.ImageIndex = 1
rootNodeDiscretionary.SelectedImageIndex = 1
 
'Create the child Node
Dim childNodeDiscretionary As TreeNode
childNodeDiscretionary = rootNodeDiscretionary.Nodes.Add("Discretionary Child Text")
'Bind fourth image from imagelist
childNodeDiscretionary.ImageIndex = 3
childNodeDiscretionary.SelectedImageIndex = 3
 
End Sub

Thursday, October 15, 2009

Error: $Oracle.EXCEPTION_ORA_6532: or ORA-06532: Subscript outside of limit

Error: $Oracle.EXCEPTION_ORA_6532: or ORA-06532: Subscript outside of limit

This error occurs when I try to fill data into "String_Table" (table of varchar) with extend. In details; I was getting this error when I tried to fill value into "codeTable" (variable of "String_Table") event I am doing extend immediately.

i := 0;

codeTable (i) := "Some Value"

codeTable.extend;

Above code will throw "ORA-06532: Subscript outside of limit"

Solution is "String_Table" need to be start filling from 1 not 0, so Correct code is

i := 1;

codeTable (i) := "Some Value"

codeTable.extend;

After giving couple of hours I found this and solved by using above solution (By changing "i" value 0 to 1)

Following are the example I have tested with Oracle 11g or Oracle 10g too. These are very important and very useful stored procedure example described How to use XML type and String Array in Oracle stored procedure.

OBJECT-1 (STRING_TABLE)

Create a global package which includes Ref Cursor and String_Table to store value to create IN Clause value or others

CREATE OR REPLACE PACKAGE GLOBAL_PACKAGE AS

TYPE REF_CUR IS REF CURSOR;

TYPE "STRING_TABLE" AS TABLE OF varchar2 (100) INDEX BY BINARY_INTEGER;

END GLOBAL_PACKAGE;

OBJECT-3 (STORED PROCEDURE-1)

Below stored procedure is an example to take a value (String_Array) as an Input parameter to create In Clause value for select query and a Output parameter (REF_CUR) that will return record set (No of rows from employee table) of employee data.

PROCEDURE SELECT_DATA_FROM_EMP

(

prmIds IN Global_Package.STRING_TABLE,

prmOutCursor OUT Global_Package.REF_CUR

)

IS

codeTable string_table := string_table(NULL);

i Number;

BEGIN

FOR i IN 1..prmIds.count LOOP --Never start loop with 0

codeTable (i) := prmIds(i);

codeTable.extend;

END LOOP;

Open prmOutCursor for

SELECT *

FROM employee

WHERE empid IN (select column_value from table(codeTable));

END SELECT_DATA_FROM_EMP;

OBJECT-4 (STORED PROCEDURE-2)

Sample xml to send from code for stored procedure, this XML format you can change as per your requirement but you have to change the XML node path in the stored procedure ("XMLTYPE.EXTRACT")

<ArrayList>

<Values><Value>1000</Value></Values>

<Values><Value>2000</Value></Values>

<Values><Value>3000</Value></Values>

<Values><Value>4000</Value></Values>

</ArrayList>

Below stored procedure is an example to take a value (XMLTYPE) as an Input parameter to create In Clause value for select query and a Output parameter (REF_CUR) that will return record set (No of rows from employee table) of employee data.

CREATE OR REPLACE PROCEDURE SELECT_DATA_FROM_EMP

(

prmXmlCode IN XMLTYPE,

prmOutCursor OUT Global_Package.REF_CUR

)

AS

codeTable string_table := string_table(NULL);

i Number := 1; --Never initialized with 0

BEGIN

FOR vRow IN

(

SELECT

XMLTYPE.EXTRACT (VALUE (Code), '/Value/text()').getstringval() AS iCode

FROM TABLE

(XMLSEQUENCE (prmXmlCode.EXTRACT ('ArrayList/Values/Value'))) Code)

LOOP

codeTable (i) := vRow. iCode;

i := i + 1;

codeTable.extend;

END LOOP;

OPEN prmOutCursor FOR

SELECT * FROM EMPLOYEE

WHERE EMPID IN (select column_value from table(codeTable));

END SELECT_DATA_FROM_EMP;

Note: If you have more than one select query in a single stored procedure and you are sending XMLTYPE as an input parameter, instead of writing XMLTYPE.EXTRACT in each SQL select statement, it is better to do XMLTYPE.EXTRACT once and keep the value into "String_Table" and use "String_Table" for all your SQL select statement like "select column_value from table(codeTable));"