Tag Archive for 'c#'

Page 2 of 2

C#, decimal.toString(), and how to get rid of trailing zeros

Today I spend A LOT OF time on trying to display a percentage – string representation of a decimal but without trailing zeros, e.g. 15.1% instead of 15.10%.

In the application I already had NumberFormatInfo for displaying monetary values and percentages. An object of that type was defined as follows:

NumberFormatInfo nfi = new NumberFormatInfo();
nfi.PercentDecimalDigits = 2;
// some additional settings for nfi 
// (including monetary and numeric attributes)

Continue reading ‘C#, decimal.toString(), and how to get rid of trailing zeros’

System.Net.WebException: The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.

One of my applications was supposed to use image having its URL. The Image was instantiated with the stream the server that stored the image file responded with after being requested as below:

// create a request for image
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(IMAGE_URL);
request.Method = "GET";
request.ContentType = "multipart/form-data";
request.UserAgent = "Mozilla/4.0+(compatible;+MSIE+5.01;+Windows+NT+5.0";
request.Proxy.Credentials = CredentialCache.DefaultCredentials;
request.Credentials = CredentialCache.DefaultCredentials;

// get response from the server
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream resStream = response.GetResponseStream();

// create image from stream
Image image = Image.FromStream(resStream);

It worked like a charm when TCP port was used (regular, non-decure connection) but whenever the image was available at a URL that was secured (https://xxx.xx) I was getting the following stack trace:

Continue reading ‘System.Net.WebException: The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.’

MonoRail – RenderMailMessage – System.ArgumentNullException: Value cannot be null. Parameter name: format

This was a nasty issue…

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentNullException: 
Value cannot be null.
Parameter name: format
   at System.String.Format(IFormatProvider provider, String format, Object[] args)
   at System.String.Format(String format, Object arg0, Object arg1)
   --- End of inner exception stack trace ---


This is part of the stack trace I got in one of my applications which uses MonoRail. I got it while creating the Castle.Components.Common.EmailSender.Message in order to prepare the content of the email having the name of its template file (vm):

Message msg = RenderMailMessage(templateName)

That view file defined the content and used data from PropertyBag and from Resource files.

Just to recall a resource file (resx) is bound with the controller class with this definition:

[Resource("text", "LocalizationSample.Resources.Home")]

What was wrong there? I was sure templateName passed as the parameter was correct – it for sure pointed to correct vm file. Moreover, that piece of code was defined in a superclass which was extended by this particular controller and another one which also could send this email. Of course there was no problem with sending email by the latter.


The problem here was I used this construction in the vm file:

$string.format($text.someText, $param1, $param2)

And for some reason I forgot to bind the appropriate resource file (the one referenced with $text) with one of the controllers. As a result string.format failed because $param1 and $param2 couldn’t be injected into string which was not found.

Summary of Communities to Communities (C2C) 2009 Conference

C2C is a history now. I attended .NET stream and one session on SQL. The conference as a whole was very interesting, well organised, and basically developing. It was a Polish conference so most sessions were conducted in Polish but there were a couple of speakers from abroad too, and they spoke in English.

Basically I’m happy I had a chance to attend the conference. Below are my notes and comments. Please note they are my comments; should I write something which is not true or accurate, please let me know, sometimes pace was fast so I might have skipped something icon smile Summary of Communities to Communities (C2C) 2009 Conference
Continue reading ‘Summary of Communities to Communities (C2C) 2009 Conference’

System.IO.IOException: The process cannot access the file used as attachment with MailMessage

Recently I had to improve the mechanism of sending emails so that files can be attached to it. As I used MailMessage it seemed to be an easy task, especially that MSDN provided an example of how to do it. They assumed there that the file used as the attachment was already on the server and it shouldn’t be deleted after the email is sent.

However, I had to upload this file and therefore remove it afterwards. At the end of the day I ended up with an exception thrown while removing the file:

System.IO.IOException: The process cannot access the file 'XXX' because it is being used by another process.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.File.Delete(String path)


There’s a simple fix for that. After the email is sent but before the uploaded attachment is deleted use Dispose() method of MailMessage. This way you will release all resources used by MailMessage.

Alternatively you can create MaillMessage within using block. As a result all resources will be released (disposed or closed) automatically when execution of the block completes.

using(MailMessage email = new MailMessage(...))

How to dynamically add new columns to report created with Reporting Services?

Imagine you have a report that displays some data and the number of columns cannot be defined at the design stage, i.e. the number of columns depends on user input. That seems to cause some problems when you want to render this report with Reporting Services…


Note: The described solution applies to the case when RDLC files are used by ReportViewer control in local mode. However, it can be simply tweaked so it works in general.

Because the number of columns that should be displayed on the report can be named only at runtime it seems reasonable to update the RDLC file and only then deliver the report. RDLC is an XML file that contains the definition of the report – styling and all fields displayed. So all you need to do is to read the RLDC file into XmlDocument, get some nodes and update them.
Continue reading ‘How to dynamically add new columns to report created with Reporting Services?’

Refreshing C# and ASP.NET skills

As a matter of fact I’ve been learning Java (for SCJP) and Microsoft .NET (because of project change) recently.

This week I attended two crash courses:

Those courses were fine; I refreshed a lot and learned some new stuff as well. Now it’s time to play around with this knowledge in real world icon wink Refreshing C# and ASP.NET skills

Java vs. C#

From now on I’ll be doing much more in Microsoft .NET rather than in Java. Thus, it seems my preparation for SCJP exam will slow down now… Also, I presume there’ll be slightly less about Java on that blog from now on (at least for some time).

I learned about .NET framework and C# programming language at the univesity, but it was a few years ago. Also, I’ve never been involved in a business project that used this framework. So, time to refresh the knowledge of .NET icon smile Java vs. C#

Luckily, it seems there shouldn’t be much difficulties in switching from Java to C#. Of course, those languages differ but still they have a lot in common. This article should be helpful in understanding the differences between Java and C#. It’s well written and the theory is supported with code examples.

After refreshing C# there will be time to re-learn ASP.NET…