Tips: Prevent SSRS report chaching

There are many ways to turn off the report cache, but many of them need you to have enough privilege to change the settings. There is one approach that everyone can do: from 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.

[eBook]Improving .NET Application Performance and Scalability

A very good book from MS would like to share with everyone: Improving .NET Application Performance and Scalability You can also read it on-line.

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 is 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.

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
{
//According to the rule below, we should pass Uri object instead of string.
//http://msdn.microsoft.com/en-us/library/ms182360.aspx
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;
}

MS recommend us to use the FtpWebRequest class if the file is on a FTP server.
public string DownloadFile(string url, string username, string pw)
{
string strResult = "";
try
{
FtpWebRequest oFtpReq = (FtpWebRequest)WebRequest.Create(new Uri(url));
oFtpReq.Method = WebRequestMethods.Ftp.DownloadFile;

//set the username & password
oFtpReq.Credentials = new NetworkCredential(username, pw);

//There is no need to use the binary mode for a simple and small text file
oFtpReq.UseBinary = false;

//Add this if you get an error like this:
//The remote server returned an error: 227 Entering Passive Mode
oFtpReq.UsePassive = false;
FtpWebResponse oFtpResp = (FtpWebResponse)oFtpReq.GetResponse();

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

oFtpResp.Close();
oFtpReq = null;
oFtpResp = null;
}
catch (WebException wex)
{
//your error handling here
}
return strResult;
}

Basically, using WebClient is the same with using WebRequest.
public string DownloadFile3(string url, string username, string pw)
{
string strResult = "";
try
{
using (WebClient oWebClient = new WebClient())
{
oWebClient.Credentials = new NetworkCredential(username, pw);
strResult = oWebClient.DownloadString(new Uri(url));
}
}
catch (WebException wex)
{
//your error handling here
}
return strResult;
}

PS:
1.Be careful if the file size is big. If so, it's better to use byte[] to hold the stream.
2.FtpWebRequest supports resume download.

ASP.Net: State Management

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

Random Posts

Powered by Stuff-a-Blog