Saturday, October 29, 2011

What is an application object and where we use application object in asp.net | application object sample using asp.net


What is an application object?

Application object is used to store the information and access variables from any page in application. Application object is same as session object only the difference is session object is used to maintain the session for particular user. If one user enters in to the application then session id will create for that particular user if he leaves from the application then the session id will deleted.  they will get different session id but application object is same for all users once application object is created that application object is used throughout the application regardless of user. The information stored in application object accessed throughout all the pages in application (like database connection information) and we can change the application object in one place those changes automatically reflected in all the pages.

Thursday, April 29, 2010

How to Make by pressing Enter Key ASP Button fires Click Event using JQUERY

<div class="headerSearch formPanel" formButtonId="<%=SearchButton1.ClientID %>">

<label for="txtSearch">
Product Search:</label>
<input type="text" id="TextBox1" class="text" value="Enter Keyword" runat="server"
onfocus="clearText(this.id)" />
<div class="button buttonBlack">
<asp:LinkButton ID="SearchButton1" runat="server" OnClientClick="return ProductSearch();"
OnClick="SearchButton1_Click" Text="<span></span>Go" />
</div>
</div >


Include this JQUERY in your ASPX page


$(function(){
$("div.formPanel").each(function()
{
if($(this).attr("formButtonId"))
{
$(this).bind("keypress", function(e){
if (e.keyCode == 13)
{
e.returnValue = false;
e.cancel = true;
var button = $("#" + $(this).attr("formButtonId"));

var retValue = button.triggerHandler("click");
if(retValue == undefined || retValue == true)
eval(button.attr("href"));

return false;
}
});
}
});
});

Tuesday, March 2, 2010

Creating Log File

actually keep this piece of code in a class called util. so I'm just going to paste the function and also the setting in the config file.

Setting in Config File

<add key="FilePath" value="D:\logFile.txt"/>

public static void function in util.cs

public static void writeToLogFile(string logMessage)
{
string strLogMessage = string.Empty;
string strLogFile = System.Configuration.ConfigurationManager.AppSettings["logFilePath"].ToString();
StreamWriter swLog;

strLogMessage = string.Format("{0}: {1}", DateTime.Now, logMessage);

if (!File.Exists(strLogFile))
{
swLog = new StreamWriter(strLogFile);
}
else
{
swLog = File.AppendText(strLogFile);
}

swLog.WriteLine(strLogMessage);
swLog.WriteLine();

swLog.Close();

}

Wednesday, February 24, 2010

Introduction


OOPS or Object Oriented Programming Concepts but some of the concepts have been always used in one or the other programming languages. For example you must have used structs in C which is a good example of encapsulation. There are four major pillar of OOPS. Let’s try to understand each one of them by taking some examples also:-
1.) Encapsulation
2.) Abstraction
3.) Polymorphism
4.) Inheritance

Encapsulation:-

Interview Definition:- Binding data and member functions together inside a single unit.
How to Encapsulate:- By creating types e.g Classes and Struct
Bu using encapsulation we can create out own custom types by reusing the existing or primitive types.

Abstraction:-

Abstraction defines way to abstract or hide your data and members from outside world. Simply speaking Abstraction is hiding the complexities of your class or struct or in a generic term Type from outer world. This is achieved by means of access specifiers.
Interview Definition: - Hiding the complexities of your type from outside world.

How to Abstract: - By using Access Specifiers
.Net has five access specifiers:-
Public -- Accessible outside the class through object reference.
Private -- Accessible inside the class only through member functions.
Protected -- Just like private but Accessible in derived classes also through member functions.
Internal -- Visible inside the assembly. Accessible through objects.
Protected Internal -- Visible inside the assembly through objects and in derived classes outside the assembly through member functions.
Let’s try to understand by a practical example:-
Interview Tip:-
The default access specifier for a class in internal. Mind it I mean class not class’s data members.

public class Class1
{
int i; //No Access specifier means private
public int j; // Public
protected int k; //Protected data
internal int m; // Internal means visible inside assembly
protected internal int n; //inside assembly as well as to derived classes outside assembly
static int x; // This is also private
public static int y; //Static means shared across objects
[DllImport("MyDll.dll")]
public static extern int MyFoo(); //extern means declared in this assembly defined in some other assembly
public void myFoo2()
{
//Within a class if you create an object of same class then you can access all data members through object reference even private data too
Class1 obj = new Class1();
obj.i =10; //Error can’t access private data through object.But here it is accessible.:)
obj.j =10;
obj.k=10;
obj.m=10;
obj.n=10;
// obj.s =10; //Errror Static data can be accessed by class names only
Class1.x = 10;
// obj.y = 10; //Errror Static data can be accessed by class names only
Class1.y = 10;
}
}

//Now lets try to copy the same code inside Main method and try to compile
[STAThread]
static void Main()
{
//Access specifiers comes into picture only when you create object of class outside the class
Class1 obj = new Class1();
// obj.i =10; //Error can’t access private data through object.
obj.j =10;
// obj.k=10; //Error can’t access protected data through object.
obj.m=10;
obj.n=10;
// obj.s =10; //Errror Static data can be accessed by class names only
Class1.x = 10; //Error can’t access private data outside class
// obj.y = 10; //Errror Static data can be accessed by class names only
Class1.y = 10;
}
//What if Main is inside another assembly
[STAThread]
static void Main()
{
//Access specifiers comes into picture only when you create object of class outside the class
Class1 obj = new Class1();
// obj.i =10; //Error can’t access private data through object.
obj.j =10;
// obj.k=10; //Error can’t access protected data through object.
// obj.m=10; // Error can’t access internal data outside assembly
// obj.n=10; // Error can’t access internal data outside assembly

// obj.s =10; //Errror Static data can be accessed by class names only
Class1.x = 10; //Error can’t access private data outside class
// obj.y = 10; //Errror Static data can be accessed by class names only
Class1.y = 10;
}

Tuesday, February 9, 2010

Thursday, February 4, 2010

Page Refresh Problem

A common problem that Web Application developers encounter is how to stop the user from refreshing the page. The problem arises, if the previous request to the server was a PostBack, which, for example, inserts the WebForm's data into a database. This will result in the addition of duplicate rows in the database. But we have a constraint that we can't stop the user by refreshing the page. So, what to do? Although we can't stop the user from refreshing the page, but we can determine if this event has already occurred and then take appropriate action.

My strategy will make use of the ViewState feature. As we are using ViewState, it would seem logical to perform the operation in the LoadViewState and SaveViewState methods. Using these two methods, instead of the OnLoad method, has more benefits in that it eliminates the potential problems of sub-classes implementing Page_Load. methods follows:

public class Refresh : System.Web.UI.Page

{
private bool _refreshState;
private bool _isRefresh;
public bool IsRefresh

{

get

{

return _isRefresh;

}

}

protected override void LoadViewState(object savedState)

{

object[] allStates = (object[])savedState;

base.LoadViewState(allStates[0]);

_refreshState = (bool)allStates[1];

_isRefresh = _refreshState == (bool)Session["__ISREFRESH"];


}



protected override object SaveViewState()

{

Session["__ISREFRESH"] = _refreshState;

object[] allStates = new object[2];

allStates[0] = base.SaveViewState();

allStates[1] = !_refreshState;

return allStates;

}

}

Thursday, September 24, 2009

Reverse Data Table

This Method is used the Reverse the given input DataTable

For Eample,

Input Data Table has the following Records,

NAME TITLE DEV_IN SCOST DCOST SOLD
-------- -------------------- -------- ---------- ---------- ----------
RAMESH HOTEL MANAGEMENT DBASE 12000 35000 4
RAMESH DEAD LEE PASCAL 99.95 4500 73

public static DataTable ReverseDataTable(DataTable dataTable)
{
DataTable table = new DataTable();
DataRow[] rowArray = dataTable.Select();
table = dataTable.Clone();
for (int i = rowArray.Length - 1; i >= 0; i--)
{
table.ImportRow(rowArray[i]);
}
return table;
}

OutPut

NAME TITLE DEV_IN SCOST DCOST SOLD
-------- -------------------- -------- ---------- ---------- ----------
RAMESH DEAD LEE PASCAL 99.95 4500 73
RAMESH HOTEL MANAGEMENT DBASE 12000 35000 4