Sunday, 16 February 2014

Implement Animated cursor using C# Winforms



First you must add three namespaces like below.

using System.ComponentModel;
using System.Reflection;
using System.Runtime.InteropServices;


Then write a method (LoadCustomCursor) like given below.
 
public static Cursor LoadCustomCursor(string path)
        {
            IntPtr hCurs = LoadCursorFromFile(path);
            if (hCurs == IntPtr.Zero) throw new Win32Exception();
            var curs = new Cursor(hCurs);
            // Note: force the cursor to own the handle so it gets released properly
            var fi = typeof(Cursor).GetField("ownHandle", BindingFlags.NonPublic | BindingFlags.Instance);
            fi.SetValue(curs, true);
            return curs;
        }


        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        private static extern IntPtr LoadCursorFromFile(string path);


Sample usage:

private void btnSave_Click(object sender, EventArgs e)
{ 
this.Cursor = LoadCustomCursor(Application.StartupPath + "\\images\\Black and Blue Working in Background.ani");

//Enter your C# Code here

this.Cursor = Cursors.Default;

}

How to calculate the sum of the datatable column in asp.net?

To calculate the sum of a column in a DataTable use the DataTable.Compute method.

Example of usage

object sumObject = dtPayment.Compute("Sum(Amount)", "");

Display the result in your Total Amount Label like so:


lblTotalAmount.Text = sumObject.ToString();
 

Wednesday, 18 December 2013

Cannot delete file its being used by another process



delete file error the process cannot access the file because it is being used by another process.the process cannot access the file because it is being used c#.

Solution

Type following code before your code
GC.Collect();
GC.WaitForPendingFinalizers();

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)";