Saturday, April 25, 2009

Difference Between .net FrameWork 1.1,2.0,3.0,3.5

2.0 has some extendable features when compared to 1.1

1).net 2.0 supports web Request, web Response and web client classes

2).net 2.0 added the features of generic classes
3).net 2.0 added the feature of MultipleActiveResultSet,UserDefinedDataTypes,xmlDataTypes
4).net 2.0 UTF8Encoding is more faster than 1.1
5)When the file attachment u s

To read more Detail click the following link
http://msdn.microsoft.com/en-us/library/t357fb32.aspx

Difference between 2.0 and 3.0 is
WCF,WPF,WWF,WindowsCardSpace

3.0 and 3.5 is similar, with the additional features of enhanced WCF Control,ASp.net with Ajax , LinQ

Monday, February 2, 2009

Unsafe code may only appear if compiling with /unsafe in VisualStudio 2005

Hi,
Here is the error which i got when using unsafe code
Unsafe code may only appear if compiling with /unsafe
Here is the solution for the above error
Add the following line under system.codedom /compilers
compiler language="C#;cs;csharp" extension=".cs" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" compilerOptions="/unsafe" /

UnSafe Code in C#

Hi,
Here is some details about Unsafe in C#
Unsafe code is code which does not executed under full control of CLR. It can cause some problems therefore each use must be marked as "unsafe".
...
unsafe
{
...
// unsafe context: can use pointers here
...
}
...
Generally speaking when you write any program in C# you create managed code. Managed code is executed under the control of Common Language Runtime (CLR). CRL causes that programmer do not need to manage memory and take care about memory's allocation and deallocation. CLR also allows you to write what is called 'unsafe code'.

Advantages:

  • Performance and flexibility, by using pointer you can access data and manipulate it in the most efficient way possible.
  • Compatibility, in most cases we still need to use old windows APIs, which use pointers extensively. Or third parties may supply DLLs that some of it’s functions need pointer parameters. Although this can be done by using DLLImport and System.IntPtr class, but in some cases it’s just much simpler to use pointer.
  • Memory Addresses, there is no way to know the memory address of some data without using pointers.

Disadvantages:

  • Complex syntax, to use pointers you need to go throw more complex syntax than we used to experience in C#.
  • Harder to use, you need be more careful and logical while using pointers, miss using pointer might lead to the following:
    • Overwrite other variables.
    • Stack overflow.
    • Access areas of memory that doesn’t contain any data as they do.
    • Overwrite some information of the code for the .net runtime, which will surely lead you application to crash.
  • Type-safety, using pointers will cause the code to fail in the .net type-safety checks, and of course if your Security policy don’t allow non type-safety code, then the .net framework will refuse to execute it.

Face Detection in Webapplication asp.net c#

Hi,
Here is the web application to detect the face on the image .Face Detection is does by open CV (open source Computer vision).

It is a library programming unit aimed at facial Recognition, motion detection . Download open CV here http://www.brothersoft.com/opencv-20868.html and install it Here is the link for face detection in windows application .http://www.codeproject.com/KB/library/eyes.aspx?fid=990485&df=90&mpp=25&noise=3&sort=Position&view=Quick&fr=51&select=2502155.The code that Modified from window application to web application.
To execute the application
Create Images Folder in the path of your website , place sample image as test.jpg .

Here is the code for aspx page
Add the following controls : file upload , Button,Image , HiddenField

Here is the code for aspx.cs page
public partial class _Default : System.Web.UI.Page
{
private string fileName;
private Guid imageId;
private Bitmap lastPic;
private clsFaceDetectionWrapper objFaceDetector = null;
private Pen myPen = new Pen(Color.Red, 2.0f);
Graphics picGraphics;
Graphics g;
private string filePath;
protected void Page_Load(object sender, EventArgs e)
{
objFaceDetector = clsFaceDetectionWrapper.GetSingletonInstance();
detectedImage.ImageUrl = Server.MapPath("./images/output.jpg");
}

void DrawRectangle(int lx, int ly, int rx, int ry)
{
g.DrawRectangle(myPen, lx, ly, rx - lx, ry - ly);
}
void DrawCross(int x, int y)
{
}

protected void detectButton_Click(object sender, EventArgs e)
{
imageId = new Guid();
imageId = Guid.NewGuid();
if (imageUpload.HasFile)
{
fileName = imageUpload.FileName.ToString().
Substring(imageUpload.FileName.ToString().LastIndexOf("."));
}
if (fileName.ToLower() == ".jpg" || fileName == ".gif" || fileName == ".jpeg" ||fileName == ".bmp" || fileName == ".png" || fileName == ".tif")
{
if (imageUpload.FileName != "")
{
pathHiddenField.Value = Server.MapPath("./images/" + imageId + "_" + imageUpload.FileName);
imageUpload.PostedFile.SaveAs(Server.MapPath("./images/" + imageId + "_" + imageUpload.FileName));
}
}
lastPic = new Bitmap(pathHiddenField.Value);
g = Graphics.FromImage(lastPic);
// here is the path to plave the cascade folder in the specified path.If Possible place the path cascade file in the execution path else modiy the face locatormethod and its coressponding dependabale method to get the path as arguments
String path = Server.MapPath("./XML/");
if (lastPic != null)
{
int faces = objFaceDetector.WrapDetectFaces(lastPic,path);

unsafe
{
int lx, ly, rx, ry, res;
for (int f = 0; f < faces; f++)
{
objFaceDetector.WrapGetFaceCordinates(f, &lx, &ly, &rx, &ry); g.DrawRectangle(myPen, lx, ly, rx, ry);
}
Response.ContentType = "image/jpeg";
lastPic.Save(Server.MapPath("./Images/Test.jpg"), ImageFormat.Jpeg); } detectedImage.ImageUrl = Server.MapPath("./Images/Test.jpg"); detectedImage.DataBind();
g.Save();
lastPic.Dispose();
g.Dispose();
}
}

Thursday, January 29, 2009

Import Sql Data To Excel

Hi,
Here is the Procedure to load the sql data to excel with out writing lot of code

1)In Excel Click the Menu Data -> ImportExternalData -> NewDataBaseQuery

2)You will get the window Choose DataSource. Select New Datasource -> click OK

3)In Create New Data Source, give the name for the Datasource .Select the driver as SqlServer
in the driver type.Click connect

4)Sql Server Login window opens ,Specify the Server Name.Click options Select the Database by default it will assign Master Database .Click Ok

5)Now the datasource will be connected.Select the table name to import your data in to excel.Click OK

6)Query Wizard Opens to select columns

7)Click the option .In the Table Options window there is a option to list only the tables,views, systemTables.Choose as per ur need.In the Owner select dbo.which excludes system tables.Click Next.Click Next

8)In query wizard- finish select the option view data or edit query in Microsoft Query.click Finish.

9)To execute your procedure click on File-> Execute Sql -> select Procedure

10)To import the data click File -> cancel and return to Microsoft excel

Interface and abstract C#

Hi,
Here is the situation of using abstract vs interface . you may get some basic idea of using interface and abstract

Consider about the Content Management system we generalize the content as
Articles,Blogs,Reviews

Interface Vs Abstract

Now Consider Content Contains behavior named publish . If the Publish contain some default behavior then the option would be abstract. If the publish is derived and make their own implementation, then no need to implement in base .In this situation the option would be Interface.


If I made content class as Interface then it is difficult to make changes in base class because if I add new method or property in content interface then I have to implement new method in every derive class. This can be overcome in abstract class by creating new method not an abstract type.

In terms of speed abstract is fast then interface because interface requires extra in-direction.

Dynamic GridView C#

Hi,

Here is the code to create dynamic gridview with the styles applied

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Sql"].
ConnectionString);

con.Open();
SqlCommand com = new SqlCommand("GetArticles", con);
com.CommandType = CommandType.StoredProcedure;
SqlDataAdapter ada = new SqlDataAdapter(com);
DataSet ds = new DataSet();
ada.Fill(ds);
for (int i = 0; i <>
{
if (ds.Tables[i].Rows.Count > 0)
{
GridView gvDynamicArticle = new GridView();
gvDynamicArticle.Width = Unit.Pixel(700);
gvDynamicArticle.BorderWidth = Unit.Pixel(0);
gvDynamicArticle.Caption = "
" + ds.Tables[i].Rows[0]["Category"].ToString() + " Articles
";

gvDynamicArticle.AutoGenerateColumns = false;
gvDynamicArticle.AlternatingRowStyle.BackColor = Color.Brown;
gvDynamicArticle.ShowFooter = true;
TemplateField tf = null;
tf = new TemplateField();
tf.HeaderTemplate = new DynamicGridViewTextTemplate("ArticleID", DataControlRowType.Header);
tf.ItemTemplate = new DynamicGridViewTextTemplate("ArticleID", DataControlRowType.DataRow);
tf.FooterTemplate = new DynamicGridViewTextTemplate(DataControlRowType.Footer, ds.Tables[i].Rows.Count);
gvDynamicArticle.Columns.Add(tf);
tf = new TemplateField();
tf.HeaderTemplate = new DynamicGridViewTextTemplate("Title", DataControlRowType.Header);
tf.ItemTemplate = new DynamicGridViewTextTemplate("Title", DataControlRowType.DataRow);
gvDynamicArticle.Columns.Add(tf);
tf = new TemplateField();
tf.HeaderTemplate = new DynamicGridViewTextTemplate("Description", DataControlRowType.Header);
tf.ItemTemplate = new DynamicGridViewTextTemplate("Description", DataControlRowType.DataRow);
gvDynamicArticle.Columns.Add(tf);
tf = new TemplateField();
tf.HeaderTemplate = new DynamicGridViewURLTemplate("Title", "URL", DataControlRowType.Header);
tf.ItemTemplate = new DynamicGridViewURLTemplate("Title", "URL", DataControlRowType.DataRow);
gvDynamicArticle.Columns.Add(tf);
tf = new TemplateField();
tf.HeaderTemplate = new DynamicGridViewTextTemplate("Author", DataControlRowType.Header);
tf.ItemTemplate = new DynamicGridViewTextTemplate("CreatedBy", DataControlRowType.DataRow);
gvDynamicArticle.Columns.Add(tf);
gvDynamicArticle.DataSource = ds.Tables[i];
gvDynamicArticle.DataBind();
phDynamicGridHolder.Controls.Add(gvDynamicArticle);
}
}

}

}
public class DynamicGridViewTextTemplate : ITemplate
{
string _ColName;
DataControlRowType _rowType;
int _Count;

public DynamicGridViewTextTemplate(string ColName, DataControlRowType RowType)
{
_ColName = ColName;
_rowType = RowType;
}
public DynamicGridViewTextTemplate(DataControlRowType RowType, int ArticleCount)
{
_rowType = RowType;
_Count = ArticleCount;
}
public void InstantiateIn(System.Web.UI.Control container)
{
switch (_rowType)
{
case DataControlRowType.Header:
Literal lc = new Literal();
lc.Text = "" + _ColName + "";
container.Controls.Add(lc);
break;
case DataControlRowType.DataRow:
Label lbl = new Label();
lbl.DataBinding += new EventHandler(this.lbl_DataBind);
container.Controls.Add(lbl);
break;
case DataControlRowType.Footer:

Literal lc1 = new Literal();
lc1.Text = " ";
container.Controls.Add(lc1);
break;
default:
break;
}
}
private void lbl_DataBind(Object sender, EventArgs e)
{
Label lbl = (Label)sender;
GridViewRow row = (GridViewRow)lbl.NamingContainer;
lbl.Text =DataBinder.Eval(row.DataItem, _ColName).ToString();
}

}
public class DynamicGridViewURLTemplate : ITemplate
{
string _ColNameText;
string _ColNameURL;
DataControlRowType _rowType;

public DynamicGridViewURLTemplate(string ColNameText, string ColNameURL, DataControlRowType RowType)
{
_ColNameText = ColNameText;
_rowType = RowType;
_ColNameURL = ColNameURL;
}
public void InstantiateIn(System.Web.UI.Control container)
{
switch (_rowType)
{
case DataControlRowType.Header:
Literal lc = new Literal();
lc.Text = "" + _ColNameURL + "";
container.Controls.Add(lc);
break;
case DataControlRowType.DataRow:
HyperLink hpl = new HyperLink();
hpl.Target = "_blank";
hpl.DataBinding += new EventHandler(this.hpl_DataBind);
container.Controls.Add(hpl);
break;
default:
break;
}
}
private void hpl_DataBind(Object sender, EventArgs e)
{
HyperLink hpl = (HyperLink)sender;
GridViewRow row = (GridViewRow)hpl.NamingContainer;
hpl.NavigateUrl = DataBinder.Eval(row.DataItem, _ColNameURL).ToString();
hpl.Text = "
" + DataBinder.Eval(row.DataItem, _ColNameText).ToString() + "
";

}