Tuesday, September 28, 2004
How to prevent memory leak in C#.NET?
If you are letting .NET to manage memory, it will do a good job of memory management. If you start messing with the GC then you get into big trouble.
.NET's GC does not collect items right away, it lingers around in case the block is needed somewhere else. It collects when it thinks it should.
Don't mess with memory management unless you have a really, really good reason to. The Dispose method is used to release unmanaged resources that aren't handled by the garbage collector, like some streams and graphics operations. If you are using these, then you'll want to call the Dispose method when you're done with the object. The object itself will remain in memory until it's collected, but the unmanaged resources associated with it will be freed.
Note: For most streams, the Dispose method will probably be replaced by the Close method, which does the same thing.
With Best Regards,
Mitesh Mehta
Email : miteshvmehta@gmail.com
http://cc.1asphost.com/miteshvmehta/
Delegate: A neat example!
Delegate: A neat example!
Suppose your daughter is grounded: she's not allowed to use the phone.
If she picks up the phone, both mom and dad want to know about it.
Slap this code on her and she could wind up in big trouble!
using System;
namespace Grounded {
class Mother {
public void subscribe() {
Console.WriteLine("Mother Subscribing");
Daughter.onPhoneCall += new Daughter.OnPhoneCallHandler(Notify);
}
public void Unsubscribe() {
Console.WriteLine("Mother Unsubscribing");
Daughter.onPhoneCall -= new Daughter.OnPhoneCallHandler(Notify);
}
public void Notify() {
Console.WriteLine("Mother Notified");
}
}
class Father {
public void subscribe() {
Console.WriteLine("Father Subscribing");
Daughter.onPhoneCall += new Daughter.OnPhoneCallHandler(Notify);
}
public void Unsubscribe() {
Console.WriteLine("Father Unsubscribing");
Daughter.onPhoneCall -= new Daughter.OnPhoneCallHandler(Notify);
}
public void Notify() {
Console.WriteLine("Father Notified");
}
}
class Daughter {
public delegate void OnPhoneCallHandler();
public static event OnPhoneCallHandler onPhoneCall;
public void PlaceCall() {
Console.WriteLine("Daughter placed call");
if(onPhoneCall != null) {
onPhoneCall();
}
}
}
class Class1 {
static void Main(string[] args) {
Father f = new Father();
Mother m = new Mother();
Daughter d = new Daughter();
m.subscribe();
f.subscribe();
d.PlaceCall();
m.Unsubscribe();
f.Unsubscribe();
d.PlaceCall();
}
}
}
Suppose your daughter is grounded: she's not allowed to use the phone.
If she picks up the phone, both mom and dad want to know about it.
Slap this code on her and she could wind up in big trouble!
using System;
namespace Grounded {
class Mother {
public void subscribe() {
Console.WriteLine("Mother Subscribing");
Daughter.onPhoneCall += new Daughter.OnPhoneCallHandler(Notify);
}
public void Unsubscribe() {
Console.WriteLine("Mother Unsubscribing");
Daughter.onPhoneCall -= new Daughter.OnPhoneCallHandler(Notify);
}
public void Notify() {
Console.WriteLine("Mother Notified");
}
}
class Father {
public void subscribe() {
Console.WriteLine("Father Subscribing");
Daughter.onPhoneCall += new Daughter.OnPhoneCallHandler(Notify);
}
public void Unsubscribe() {
Console.WriteLine("Father Unsubscribing");
Daughter.onPhoneCall -= new Daughter.OnPhoneCallHandler(Notify);
}
public void Notify() {
Console.WriteLine("Father Notified");
}
}
class Daughter {
public delegate void OnPhoneCallHandler();
public static event OnPhoneCallHandler onPhoneCall;
public void PlaceCall() {
Console.WriteLine("Daughter placed call");
if(onPhoneCall != null) {
onPhoneCall();
}
}
}
class Class1 {
static void Main(string[] args) {
Father f = new Father();
Mother m = new Mother();
Daughter d = new Daughter();
m.subscribe();
f.subscribe();
d.PlaceCall();
m.Unsubscribe();
f.Unsubscribe();
d.PlaceCall();
}
}
}
Tuesday, September 21, 2004
Xcopy Deployment
Xcopy command is used to easily copy all the files of a .Net application to
a target installation directory... For applications that do not require any
further registration Xcopy is command is the simplest way to distribute your
.Net applications...
Do note that Xcopy command does not copy the hidden and system files... Also
note that Xcopy creates files with the archive attribute set, whether or not
this attribute was set in the source file...If you have a disk that contains
files in subdirectories and you want to copy it to a disk that has a
different format, you should use the Xcopy command instead of Diskcopy.
Since the Diskcopy command copies disks track by track, it requires that
your source and destination disks have the same format. Xcopy has no such
requirement. In general, use Xcopy unless you need a complete disk image
copy or you wish to copy the system files.
With Best Regards,
Mitesh Mehta
Email : miteshvmehta@gmail.com
http://cc.1asphost.com/miteshvmehta/
a target installation directory... For applications that do not require any
further registration Xcopy is command is the simplest way to distribute your
.Net applications...
Do note that Xcopy command does not copy the hidden and system files... Also
note that Xcopy creates files with the archive attribute set, whether or not
this attribute was set in the source file...If you have a disk that contains
files in subdirectories and you want to copy it to a disk that has a
different format, you should use the Xcopy command instead of Diskcopy.
Since the Diskcopy command copies disks track by track, it requires that
your source and destination disks have the same format. Xcopy has no such
requirement. In general, use Xcopy unless you need a complete disk image
copy or you wish to copy the system files.
With Best Regards,
Mitesh Mehta
Email : miteshvmehta@gmail.com
http://cc.1asphost.com/miteshvmehta/
App Config File not being picked up
Some times you would have done nearly everything right in the windows
application but still on trying to access configuration file you would get
back null or nothing... Well the problem might be the name that you have
used for the config file...
You should know that for a windows based application to pick up app.config
file following should be taken care of...
1.) The app.config name should match your assembly name... i.e. if you have
myassembly.dll then the config file name should be myassembly.config...
2.) Also you should know that the config file is picked up for the main or
the application which is the starting point... So your app.config file
should be attached to that...
With Best Regards,
Mitesh Mehta
Email : miteshvmehta@gmail.com
http://cc.1asphost.com/miteshvmehta/
application but still on trying to access configuration file you would get
back null or nothing... Well the problem might be the name that you have
used for the config file...
You should know that for a windows based application to pick up app.config
file following should be taken care of...
1.) The app.config name should match your assembly name... i.e. if you have
myassembly.dll then the config file name should be myassembly.config...
2.) Also you should know that the config file is picked up for the main or
the application which is the starting point... So your app.config file
should be attached to that...
With Best Regards,
Mitesh Mehta
Email : miteshvmehta@gmail.com
http://cc.1asphost.com/miteshvmehta/
When using the SqlDataReader, how can I tell how many records where found?
When using the SqlDataReader, how can I tell how many records where found?
The SqlDataReader provides a means of reading a "forward-only" stream of rows from a SQL Server database. Hence the number of rows read from the database is not known until the last row is read.
This is the same type of problem we had back in classic ADO where the Recordset's RecordCount was always -1 for a server-side, forward-only cursor. The solution in classic ADO was to use a static or keyset cursor. Or use a client-side cursor location, which always used a static cursor. However in ADO.NET 1.0, there is no such thing as a server-side, static or keyset cursor. There is only the server-side, forward-only DataReader. Or there is the client-side, static DataSet (in classic ADO "terms").
You can work-around this issue by either
1) Increment a counter while looping throuh all of the rows, or
2) use two SELECT statements in your query. The first one returns the row count and the second one returns the actual rows. e.g. "SELECT COUNT(*) FROM myTableName; SELECT * FROM myTableName". Then you can use the NextResult method to move from the first to the second result set, or
3) Or use a DataSet instead of the SqlDataReader. Then use the DataSet's Table Rows count. e.g. oDataSet.Tables("Products").Rows.Count
For more information, see: http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q308050
#) Best Practices for Using ADO.NET
Learn about the best solutions for implementing and achieving optimal performance, scalability, and functionality in Microsoft ADO.NET applications
* Information about the .NET Framework data providers included with the .NET Framework.
* Comparisons between the DataSet and the DataReader, and an explanation of the best use for each of these objects.
* An explanation on how to use the DataSet, Commands, and Connections.
* Information about integrating with XML.
* General tips and issues.
more here..http://msdn.microsoft.com/library/defaultasp?url=/library/en-us/dnadonet/html/adonetbest.asp
#) What is the difference between a Recordset and Dataset?
A Recordset is a single set of data from a database, whereas a DataSet is a relational set of data. ADO.NET allows for DataSets to be populated from the database, but DataSets are not exclusively an ADO.NET construct. A single dataset may house related, but distinct sets of data. For example, a dataset may contain a table of information about a patient (think a patient recordset) and also a related table of all the medications the patient is taking. In ADO, you would need to treat these as different recordsets. In ADO.NET this is remarkably different. DataSets are also inherited as disconnected.
With Best Regards,
Mitesh Mehta
Email : miteshvmehta@gmail.com
http://cc.1asphost.com/miteshvmehta/
The SqlDataReader provides a means of reading a "forward-only" stream of rows from a SQL Server database. Hence the number of rows read from the database is not known until the last row is read.
This is the same type of problem we had back in classic ADO where the Recordset's RecordCount was always -1 for a server-side, forward-only cursor. The solution in classic ADO was to use a static or keyset cursor. Or use a client-side cursor location, which always used a static cursor. However in ADO.NET 1.0, there is no such thing as a server-side, static or keyset cursor. There is only the server-side, forward-only DataReader. Or there is the client-side, static DataSet (in classic ADO "terms").
You can work-around this issue by either
1) Increment a counter while looping throuh all of the rows, or
2) use two SELECT statements in your query. The first one returns the row count and the second one returns the actual rows. e.g. "SELECT COUNT(*) FROM myTableName; SELECT * FROM myTableName". Then you can use the NextResult method to move from the first to the second result set, or
3) Or use a DataSet instead of the SqlDataReader. Then use the DataSet's Table Rows count. e.g. oDataSet.Tables("Products").Rows.Count
For more information, see: http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q308050
#) Best Practices for Using ADO.NET
Learn about the best solutions for implementing and achieving optimal performance, scalability, and functionality in Microsoft ADO.NET applications
* Information about the .NET Framework data providers included with the .NET Framework.
* Comparisons between the DataSet and the DataReader, and an explanation of the best use for each of these objects.
* An explanation on how to use the DataSet, Commands, and Connections.
* Information about integrating with XML.
* General tips and issues.
more here..http://msdn.microsoft.com/library/defaultasp?url=/library/en-us/dnadonet/html/adonetbest.asp
#) What is the difference between a Recordset and Dataset?
A Recordset is a single set of data from a database, whereas a DataSet is a relational set of data. ADO.NET allows for DataSets to be populated from the database, but DataSets are not exclusively an ADO.NET construct. A single dataset may house related, but distinct sets of data. For example, a dataset may contain a table of information about a patient (think a patient recordset) and also a related table of all the medications the patient is taking. In ADO, you would need to treat these as different recordsets. In ADO.NET this is remarkably different. DataSets are also inherited as disconnected.
With Best Regards,
Mitesh Mehta
Email : miteshvmehta@gmail.com
http://cc.1asphost.com/miteshvmehta/
Obtaining installation state of Visual Studio .NET programmaticall
The following article describes how to obtain the installation state of Visual
Studio .NET programmatically.
Typically, you can examine a file or a registry entry to determine
whether you have a version of Microsoft Visual Studio .NET, or of any
program, already installed on your computer. You can also determine
the version information programmatically by using Microsoft Windows
Installer functions such as the MsiQueryProductState function. The
MsiQueryProductState function takes the product code as the input
parameter and then returns the installation state of the program.
http://support.microsoft.com/default.aspx?scid=kb;EN-US;884468
With Best Regards,
Mitesh Mehta
Email : miteshvmehta@gmail.com
http://cc.1asphost.com/miteshvmehta/
Studio .NET programmatically.
Typically, you can examine a file or a registry entry to determine
whether you have a version of Microsoft Visual Studio .NET, or of any
program, already installed on your computer. You can also determine
the version information programmatically by using Microsoft Windows
Installer functions such as the MsiQueryProductState function. The
MsiQueryProductState function takes the product code as the input
parameter and then returns the installation state of the program.
http://support.microsoft.com/default.aspx?scid=kb;EN-US;884468
With Best Regards,
Mitesh Mehta
Email : miteshvmehta@gmail.com
http://cc.1asphost.com/miteshvmehta/
Subscribe to:
Posts (Atom)