Most important topics

Static Constructor A static constructor is used to initialize the static data once or performed an action to once only. A static constructor does not take access modifiers or have parameters. A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced. User has …

UK Postcode Validation

Regular expression for JavaScript var regPostcode = /^([a-zA-Z]){1}([0-9][0-9]|[0-9]|[a-zA-Z][0-9][a-zA-Z]|[a-zA-Z][0-9][0-9]|[a-zA-Z][0-9]){1}([ ])([0-9][a-zA-z][a-zA-z]){1}$/; var result = regPostcode.test(SearchTerm); if (!result) { alert(“Please enter valid postcode”); return false; } Regular expression for C# string regPostcode = “([a-zA-Z]){1}([0-9][0-9]|[0-9]|[a-zA-Z][0-9][a-zA-Z]|[a-zA-Z][0-9][0-9]|[a-zA-Z][0-9]){1}([ ])([0-9][a-zA-z][a-zA-z]){1}”; System.Text.RegularExpressions.Regex regx = new System.Text.RegularExpressions.Regex(regPostcode); if(!regx.IsMatch(address0.PostalCode)) { results.Add(new ValidationResult(String.Format(“Please enter valid postcode for {0}.”, addressRank))); } You can check the regular expression at http://www.regexplanet.com/ …

Remove illegal characters from file name or file path in c#

File cannot be saved if file name has illegal characters. So how to remove the illegal characters by a single line code as below. I use LINQ Querty for that. string fileName = “prakash C/O swami.xml”; fileName = System.IO.Path.GetInvalidFileNameChars().Aggregate(fileName, (current, c) => current.Replace(c.ToString(), string.Empty)); Now the fileName is “rakash CO swami.xml”. The code removes slash …

SQL Server Transaction

BEGIN TRANSACTION BEGIN TRY — put SQL commands here INSERT/UPDATE/Delete — if successful – COMMIT the work COMMIT TRANSACTION END TRY BEGIN CATCH — handle the error case (here by displaying the error) SELECT ERROR_NUMBER() AS ErrorNumber, ERROR_SEVERITY() AS ErrorSeverity, ERROR_STATE() AS ErrorState, ERROR_PROCEDURE() AS ErrorProcedure, ERROR_LINE() AS ErrorLine, ERROR_MESSAGE() AS ErrorMessage — in case …

How to display Progress bar while page is loading

<pre> <code> protected void Page_Load(object sender, EventArgs e) { showPageLoad(); //do somthing     } private void showPageLoad() { int i=0; Response.Write(“<div id=’divTitle’>Loading(<span id=’message’>0%</span>)</div>”); Response.Flush(); for(i=5;i<=100;i=i+5) { System.Threading.Thread.Sleep(200);// any codes can be typed here outputflash(i.ToString() + “%”); Response.Flush(); } } private void removePageLoad() { ScriptManager.RegisterStartupScript(Page, this.GetType(), “deleteLoading”, “var d = document.getElementById(‘divTitle’);d.parentNode.removeChild(d);”, true);     } private void …

Download / Upload Files on FTP Server

Require following ftp details where you want to upload the files: FTP URL: “ftp://ftp.yoursite.com/” FTP Username: username FTP Password: password FTP Port: 21 Upload File public string UploadFile(string FtpUrl, string fileName, string userName, string password, string UploadDirectory = “”) { string PureFileName = new FileInfo(fileName).Name; String uploadUrl = String.Format(“{0}{1}/{2}”, FtpUrl, UploadDirectory, PureFileName); FtpWebRequest req = …