Wednesday 9 October 2013

Time validation C#



using System.Text.RegularExpressions;

string strcd = DateTime.Now.ToString("hh:mm:ss tt"); //02:56:59 PM 
bool isOK = Regex.IsMatch(strcd, @"[0-2][0-9]\:[0-5][0-9]\:[0-5][0-9]\ [aApP][mM]");
if (isOK)
  {
     // Do something.
  }
else
  {



string strcd = DateTime.Now.ToString("hh:mm tt"); //01:06 PM        
bool isOK = Regex.IsMatch(strcd, @"[0-2][0-9]\:[0-5][0-9]\ [aApP][mM]");
if (isOK)
  {
     // Do something.
  }
else
  {
     // Do something else.
  }

Disable Browser Back Button



Javascript


<script type = "text/javascript" >
function disableBackButton()
{window.history.forward();
}
setTimeout("disableBackButton()", 0);
</script>

For internet explorer: <body onload="disableBackButton()">
If you are using firefox then use <body onunload="disableBackButton()">


C#  Asp.net

Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.SetValidUntilExpires(true);

Remove duplicate rows from a datatable in ASP.NET/C#



In this article I will explain how to remove (delete) duplicate rows (records) from DataTable using DataView in C# .

Namespaces
You will need to import the following namespace.

using System.Data;

Code:C#

DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Country") });
        dt.Rows.Add(1, "Raja Durai", "United States");
        dt.Rows.Add(2, "Raja", "United States");
        dt.Rows.Add(3, "Durai", "India");
        dt.Rows.Add(4, "S.Raja Durai", "India");
        dt.Rows.Add(4, "S.Raja Durai ", "India");
        dt.Rows.Add(4, "S.Raja Durai ", "India");
dt = dt.DefaultView.ToTable(true, "Id", "Name", "Country");

How to get File size in ASP.NET/C#



I want to share with you how to get the file size in asp.net. Its very simple...

Code:C#

You will need to import the following namespace.
Using System.IO;

string sMyFileName = "~/rajadurai/raja.jpg";

FileInfo finfo = new FileInfo(Server.MapPath(sMyFileName ));
long FileInBytes = finfo.Length;
long FileInKB = finfo.Length / 1024;
long FileInMB = FileInKB /1024;

lblImgSize.Text="File Size: " + FileInBytes.ToString() +" bytes (" + FileInMB.ToString() + " MB)";