.NET Tip of The Day
Learn one new .NET trick every day
Login or Join

.NET Tips & Tricks Community (ALPHA)

First 1 2 3 4 5  ... Last  RSS

need to use either “using” or explicitly close the connection if we use the datareader

We need to use either “using” or explicitly close the connection if we use the datareader.

Otherwise, it will lead to Connection Pool error. See the underlined text below:

 

However, there are other cases where it is unclear when to close the connection. An example is the ExecuteReader method. This method returns an object that implements the IDataReader interface. The Database base class has a default implementation that returns a DbDataReader object. DbDataReader objects are designed to read specific portions of the data as needed, which requires an open connection. In other words, it is unknown when the application no longer needs the DbDataReader. If the Data Access Application Block methods close the connection before returning the DbDataReader, the DbDataReader becomes useless to the client code. Instead, the DbDataReader methods indicate to the underlying ADO.NET call to automatically close the connection when the DbDataReader is finished. In this situation, it is considered a best practice for the application to ensure that the DbDataReader is closed in a timely fashion, either by explicitly closing the reader with the DbDataReader.close method or by forcing the disposal of the DbDataReader, which results in the Close method being called.

The following code demonstrates a call to the ExecuteReader method. The using statement (Using in Visual Basic) ensures that the DbDataReader object is disposed, which closes the DbDataReader object.

[C#] 

Copy imageCopy Code

Database db = DatabaseFactory.CreateDatabase();

 

DbCommand dbCommand = db.GetSqlStringCommand("Select Name, Address From Customers");

using (IDataReader dataReader = db.ExecuteReader(dbCommand))

{

// Process results

}

 

 

submitted by saisuchir
5/15/2008

BrowsableAttribute

when u are creating a control, and u decides that a property or a event should not be displayed in property window.

System.ComponentModel.BrowsableAttribute is there for you, to do the above.

Eg:

private string text;

[Browsable(false)]        //  True is default
 public string Text
      {
         get
         {
            return text;
         }
         set
         {
            text = value;
         }
  }

submitted by dhanaid
5/13/2008

Get rid of unused usings

In a Visual Studio 2008 source window, right-click and select Organize Usings | Remove Unused Usings. Visual Studio 2005 users may use ReSharper for this.

 

P.S. interesting, does this optimization gives you any benefits other than cleaner code?

submitted by kostya.ly
5/12/2008

Capitalization of ID and OK abbreviations used in identifiers

I was wondering how to capitalize properties which have either ID or OK abbreviation in its name. For example, is it TipID or TipId? I have found the answer in Design Guidelines for Developing Class Libraries:

In Pascal-cased identifiers (all identifiers except parameter names) they should appear as Id, and Ok. If used as the first word in a camel-cased identifier, they should appear as id and ok, respectively.

 

P.S. And of course .NET Framework doesn't fulfil this rule :) For example, Control.ClientID property.

submitted by kostya.ly
5/7/2008
First 1 2 3 4 5  ... Last  RSS
RSS .NET Tip of The Day
Subscribe to receive one tip from the .NET Tips and Tricks Community per day.
Previous Tips of The Day
The best of the .NET Tips & Tricks Community.
.NET Practitioners .NET Tips & Tricks Community
Every .NET practitioner has a trick up in their sleeve. This is the place to share it with other .NET people.
Submit a Tip
Discovered a new trick? Share it with others.
My Tips
Manage tips you authored.