Monday, August 16, 2010

Difference between ASP and ASP.Net

ASP session state is dependent on IIS process very heavily. So if IIS restarts ASP session variables are also recycled

ASP.NET session can be independent of the hosting environment thus ASP.NET session can maintained even if IIS reboots.

ASP session only works when browser supports cookies

ASP.NET session can be used with browser side cookies or independent of it.

ASP session state has no inherent solution to work with Web Farms

ASP.NET session can be stored in state server and SQL SERVER which can support multiple servers.

ASP is interpreated

ASP.Net is complied

ASP uses ADO

ASP.Net uses ADO.Net

ASP, there was no server controls. You have to write all html tags manually

ASP.NET offers a very rich set of controls called Server Controls and Html Controls. It is very easy to drag and drop any controls to a web form

ASP uses vb script for server side coding. Vb Script is not supported any more in ASP.NET

nstead, ASP.NET supports more languages including C#, VB.NET, J# etc.

ASP did not support Page level transactions

SP.NET supports Page level transactions

ASP does not offer support for Web Services and rich data structures like DataSet which allows disconnected data processing.

ASP.NET offers support for Web Services and rich data structures like DataSet which allows disconnected data processing.

ASP had maximum of 4 built in classes like Request, Response, Session and Application

ASP.NET using .NET framework classes which has more than 2000 in built classes

ASP has Mixed HTML and coding logic

Separate code and design logic possible

Limited development and debugging tools available

Variety of compilers and tools available including the Visual studio.Net

No in-built support for XML

Full XML Support for easy data exchange

No fully distributed data source support.

Fully distributed data source support

Poor Error handling system

Full proof error handling possible

Limited OOPS support

Completely Object Oriented.

Sunday, August 8, 2010

How to freeze Gridview columns in ASP.net?

I am writing below blog from one of my last project experience. I was doing a project called timesheet where i was in need of showing lots of details of the user whose timesheet is been submitted. For Example: Name, DOB, Department, Type, Designation and all the 30/31 days of the months.

Since it became difficult to show all the information in gridview becuase there is no built in functionality of scrolling. So I thought of freezing some of the columns of the gridview and keeping all other columns as it is.

I used CSS to handle this.

Find below the html:

<style type="text/css">

.Container

{

overflow: auto;

}

.FreezeCell

{

background-color:#d9e9ff;

position: relative;

font-family:Segoe UI;

font-size:11px;

font-weight:normal;

cursor: auto;

left: expression(document.getElementById("GridView1").scrollLeft-1);

}

.FreezeHeader

{

background-color:#d9e9ff;

font-weight:normal;

font-family:Segoe UI;

font-size:11px;

position: relative;

cursor: auto;

top: expression(document.getElementById("GridView1").scrollTop-1);

z-index: 10;

}

.FreezeHeader.locked

{

z-index: 99;

}

style>

<div id="div1" class="Container" style="width: 200px; color: #4977bc;

border-style: solid; border-width: thin;" runat="server">

<asp:GridView ID="GridView1" CssClass="GridView" runat="server" Width="250px"

GridLines="Both" BorderWidth="1px" AutoGenerateColumns="false">

<HeaderStyle BackColor="#d9e9ff" Font-Bold="false" Font-Names="Segoe UI" Font-Size="11px" />

<Columns>

<asp:BoundField DataField="name" HeaderText="Name" HeaderStyle-Width="100px" ItemStyle-CssClass="FreezeCell"

HeaderStyle-CssClass="FreezeCell">

<ItemStyle VerticalAlign="Top" BackColor="#ffffff" Width="100px"/>

asp:BoundField>

<asp:BoundField DataField="age" HeaderText="Age" HeaderStyle-Width="50px" >

<ItemStyle VerticalAlign="Top" Width="50px" HorizontalAlign="center" BackColor="#ffffff" />

asp:BoundField>

<asp:BoundField DataField="sex" HeaderText="Sex" HeaderStyle-Width="50px"

HeaderStyle-HorizontalAlign="left">

<ItemStyle VerticalAlign="Top" Width="50px" HorizontalAlign="left" BackColor="#ffffff" />

asp:BoundField>

<asp:BoundField DataField="datetime" HeaderText="DateTime" HeaderStyle-Width="15px"

HeaderStyle-HorizontalAlign="left">

<ItemStyle VerticalAlign="Top" Width="150px" HorizontalAlign="left" BackColor="#ffffff" />

asp:BoundField>

Columns>

asp:GridView>

div>

C# Source Code:

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;

using System.Collections.Generic;

public class PersonInfo

{

private string name;

public string Name

{

get { return name; }

set { name = value; }

}

private string age;

public string Age

{

get { return age; }

set { age = value; }

}

private string sex;

public string Sex

{

get { return sex; }

set { sex = value; }

}

private DateTime datetime;

public DateTime DateTime

{

get { return datetime; }

set { datetime = value; }

}

}

public partial class _Default : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

List<PersonInfo> listPersonInfo = new List<PersonInfo>();

for (int i = 0; i <>

{

PersonInfo p = new PersonInfo();

p.Name = "Ashraf" + i.ToString();

p.Age = "23" + i.ToString();

p.Sex = "Male";

p.DateTime = DateTime.Now;

listPersonInfo.Add(p);

}

GridView1.DataSource = listPersonInfo;

GridView1.DataBind();

}

}

}

This works perfectly for me.

Saturday, August 7, 2010

How to open a dll file?

Recently I was working for a Tool and I was bit familiar and sure that existing dll files can be open to view their source code. I knew there are some tools which help you to do that.

Note: This is only possible if dll not obfuscated.

Obfuscation: It is a process that makes it difficult to reengineer the code. Obfuscating is a way to hide your assembly or EXE code from reflection.

Obfuscation simple meaning is making something difficult to read, understand and interpret. Obfuscation leads to doing something intentionally ambogious and confusing.

The same applies to .Net dll files. We know that .Net assemblies can be open to know what inside using reflector tools provided. So it is the need of individuals or personnel to protect their work that nobody copies or understand.The obfuscation alogorthims may use one of the following approaches.

  • Changing the variables, functions and method name.
  • Using Encoding
  • Using encyrption and dycryption techniques.

I will demonstrate this proces using a tool Red Gat’s .Net Reflector.You can download from here: http://www.red-gate.com/products/reflector/

Step1: Run the tool

Go to File->Open-> Browse the dll file to open. Here I have selected PM.dll file

Step2: Explore your methods, classes, properties to see the code

There are many tools availble like .Net Reflector 2.4 etc. You can download to look into your dll files.

I hope you enjoyed this, but at the same time you should also understand how can you protect your dll not to be open by anyone else if they values.

Tuesday, August 3, 2010

Static variables Versus Session variables

Static variables Versus Session variables:

Let me explain you something which I came across in a project when it went to production. I was working for a project called timesheet and during its code implementation I used some static variables because I was in confident of the following things:

1) Static variables persist there value and there scope is within the session.

2) Static variables are destroyed once the session of the user is destroyed.

I was very sure that static variables will persist values for different function calls and they will not be availble across sessions. This made me very unabashed for sometime because many production issues were reported by users while they were submitting their timesheet request. One of my senior asked me, did you use static variables in you code? I said yes, and then he immediately took attention and asks me to go through the code and remove all static variables until unless they are to be used across different user’s session. Use session instead of static variables if information is user specific.

Static variables: We know that static variables persists values between different function call and it is very true. The nature of static variables in C# is at application level and you know that application level is always higher than session.

Static variables are visible and persists their values across different user’s sessions. They are intialize as soon as the application start running on IIS and they remain till IIS server (in case of ASP.net application) is reset.

Session variables: Session variables scope is below application level. A web application running can have one or more user’s session running. The session is destroyed explicitly or when the client browser is closed.

Note that having one browser open and multiple tab is been treated as same session. Different instance of browser will have different sessions for you.

Sunday, August 1, 2010

How to read Windows Registry

Sometime we are required to read windows registry values in our applications. It is quite simple just 3-4 lines of code make you life easy while reading from registry.

Here we go.

I am sure you know how to Add Registry Data, lets take an example here.

Step1: Add a new Registry data, for that I will add connection string settings to my windows registry.

/*Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\MyApplication\DAL]

"ConnectionString"="Persist Security Info=False;User ID=UserId;Initial Catalog=MyDatabase; Password=Password;Data Source=MyServer"*/

This will add a Registry value of “String Value” to my Registry.

Step2: Step 2 includes how you read the value of the registry by passing the key name.

Here first of all we need to include the name space

using Microsoft.Win32

Here is the function that return the value:

public string GetConnectionString()

{

RegistryKey connkey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\MyApplication\DAL");

if (connkey != null)

{

object keyvalue = connkey.GetValue("ConnectionString");

return keyvalue.ToString();

}

throw new Exception("ConnectionString Connection string is not set in Windows Registry");

}

This way you get the connection string value set in the registry. I hope you got the way it needs to be achieved.

Thanks and please share your valuable comments.

Sunday, July 18, 2010

Report Viewer - Hide PDF Export

I always like to share simple but interesting solutions to the users who are in urgent need of fixing it.Here I am going to share, how one can achieve hiding some of the options available from the Export options of Report Viewer from SQL Reporting Services.

I did the following to achieve this.

Steps:

1. Go to file "rsreportserver.config".

This file you can find in location "C:\Program Files\Microsoft SQL Server\MSSQL.1\Reporting Services\ReportServer"

2. Go to the section <Render>.

3. Add the Visible="false" attribute to the end of each Extension tag to which you wish to hide.

For Example:

Here I am hiding PDF option and keeping all other options enabled.

<Extension Name="PDF" Type="Microsoft.ReportingServices.Rendering.ImageRenderer.PdfReport,Microsoft.ReportingServices.ImageRendering" Visible="false"/>

After applying these settings you will see only Excel option enabled.

Note: Since these settings are applied to the report server configuration file so changes will be reflected to all reports.

:) Is it not easy?

You comments are always appreciated.