Delegate in asp dotnet csharp, with simple example

In asp.net csharp, delegates allow programmer to create a variable that ‘points’ to a method.

We know that variables STORES VALUES, similar way deligates STORES POINTER TO METOD.

So we can dynamically or at various time SET DELIGATE in a way that our CALL actually invokes different method.

First step when using a delegate is to define its signature.

So when we use(or point) a delegate variable, we can point only to a method that matches its specific signature.

//declare deligate signature
public delegate int myCustomDeligate(int v1, int v2);

protected void Page_Load(object sender, EventArgs e)
{
//deligate variable created
myCustomDeligate objPointerToMethod;

objPointerToMethod = AddNumber;
Response.Write(“At time 1 result is=” + objPointerToMethod(3,5) + “<br/>”);

objPointerToMethod = AddNumberV2;    //this is interesting our variable now pointing to second method
Response.Write(“At time 2 result is=” +objPointerToMethod(3, 5) + “<br/>”);

}

//one method
public int AddNumber(int a1, int a2)
{
return a1 + a2;
}

//second method with same signature,but some other logic here
public int AddNumberV2(int b1, int b2)
{
return b1 + b2 + 10;    //here adding numbers, and adding 10 also to it
}

Advertisements

How to make custom Task Scheduler in asp.net c# using thread

In asp.net there are needs for performing few activities periodically. For that  we can use some class libraries or third party component to schedule a task or function to execute periodically.

Recently i found following simplest way to execute periodically any code.

Suppose you have  a class and routine as follow in App_Code/CS,

public class ClassABC
{
public static void SomeFunctionToRunPeriodically()
{
try
{
System.IO.File.WriteAllText(System.Web.Hosting.HostingEnvironment.MapPath(“~/LatestRunSaved.txt”), “Last Run of function at : ” + DateTime.Now.ToString() + Environment.NewLine);
}
catch (Exception ex)
{
string t = ex.Message;
}
System.Threading.Thread.Sleep(20000);
SomeFunctionToRunPeriodically();    //calling self after interval

}
public ClassABC()
{
//
// TODO: Add constructor logic here
//
}
}

If you notice in above function then its calling to itself after interval.

Now in global application file ‘global.asax’  inside application start event use following lines to create a new thread based call to a function above.

void Application_Start(object sender, EventArgs e)
{
System.Threading.Thread obj = new System.Threading.Thread(new System.Threading.ThreadStart(ClassABC.SomeFunctionToRunPeriodically));
obj.Name = “AnyTestName”;
obj.IsBackground = true;
obj.Start();
}

New thread based function call will run until your application is running in IIS process, It is a background process. Other routine things on site will work normally without problem.

LINQ basic query samples in csharp asp.net with join

Assuming “db” is considered as databasecontext object

/////// typical query with condition that returns annonymous object ///////////
var contacts =
from customer in db.Customers
where customer.Name.StartsWith(“A”) && customer.Orders.Count > 0
orderby customer.Name
select new { customer.Name, customer.Phone };

/////// get count with running query///////////
var query = (from o in Order
where CustomerID = 2
select o).Count();

/////////////// inner join (matching in both only) //////////////

var cr = from c in MyDatabase.GetReadOnlyContext().tblCategories
join p in MyDatabase.GetReadOnlyContext().tblProducts on c.CategoryId equals p.CategoryId
select new { c.CategoryName, p.ProductName };

//////////////// left outer joing

var crForLeftOuterJoin = from cat in dc.tblCategories
join pro in dc.tblProducts
on cat.CategoryId equals pro.CategoryId
into xname
from pro in xname.DefaultIfEmpty()
select new {
CAP_Cat = cat.CategoryName,
CAP_Product = pro == default(tblProduct) ? “(no product)” : pro.ProductName
};

//////// getting rows from table as LIST collection /////////////
List lstCat= dc.tblCategories.ToList();

//////// getting rows from table as Array /////////////
tblCategory[] arrCat= dc.tblCategories.ToArray();

///////////////seeking particular element/single element from query ///////////////////

var firstproduct = (from product in db.Products
where product.Category.CategoryName == “Beverages”
select product
).AsEnumerable().ElementAt(0);

online video conversion(flv conversion) using asp.net(web development) – borania siddharth

Audio and Video files are becoming important part of web sites, now a days.

Many social networking and job portal sites are built to offer audio/video upload and play functionality.

These videos are then played in client’s browsers.

Playing video in client’s machine is preferred with FLV format rather than any other(like .wmv).

And same technology is applied for audio playing.

and as there is different format files may be uploaded by user, always there is question to weather restricting users for ONLY uploading FLV files or giving them FLEXIBILITY to upload file in their desired(available) format, and online convert it into FLV.

So, here i have present small code snippet, to outline how FLEXIBILITY may be given to user for uploading any audio/video format, and same time its playable by website.

string strinp;
strinp = Server.MapPath(“~/test1.wmv”).ToString();

string strop;
strop = Server.MapPath(“~/test2.flv”);

Process p;
try
{
ProcessStartInfo info = new ProcessStartInfo(Server.MapPath(“~/ffmpeg.exe”));

info.Arguments = “-i \”” + strinp + “\” \”” + strop + “\””;

p = Process.Start(info);
p.Start();
p.Kill();
}
catch (Exception ex)
{
//catch error here
p.Kill();
}

Any Suggestions are welcome.

-By Siddharth Borania from Dhanashree Incorporation