Sunday, July 27, 2008
Factory patteren based code in C#.net
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public interface myinterface
{
int add(int x,int y);
//int mul(int w, int z);
}
public class class1 : myinterface
{
public int add(int a, int b)
{
int c = a + b;
return c;
}
}
public class class2 : myinterface
{
public int add(int j, int k)
{
int l = j * k;
return l;
}
}
class Factory
{
public myinterface GetObject(int type)
{
myinterface base1 = null;
switch (type)
{
case 1:
base1 = new class1();
break;
case 2:
base1 = new class2();
break;
}
return base1;
}
}
public partial class _Default : System.Web.UI.Page
{
Factory factory = new Factory();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
myinterface mytest = factory.GetObject(1);
int result = mytest.add(Convert.ToInt32(TextBox1.Text.ToString()), Convert.ToInt32(TextBox2.Text.ToString()));
txtresult.Text = Convert.ToString(result);
}
protected void Button2_Click(object sender, EventArgs e)
{
myinterface multiply = factory.GetObject(2);
int multiplevalue = multiply.add(Convert.ToInt32(TextBox1.Text.ToString()), Convert.ToInt32(TextBox2.Text.ToString()));
txtmultiply.Text = Convert.ToString(multiplevalue);
}
}
Thursday, July 24, 2008
Regular Expression in dotnet
Regular expression examples for decimals input
Positive Integers — ^\d+$
Negative Integers — ^-\d+$
Integer — ^-{0,1}\d+$
Positive Number — ^\d*\.{0,1}\d+$
Negative Number — ^-\d*\.{0,1}\d+$
Positive Number or Negative Number - ^-{0,1}\d*\.{0,1}\d+$
Phone number — ^\+?[\d\s]{3,}$
Phone with code — ^\+?[\d\s]+\(?[\d\s]{10,}$
Year 1900-2099 — ^(19|20)[\d]{2,2}$
Date (dd mm yyyy, d/m/yyyy, etc.) — ^([1-9]|0[1-9]|[12][1-9]|3[01])\D([1-9]|0[1-9]|1[012])\D(19[0-9][0-9]|20[0-9][0-9])$
IP v4 — ^(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]){3}$
Regular expression examples for Alphabetic input
Personal Name — ^[\w\.\’]{2,}([\s][\w\.\’]{2,})+$
Username — ^[\w\d\_\.]{4,}$
Password at least 6 symbols — ^.{6,}$
Password or empty input — ^.{6,}$|^$
email — ^[\_]*([a-z0-9]+(\.|\_*)?)+@([a-z][a-z0-9\-]+(\.|\-*\.))+[a-z]{2,6}$
domain — ^([a-z][a-z0-9\-]+(\.|\-*\.))+[a-z]{2,6}$
Url — http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?
Other regular expressions
Match no input — ^$
Match blank input — ^\s[\t]*$
Match New line — [\r\n]|$
Friday, July 18, 2008
How use trigger in sql
CREATE trigger [TRIGGER_Deletesubcategory] ON dbo.category
FOR delete
AS
declare @nmid int
select @nmid=sno from deleted
delete from subcategory where cat_id= @nmid
There will be two table one is category and second one is subcategory and the primery key of category table will be sno and the this will foreign key(cat_id) for subcategory table.
Friday, July 11, 2008
Image Thumbnail using C#.net
resizeimage(imagename); (where imagegname like- image1.jpg)
//this is the image control where you bind the image
img src='MakeThumbnail.aspx?file="imagename" &h=" + h + " &w=" + w + "' height="200" widht="200"
//Resize function (where width of the image will be fix and the hight of the image will be change according to height
public void resizeimage(string image)
{
System.Drawing.Image img;
try
{
string path = Server.MapPath("imagefolder/" + image);
img = System.Drawing.Image.FromFile(path);
}
catch
{
string path = Server.MapPath("imagefolder/no_img_avai.gif");
img = System.Drawing.Image.FromFile(path);
}
h = img.Height;
w = img.Width;
if (w >= 103)
{
int hei = h * 103;
int he = hei / w;
h = he;
w = 103;
}
else
{
int hei = h * 103;
int he = hei / w;
h = he;
}
}
//MAKE ONE ASPX FILE NAME MakeThumbnail.aspx
using System;
using System.IO;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class MakeThumbnail : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// get the file name -- fall800.jpg
string file = Request.QueryString["file"];
int w = Convert.ToInt32(Request.QueryString["w"]);
int h = Convert.ToInt32(Request.QueryString["h"]);
string filepath = Server.MapPath("imagefolder/" + file.ToString());
// create an image object, using the filename we just retrieved
System.Drawing.Image image = System.Drawing.Image.FromFile(filepath);
// create the actual thumbnail image
System.Drawing.Image thumbnailImage = image.GetThumbnailImage(w, h, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
// make a memory stream to work with the image bytes
MemoryStream imageStream = new MemoryStream();
// put the image into the memory stream
thumbnailImage.Save(imageStream, System.Drawing.Imaging.ImageFormat.Jpeg);
// make byte array the same size as the image
byte[] imageContent = new Byte[imageStream.Length];
// rewind the memory stream
imageStream.Position = 0;
// load the byte array with the image
imageStream.Read(imageContent, 0, (int)imageStream.Length);
// return byte array to caller with image type
Response.ContentType = "image/jpeg";
Response.BinaryWrite(imageContent);
}
public bool ThumbnailCallback()
{
return true;
}
}
Tuesday, July 8, 2008
Change color after click on checkbox in grid using javascript
function changeColor(CheckBoxObj)
{
if (CheckBoxObj.checked == true) {
CheckBoxObj.parentNode.parentNode.style.backgroundColor='#88AAFF';
}
else
{
CheckBoxObj.parentNode.parentNode.style.backgroundColor='';
}
}
call it check box onclick event
onclick="changeColor(this);"
