Wednesday, December 5, 2007

String.Format - include { in output

I had to work with string formatting today for a number of time.
string.Format is a powerful mechanism to display strings in different format. It formats numeric values, date values and many other objects into different formats, different patterns.

I had to format a number based on the format given from the configuration file. It is just a simple case of left padding with zeros. The value in the configuration file will be like "######". Based on the length of this mask, I have to pad the resultant number. For example, if the mask is "######" and the number is 6, I should display the result as "000006".

It is a simple issue, if I use string concatenation connected to string.Format. So first I did the same in the following way.

string format = "{0:d" + mask.Length.ToString() + "}";
string formattedValue = string.Format(format, count);

But I felt that this is wrong. I don't use string concatenation. A performance issue. I have to use string.Format for the total formatting. So I tried many ways to format; but most of them resulted into run time error. For example,

string format = string.Format("{0:d{0}}", mask.Length);
string formattedValue = string.Format(format, count);

I understood that this happens because there I need "{" in the output of the first line. An escape sequence? I tried to put a "\" just before the "{". Now even the compiler didn't allow me to do so.

So what is next? After a moment of silence, I thought about putting an additional "{" where I require a "{" come as out put. So my code looked like this.

string format = string.Format("{{0:d{0}}}", mask.Length);
string formattedValue = string.Format(format, count);

To my happiness, that code worked.

Wednesday, October 24, 2007

Thread aborted while response.redirect. ASP.NET

Thread was being aborted - Response.Redirect or Server.Transfer Yesterday I was working with the maintenance of a project that was working fine. After the required changes in the code, my team mate did the unit testing. At that time she found that at some point the application throws an error message "Thread was being aborted". The same code was working fine earlier and we didn't expect that error there. She started to debug and found that the error is thrown from a Server.Transfer() call. However the code was there in the previous version and it was working. Our eyebows became more curved and forehead with wrinkles.
As I couldn't solve the problem in the first look, I decided to revert the code and test. Yes.. it is working. The main change what we had done was to take out the user interaction of that page and the required values came to this page as query string. I had a button clik event earlier. The code resided in that event is now (by the change) is called from the Page.Load event. I understood that the reason is that we are calling this method from the Page_Load(). But we have to do that.
So the next step is to do some googling. What I got is the information from Microsoft that this is by design. They suggested to use Server.Transfer("page", false) or Response.Execute(). But I was not ready to change my worked code. I started to do "comment and uncomment" debugging method to find tha actual villain. At last I found.

In my code Page_Load(), there was a line in the beginning "Session.Abandon()". I used this line of code to release the session when the user logged out from the application. When I commented that line my application worked with the the new code. I am happy, but I have to think about the reason behind it. I will tell later.

Wednesday, October 17, 2007

Introduction

I wonder all days, in the morning, mid-day, evening or in the night. What all the new information we gather all days! How many new faces, we see! Many kind of people we met! Even if it is an ordinary gray day. Yes! There are new... A lot of, a host of new....

I am working in the field of Information Technology. Life as a software engineer. Hectic, but enjoying.
One day I thought that I will write all the new information I get every day some where. Started to think long back. Years! And now comes my first blog. The introduction.