Tuesday, April 27, 2010

using statement in C#

This is a nice little statement which reduces our coding and minimises prone to errors.
There are many situations we have to Dispose the object we created. Example

string connString = "Data Source=localhost;Integrated Security=SSPI;Initial Catalog=MyDB;";

SqlConnection myConnect = new SqlConnection(connString);
SqlCommand cmd = myConnect.CreateCommand();
cmd.CommandText = "SELECT * FROM MyTable";


SqlDataReader dr = cmd.ExecuteReader();

while (dr.Read())
Console.WriteLine("{0}\t{1}", dr.GetString(0), dr.GetString(1));


Here on above example I just try to connect to a database and take out all records from MyTable.

I have created a SqlConnection object named myConnect  and a SqlDataReader object named dr, at the very bottom of the code snippet I have disposed those two objects using Dispose() method.

What will happen if I forget to dispose these objects? memory issues, using statement is a remedy for this and its makes code more readable, robust and clean. I will recode the above code snippet with using “using ” statement.

using (SqlConnection myConnect = new SqlConnection(connString))
SqlCommand cmd = myConnect.CreateCommand();
cmd.CommandText = "SELECT * FROM MyTable";


using (SqlDataReader dr = cmd.ExecuteReader())

while (dr.Read())
Console.WriteLine("{0}\t{1}", dr.GetString(0), dr.GetString(1));

Don’t you feel more comfortable with using statement? Let me just go bit deep on using statement, how does this work? Actually when an using statement compiled, the CLR converts it to a try and Finally block. It does three things, get the resources (SqlConnection ), encloses in a try block and disposes in the Finally block. Hope you enjoyed …………….

Wednesday, April 21, 2010

Referring to another config file in web.config file

Instead of having all connection string detail on web.config file , we can create a new configuration
whatEvernName.config eg(connectionString.config) and have all connection string details in this new .config file.

Please go through the code below to understand this.

<connectionStrings configSource="connectionString.config" />

clear /> 
add name="myConnection" connectionString="myConnectionString" providerName="System.Data.SqlClient" /> </connectionStrings>

This is really nice feature when you have a lot of tags in your web.config file,
as it increases the readability. There is another cool feature which is very useful for
developers when it’s come to development or rather I would say debug the code.
Most of the times we have our connection string in one file and there is a high chance to forget
to change live connection string properties to test environment properties, I have made such a mistake and had to restore a latest database backup, but luckily there was not any data lost.

This cool feature avoids taking place such a bad mistake. Try this it’s is really nice and you do not have to keep on changing your web.config’s conection string values. As in above code snippet you can have two config files as connectionString.debug.config and connectionString.releaase.config

VS IDE will take the valuse depending on what you have selected, debug or release. Isn’t it cool.

Tuesday, April 20, 2010

GridView column text wrap width

We had a grid view and one of the column data was dynamically bound. Most of the time this grid goes out of the sreen area and makes user to scroll to right, because of this dynamically bounded column's text lenght. There were some solutions on the web but none of them working properly. Solution is, put the text inside a paragraph tag “<p> your text here</p>”


Friday, April 09, 2010

Rules to Better Websites Navigation

These rules I found while I am going through Scott Guthrie's web blog. It's captioned as "Adam Cogan's Rules for Better WebSites" and this was written in September 2003, pretty old but still apply.


Scott Guthie's web blog http://weblogs.asp.net/Scottgu/

My Achievements

Member of

Blog Archive