2009/06/30

Tips: Prevent SSRS report caching

There are many ways to turn off the report cache, but many of them need you to have enough privilege to change the report or server settings. There is one approach that everyone can do without any privilege: comtrol the parameters in the url.

Append this commend to the end of the url:
&rc:emissionTime=633819525738564717
The number should be random, and you can do like this:
"http://.....&rc:emissionTime=" + DateTime.Now.Ticks;

The it's the same trick like this one.

2009/06/18

Tips: Use String.IsNullOrEmpty() instead of != "" to check empty string

According to FxCop's recommendation, use String.IsNullOrEmpty() is a good approach to check the String is empty or not.

Case 1:
//No good
strUsername != ""
Case 2:
//Better
strUsername.Length != 0
Case 3:
//The Best
!String.IsNullOrEmpty(strUsername)
Reason: Equals requires more MSIL instructions than the other two. Length will throw an exception if the string is null. IsNullOrEmpty has the same performance with Length but also takes care of the null problem.

Go here for more detail information and example.

2009/06/11

Download a single file from the remote FTP site (Updated)

Three approaches to download a file from FTP server: WebRequest, FtpWebRequest, and WebClient. Here are the examples:
url = "ftp://xxx.xxx.xxx.xxx/test.txt"

WebRequest
using System.Net;  //don't forget this
public string DownloadFile(string url, string username, string pw)
{
  string strResult = "";
  try
  {
    WebRequest oRequest = HttpWebRequest.Create(new Uri(url));
    oRequest.Credentials = new NetworkCredential(username, pw);
    WebResponse oResponse = oRequest.GetResponse();

    using (StreamReader sr = new StreamReader(oResponse.GetResponseStream()))
    {
      strResult = sr.ReadToEnd();
      sr.Close();
    }

    oRequest = null;
    oResponse = null;
  }
  catch (WebException wex)
  {
    //your error handling here
  }
  return strResult;
}

2009/06/03

ASP.Net: State Management

I strongly recommend this document (from MS) if you would like to truly understand this topic.