how to serialize the object in .net

One need to serialize the data in case of passing data from/to webservice or making frequent operation on xml.

Here is the code to serialize the object

C#:
public static string GetSerializeData(object obj)
{
string output = String.Empty;
System.IO.StringWriter writer = new System.IO.StringWriter();
System.Xml.Serialization.XmlSerializer serializer = null;
serializer = new System.Xml.Serialization.XmlSerializer(obj.GetType());
serializer.Serialize(writer, obj);
output = writer.ToString();
writer.Close();
return output;
}

VB:
Public Function ToXML(ByVal entity As Object) As String
Dim output As String = String.Empty
Dim writer As New StringWriter
Dim serializer As XmlSerializer = Nothing
serializer = New XmlSerializer(entity.GetType)
serializer.Serialize(writer, entity)
output = writer.ToString
writer.Close()
Return output
End Function

In both the above case, pass your class object which you want to serialize and you’re done.

Happy Programming :-)

Use of Dynamic SqlQuery and its Optimization

Before writing this article I have many misconceptions for using the dynamic sql in sqlserver.
We are basically using the Execute statement for executing the dynamic query in sqlserver.
There are two options to execute the dynamic query
1) Execute @SqlQuery
2) Execute Sp_executesql @Sqlquery
In this article I will explore the Second option and how useful that option in Development.
Below is the example of Sp_executesql.

DECLARE @IntPatientID int;

DECLARE @SQLString nvarchar(500);

DECLARE @ParmDefinition nvarchar(500);

DECLARE @EncouterCount int;

SET @IntPatientID = 286;
SET @SQLString = N’Select @EncouterCnt=count(EncounterID) from Pat_Encounter where PatientID = @PatientID’;
SET @ParmDefinition = N’@PatientID int, @EncouterCnt int OUTPUT’;

EXECUTE sp_executesql @SQLString, @ParmDefinition, @PatientID = @IntPatientID, @EncouterCnt=@EncouterCount OUTPUT;

SELECT @EncouterCount;

In Execute option you will not get option of parameter you must define the variable in the dynamic sql to achieve the functionality.
In Sp_executesql you will get input and output parameter and that’s very useful in Development.
Another thing is Execute is not reuse Execution query plan else you are not define same value in condition of where.
Sp_executesql is reuse the Execution Plan and give fast output then Execute clause.
For the Optimization Sp_executesql is very handy in procedure.
You will get syntax from below link.

http://msdn.microsoft.com/en-us/library/ms188001(SQL.90).aspx

I hope this article is useful to you and enhance your knowledge of sqlserver.

Difference between object-oriented and procedural programming languages?

A major factor in the invention of Object-Oriented approach is to remove some of the flaws encountered with the procedural approach.

Object Orientation Languages (OOL) is concerned to develop an application based on real time while
Procedural Programing Languages (PPL) are more concerned with the processing of procedures and functions.

In OOL, more emphasis is given on data rather than procedures, while the programs are divided into Objects and the data is encapsulated (Hidden) from the external environment, providing more security to data which is not applicable or rather possible in PPL. In PPL, its possible to expose Data and/or variables to the external entities which is STRICTLY restricted IN OOL.

In OOL, the Objects communicate with each other via Functions while there is no communication in PPL rather its simply a passing values to the Arguments to the Functions and / or procedures.

OOL follows Bottom Up Approach of Program Execution while in PPL its Top Down approach.

OOL concepts includes Inheritance, Encapsulation and Data Abstraction, Late Binding, Polymorphism, Multithreading, and Message Passing while PPL is simply a programming in a traditional way of calling functions and returning values.

Here is the list of OOL languages :- JAVA, VB.NET, C#.NET

Here is the list of PPL languages :- C, VB, Perl, Basic, FORTRAN.

Shrink Log File (.LDF Files)

We all know the pain because of log files of sql databases. Uhh, it consumes so much of the space on hard disk;many time it grows like any thing.

Here is the one way to shrin Log file in sql 2005. You just need to chagne the database name with you database name (YOUR_DB_NAME with your DB in following script).

Hope this will help you to shrink your database without renaming the file and doing detach and all that.

(P.S This will shrink the ldf file upto 1 MB.. Sounds good na?)

================================
Print ‘Result before shrinking the log file’
=================

SELECT name AS ‘File Name’ , physical_name AS ‘Physical Name’, size/128 AS ‘Total Size in MB’,size/128.0 – CAST(FILEPROPERTY(name, ‘SpaceUsed’) AS int)/128.0 AS ‘Available Space In MB’–, * FROM sys.database_files;

USE YOUR_DB_NAME
GO
DBCC SHRINKFILE(‘YOUR_DB_NAME_log’, 1)
BACKUP LOG [YOUR_DB_NAME] WITH TRUNCATE_ONLY
DBCC SHRINKFILE(‘YOUR_DB_NAME_log’, 1)
GO

================================
Print ‘Result after shrinking the log file’
=================

SELECT name AS ‘File Name’ , physical_name AS ‘Physical Name’, size/128 AS ‘Total Size in MB’,size/128.0 – CAST(FILEPROPERTY(name, ‘SpaceUsed’) AS int)/128.0 AS ‘Available Space In MB’–, * FROM sys.database_files;

~ LUV Jigar Pandya ~~

USE of XML datatype, Read XML and Write XML in Sqlserver

ML data type is very useful in SQLserver 2005 and 2008.

Check the below script and learn how to use XML data type.

Create Table Patient(FirstName Varchar(20),LastName Varchar(20))
insert into Patient
values(‘Jack’,'Sparrow’)
insert into Patient
values(‘Peter’,'Parker’)
insert into Patient
values(‘John’,'Black’)

Below is the simple Feature of the XML.
You can set your table data in the XML type of variables
And read that.
Important feature is you can pass that XML type variables
To any procedure and you can read and process that data.
It’s like you pass table from one procedure to another procedure.

DECLARE @xmlDoc XML
SET @xmlDoc =
(
SELECT FirstName,LastName FROM Patient
FOR XML AUTO
)
SELECT @xmlDoc

Below code will fetch data from xml variable created above.

select
SelCat.CatDet.value(‘@FirstName’, ‘varchar(20)’) as FirstName,
SelCat.CatDet.value(‘@LastName ‘, ‘varchar(20)’) as LastName
from
@xmlDoc.nodes(‘Patient’) as SelCat(CatDet)

Below is the example of Write the rows to the XML TYPE
And you can read that as rows in any procedure.
This is useful when you have to pass block of rows
Without defining the table name.
Below is the example of that.

DECLARE @xmlDoc XML
SET @xmlDoc =
(
SELECT FirstName,LastName FROM Patient
FOR XML RAW
)

Below code will fetch data from xml variable created above.

SELECT
SelXML.xmlDetail.value(‘@FirstName’, ‘Varchar(20)’) AS FirstName,
SelXML.xmlDetail.value(‘@LastName’, ‘varchar(20)’) AS LastName
FROM
@xmlDoc.nodes(‘row’) AS SelXML(xmlDetail)
drop table Patient

I hope this post is useful for you and upgrade your knowledge.

Load/Unload css/JavaScript dynamically

In my project I need to load new css and unload existing css dynamically. So I have search on the net about it and I got one good link which solved my problem.

Add css dynamically

var headID = document.getElementsByTagName("head")[0];
var cssNode = document.createElement('link');
cssNode.type = 'text/css';
cssNode.rel = 'stylesheet';
cssNode.href = 'FireFox.css';
cssNode.media = 'screen';
headID.appendChild(cssNode);

Add JavaScript dynamically

var headID = document.getElementsByTagName("head")[0];
var newScript = document.createElement('script');
newScript.type = 'text/javascript';
newScript.src = 'http://www.somedomain.com/somescript.js';
headID.appendChild(newScript);

Unload Js or Css file

function removefile(filename, filetype) {
var targetElement = "script"; // in case of css this will be "link"
var targetAttr = "src"; // in case of css this will be "href"

var allCtrl = document.getElementsByTagName(targetElement);
for (var i=allCtrl.length; i>=0; i--)  { //search backwards within nodelist for matching elements to remove
if (allCtrl[i] && allCtrl[i].getAttribute(targetAttr)!=null &&
allCtrl[i].getAttribute(targetAttr).indexOf(filename)!=-1);
allCtrl[i].parentNode.removeChild(allCtrl[i]);
}
}

Replacing JavaScript or CSS file dynamically

The same way you can also replace existing css with new css by creating object of old and new css using dynamic css creation and unload function I have shown above and use “replaceChild” function of parentNode property.

Document Source:  http://www.javascriptkit.com/javatutors/loadjavascriptcss.shtml

Happy Scripting

~ With Love – Pranav ~

Difference between Implicit and Explicit Interface

There are 4 basic difference between Implicit and Explicit interface. To understand all four of them see the below example first:

There is a class C1 which implements two interfaces I1 and I2. and Both the class have method with same name i.e. sum().

public class C1 : I1, I2
{
      //Explicit Implementation of interface
      void I2.sum()
      {
      }

      //Implicit Implementation of interface
      public void sum()
      {
      }
}

public interface I1
{
      void sum();
}
public interface I2
{
      void sum();
}

The Interface I1 is implemented implicitly and I2 is explicitly. Below are the difference based on above example.

1)  In Implicit implementation, no need to specify Interface name with its method.
e.g. public void sum() { }
Where as in Explicit implementation, you need to specify Interface name with its method
e.g. void I2.sum() { }

2) In implicit, access specifier is required with method name and it must be public, even protected will not work.
Where as in Explicit, no access specifier will be required.

3) You can not make virtual method in explicit implemention and same will work in implicit implemention.

4) You can not make abstract method in explicit implemention and same will work in implicit implemention.

Practical Example

Now you wonder, by considering above scenario, what is the use of these two implementation because only Implicit implementation will also work for all type of method then why these two has been introduced by Microsoft. or what should be the practical scenario where we need to use either Implicit or Explicit.

Suppose you need to implement 2 interface and both interface will have method with same name. At that time you can not create one method in your class for both interfaces, rather you must implement one interface Implicit and one Explicit (or both Explicit) for showing two different method in your class. (Check above example).

Hiding Interface Details:

If you implement interface explicitly then it will not show in class Intellisense list. So, if you want to hide your method name to display when creating object of class then you need to go for Explicit interface implementation and in this case you need to create object of your interface to know the method name.

E.g.: In our above example if I want to invoke method of I2 interface:
I2 obj = new C1();
obj.sum();

Hope this helps you :-)