| Next Tip?
Home » Crystal Report

Difference between Clone, Close and Dispose methods of Crystal Report?

12 October 2009 2,118 views No Comment
1 Star2 Stars3 Stars4 Stars5 Stars (4 votes, average: 4.75 out of 5)
Loading ... Loading ...

Close() Vs Dispose Method
The basic difference between Close() and Dispose() is, when a Close() method is called, any managed resource can be temporarily closed and can be opened once again. It means that, with the same object the resource can be reopened or used. Where as Dispose() method permanently removes any resource ((un)managed) from memory for cleanup and the resource no longer exists for any further processing.
Example showing difference between Close() and Dispose() Method:
[Code]
using System;
using System.Data;
using System.Data.SqlClient;
public class Test
{
private string connString = “Data Source=COMP3;Initial Catalog=Northwind;User Id=sa;Password=pass”;
private SqlConnection connection;
public Test()
{
connection = new SqlConnection(connString);
}
private static void Main()
{
Test t = new Test();
t.ConnectionStatus();
Console.ReadLine();
}
public void ConnectionStatus()
{
try
{
if(connection.State == ConnectionState.Closed)
{
connection.Open();
Console.WriteLine(“Connection opened..”);
}
if(connection.State == ConnectionState.Open)
{
connection.Close();
Console.WriteLine(“Connection closed..”);
}
// connection.Dispose();
if(connection.State == ConnectionState.Closed)
{
connection.Open();
Console.WriteLine(“Connection again opened..”);
}
}
catch(SqlException ex)
{
Console.WriteLine(ex.Message+”\n”+ex.StackTrace);
}
catch(Exception ey)
{
Console.WriteLine(ey.Message+”\n”+ey.StackTrace);
}
finally
{
Console.WriteLine(“Connection closed and disposed..”);
connection.Dispose();
}
}
}
[/CODE]
In the above example if you uncomment the “connection.Dispose()” method and execute, you will get an exception as, “The ConnectionString property has not been initialized.”.This is the difference between Close() and Dispose().
Using Dataset.clone() creates new instance of the same object.
Using Dataset.copy() copies content of data to another object wihtout creating new instance.
Clone : Copy only structure
Copy : Copy Structure & Data Both

Close() Vs Dispose Method

The basic difference between Close() and Dispose() is, when a Close() method is called, any managed resource can be temporarily closed and can be opened once again. It means that, with the same object the resource can be reopened or used. Where as Dispose() method permanently removes any resource ((un)managed) from memory for cleanup and the resource no longer exists for any further processing.

Example showing difference between Close() and Dispose() Method:

using System;

using System.Data;

using System.Data.SqlClient;

public class Test

{

private string connString = “Data Source=COMP3;Initial Catalog=Northwind;User Id=sa;Password=pass”;

private SqlConnection connection;

public Test()

{

connection = new SqlConnection(connString);

}

private static void Main()

{

Test t = new Test();

t.ConnectionStatus();

Console.ReadLine();

}

public void ConnectionStatus()

{

try

{

if(connection.State == ConnectionState.Closed)

{

connection.Open();

Console.WriteLine(“Connection opened..”);

}

if(connection.State == ConnectionState.Open)

{

connection.Close();

Console.WriteLine(“Connection closed..”);

}

// connection.Dispose();

if(connection.State == ConnectionState.Closed)

{

connection.Open();

Console.WriteLine(“Connection again opened..”);

}

}

catch(SqlException ex)

{

Console.WriteLine(ex.Message+”\n”+ex.StackTrace);

}

catch(Exception ey)

{

Console.WriteLine(ey.Message+”\n”+ey.StackTrace);

}

finally

{

Console.WriteLine(“Connection closed and disposed..”);

connection.Dispose();

}

}

}

In the above example if you uncomment the “connection.Dispose()” method and execute, you will get an exception as, “The ConnectionString property has not been initialized.”.This is the difference between Close() and Dispose().

Using Dataset.clone() creates new instance of the same object.

Using Dataset.copy() copies content of data to another object wihtout creating new instance.

Clone : Copy only structure

Copy : Copy Structure & Data Both

Popularity: unranked [?]

Related interview questions

Leave your response!

You must be logged in to post a comment.