Showing posts with label ASP.NET. Show all posts
Showing posts with label ASP.NET. Show all posts

Thursday, November 5, 2009

Memory Profiler.

This is the tool which will find out memory leakage. using this we can fine tune the code.

download: http://memprofiler.com/

Wednesday, October 28, 2009

Health monitoring

Health monitoring in the Web.config file.



This is nice feature in the dotnet frame work 3.5


it will automatically write the event log, When you will not handle the exception in the application,


<system.web>
<healthMonitoring enabled="true"></healthMonitoring>
</system.web>

Thursday, October 22, 2009

how to restrict back button using asp.net.

We have come across the problem while clicking on the back button in the browser.

we shouldn't load the page again when we click on the back button.

Below code it will hide the back button. This is also one of the security leavl have to

follow in the transaction pages.

Response.Buffer = true;
Response.CacheControl = "no-cache";
Response.Cache.SetNoStore();
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.Now.AddDays(-1));

Friday, August 21, 2009

Asp DataGrid with Editable cell with out Postback using Javascript

This is created for one of my friend. he want's in his project. so i thought of sharing with everyone am writing in my blog.

step 1:

create asp grid control with binding dummy value.

step 2:

<asp:GridView ID="grView" runat="server" AutoGenerateColumns="false"
onprerender="grView_PreRender">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<div id="lblDiv" runat="server">
<asp:Label ID="lblID" runat="server" Text='<%# Eval("Empid") %>'></asp:Label>
</div>
<div id="txtDiv" style="display:none" runat="server">
<asp:TextBox ID="txtID" runat="server" Text='<%# Eval("Empid") %>'></asp:TextBox>
</div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

step 3:

DataTable DT = new DataTable();
DataColumn dc1 = new DataColumn("Empid");
DataColumn dc2 = new DataColumn("Empname");
DataColumn dc3 = new DataColumn("EmpSalary");
DataColumn dc4 = new DataColumn("EmpAddress");
DT.Columns.Add(dc1);
DT.Columns.Add(dc2);
DT.Columns.Add(dc3);
DT.Columns.Add(dc4);
DataRow DR;
for (int i = 0; i <>
{
DR = DT.NewRow();
DR["Empid"] = i.ToString();
DR["Empname"] = "Bala";
DR["Empid"] = "Salary";
DR["Empid"] = "Address";
DT.Rows.Add(DR);
}
grView.DataSource = DT;
grView.DataBind();

step 4:

Calling javascript for the cell through.

protected void grView_PreRender(object sender, EventArgs e)
{
foreach (GridViewRow dr in grView.Rows)
{
Label lbl = (Label)dr.FindControl("lblID");
lbl.ID = "lblID";
lbl.Attributes.Add("onclick", "javascript:lbltoTxt(this)");
TextBox txt = (TextBox)dr.FindControl("txtID");
txt.Attributes.Add("onblur", "javascript:txttoLbl(this)");
}
}

Step 5:
<script language="javascript">
function lbltoTxt(obj)
{
var divtxtObj = document.getElementById(obj.id.replace('lblID', 'txtDiv'));
var txtObj = document.getElementById(obj.id.replace('lblID', 'txtID'));
if (divtxtObj != null)
{
if (divtxtObj.style.display == "none")
{
divtxtObj.style.display = "block";
txtObj.focus();
}
}
var divlblObj = document.getElementById(obj.id.replace('lblID', 'lblDiv'));
if (divlblObj != null)
{
if (divlblObj != null)
{
divlblObj.style.display = "none"
}
}
}
function txttoLbl(obj) {
var divlblObj = document.getElementById(obj.id.replace('txtID', 'lblDiv'));
var lblObj = document.getElementById(obj.id.replace('txtID', 'lblID'));
if (divlblObj != null) {
if (divlblObj.style.display == "none") {
divlblObj.style.display = "block";
lblObj.innerText = obj.value;
}
}
var divtxtObj = document.getElementById(obj.id.replace('txtID', 'txtDiv'));
if (divtxtObj != null) {
if (divtxtObj != null) {
divtxtObj.style.display = "none"
}
}
}
</script>






Sunday, June 8, 2008

Adding rows in DataGrid

DataTable dtg = (DataTable) DataGrid1.DataSource;

DataRow dr = dtg.NewRow();

dr["Item_ID"]="ItemID";
dr["Item_Name"]="Item Name";
dr["Item_Desc"]="Item Description";
dr["Item_Amount"]="$15.65";

dtg.Rows.Add(dr);

DataGrid1.DataSource = dtg;

It will work for windows.

If you use DataGrid in ASP then you will have to keep DataTable in Session:

DataTable dtg = new DataTable();

if (Session["SampleDataTable"]==null)
{

DataColumn dc;
dc = new DataColumn("Item_ID",System.Type.GetType("System.String"));
dtg.Columns.Add(dc);
dc = new DataColumn("Item_Name",System.Type.GetType("System.String"));
dtg.Columns.Add(dc);
dc = new DataColumn("Item_Desc",System.Type.GetType("System.String"));
dtg.Columns.Add(dc);
dc = new DataColumn("Item_Amount",System.Type.GetType("System.String"));
dtg.Columns.Add(dc);

}
else {dtg = (DataTable) Session["SampleDataTable"];}

DataRow dr = dtg.NewRow();
dr["Item_ID"]="ItemID";
dr["Item_Name"]="Item Name";
dr["Item_Desc"]="Item Description";
dr["Item_Amount"]="$15.35;
dtg.Rows.Add(dr);

DataGrid1.DataSource=dtg;
DataGrid1.PageSize+=1;
DataGrid1.DataBind();
Session["SampleDataTable"]=dtg;

Monday, April 7, 2008

ASP.NET 2.0 MultiView Control

ASP.NET 2.0 Provies a wonderful solution by means of MultiView Control. The MultiView Control combined with the View Control can provide different UI on different scenarios as and when required.

<asp:MultiView ID="MultiView1" runat="server" >
<asp:View ID="View1" runat="server">This is one section where you can have a set of controls / contents<asp:View>
<asp:View ID="View2" runat="server">This is another section where you can have a different set of controls / contents<asp:View>
</asp:MultiView>

Now, to show the different tabs based on different events, we just need to set the following code:-

protected void Button1_Click(object sender, EventArgs e)
{
MultiView1.SetActiveView(View1);
}
protected void Button2_Click(object sender, EventArgs e)
{
MultiView1.SetActiveView(View2);
}

Multiview control using

"goto" Using Dotnet framework 2.0

for (int k = 0; k < 2; k++)
{
Response.Write("Testing Goto");
goto Testing;
Testing:
Response.Write("Testing Goto in for loop");
}

output
Testing GotoTesting Goto in for loop

Monday, February 25, 2008

Encryption connectionstring .NET Framework 2.0 using Tool

Configuration File Encryption

In the .NET Framework 2.0, developers will be able to encrypt sensitive parts of the web.config file (if containing password or keys, for instance) using the aspnet_regiis utility. The decryption is done transparently.

The DPAPI protected configuration provider supports machine-level and user-level stores for key storage. The choice of store depends largely on whether or not the application shares state with other applications and whether or not sensitive data must be kept private for each application.

If the application is deployed in the Web farm scenario, developers should use RSAProtectedConfigurationProvider to leverage the ease with which RSA keys can be exported on multiple systems. It uses RSA public key cryptography to provide data confidentiality.


The following example encrypts the connection string section using the tool aspnet_regiis:

try out this...

aspnet_regiis.exe -pef "connectionStrings" C:\VirtualDirectory\Path

URL Mapping in asp.net 2.0

URL Mapping is a mechanism by which you can change the displayed url in address bar.

Example

Step1: Add Mapping URL in web.config file.
<system.web>
<urlMappings enabled="true">
<add url="~/Department.aspx" mappedUrl=" oldforms/frmDept.aspx"/>
<add url="~/Employee.aspx" mappedUrl=" oldforms/frmEmployee.aspx"/>
<add url="~/Product.aspx" mappedUrl="& gt;
</urlMappings></system.web>

Step2: Change the URL in .aspx file
<a href="Department.aspx">Department</a><br />
<a href="Product.aspx">Product</a><br />
<a href="Employee.aspx">Employee</a>

Wednesday, January 16, 2008

Datatable Load option in Dotnet Framework 2.0

Definition

Using datatable.load you can load the sqldatareader value directly

Example


DataTable dt = new DataTable();
string connectionstring = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
SqlConnection myconn = new SqlConnection(connectionstring);
SqlCommand mycomm = new SqlCommand("select * from FA_contactdetails", myconn);
myconn.Open();
SqlDataReader sqldataread = mycomm.ExecuteReader();
dt.Load(sqldataread);////
myconn.Close();
dggrid.DataSource = dt;
dggrid.DataBind();

Thursday, December 13, 2007

Sending Email using "System.Net.Mail"

<%@ Import Namespace="System.Net.Mail" %>
void Page_Load()
{
//SmtpClient client = new SmtpClient();
//client.Host = "localhost";
//client.Port = 25;
//client.Send("steve@somewhere", "bob@somewhere.com", "Let's eat lunch!", "Lunch at the Steak House?");
}

Wednesday, October 17, 2007

Connection Pooling in ASP.NET

Connection pooling increases the performance of Web applications by reusing active database connections instead of creating a new connection with every request. Connection pool manager maintains a pool of open database connections. When a new connection is requested , pool manager checks if the pool contains any unused connections and returns connection if available.



If all connections in the pool are busy and the maximum pool size has not been reached, then new connection is created and added to the pool. When the pool reaches its maximum size all new connection requests are being queued up until a connection in the pool becomes available or the connection attempt times out.



These are four parameters that control most of the connection pooling behavior

Max Pool Size - the maximum size of your connection pool. Default is 100

Min Pool Size - initial number of connections which will be added to the pool upon its creation. Default is zero

Connect Timeout - controls the wait period in seconds when a new connection is requested, if this timeout expires, an exception will be thrown. Default is 15 seconds.

Pooling - controls if your connection pooling on or off. Default as you may've guessed is true. Read on to see when you may use Pooling=false setting.

eg.

connstring="server=myserver;database;abcdefg;Min pool size=5;Max pool size=100;connection timeout=15;pooling=yes"


Most of the Connection problems are because of Connection Leaks

SqlConnection conn=new SqlConnection(constring);
conn.Open();
//do some thing
conn.Close();

while executing the functionality if Exception occurences, then for sure connection wont be closed , to close connection explicitly .. the simple way is ..

SqlConnection conn=new SqlConnection(constring);

try{
conn.Open();
//do some thing
}
finally()
{
conn.Close();

Wednesday, October 10, 2007

Security

Allowing or Deniying access to specfic users

when the application uses windows authentication। Asp।Net checks the project's web।config authorization list to see which network users are allowed to access the application. The asterstick(*) and question mark(?) characters have some special meaning in the autorization.



* Charatcter indicates all users
? character indicates unauthenticated users.

Example:

Monday, October 8, 2007

How to create an array of Web Controls

How to create an array of Web Controls

TextBox[] textboxes = new TextBox[5];
for (int i=0; i<5; i++)
{
textboxes[i] = new TextBox();
textboxes[i].ID = "TextBox" + i;
textboxes[i].AutoPostBack = true;
PlaceHolder1.Controls.Add(textboxes[i]);
}

Clear Textbox

How to clear all the textboxes in my form


foreach (Control ctl in Page.Controls[1].Controls )
{
TextBox tb = ctl as TextBox;
if (tb!=null)
{
tb.Text = "" ;
}
}

Config files

1 What is the best place to store Database connection string?

In web.config with in appsetting you have to write the connection string..

Sunday, September 23, 2007

HTML enhancements in ASP.Net 2.0 - PART 2

HTML enhancements in ASP.Net 2.0 - PART 2

PART 1 of this article dealt with accessing head tag in codebehind. This part will deal with some more additional enhancements in 2.0 that is really useful.

Accessing <link tag&rt; :

The link tag is useful to register a CSS file with our Webpage. It can be accessed in codebehind or can be added through codebehind by the following code.

HtmlLink cssLink = new HtmlLink();
cssLink.Href = "StyleSheet.css";
cssLink.Attributes.Add("rel", "stylesheet");
cssLink.Attributes.Add("type", "text/css");
Page.Header.Controls.Add(cssLink);

This will inject a <link&rt; in the html output inside the tag.
<link href="StyleSheet.css" rel="stylesheet" type="text/css" /&rt;
Accessing <Meta&rt; tag :

Meta tags can be used to construct search engine friendly websites, can be used to refresh page and many more. In 2.0 we can achieve this from codebehind through which it gives
the capability to add in runtime.

The following code will do it,

HtmlHead head = (HtmlHead)Page.Header;

HtmlMeta metasearch1 = new HtmlMeta();
HtmlMeta metasearch2 = new HtmlMeta();
metasearch1.Name = "descriptions";
metasearch1.Content = "my personal site";
head.Controls.Add(metasearch1);
metasearch2.Name = "keywords";
metasearch2.Content = "ASP.Net,C#,SQL";
head.Controls.Add(metasearch2);

HtmlMeta metarefresh = new HtmlMeta();
metarefresh.HttpEquiv = "refresh";
metarefresh.Content = "5";
head.Controls.Add(metarefresh);

The following html tag will be injected in the HTML output.

Adding clientside Reset button:

Some time we will need to reset the webpage in clientside which in 2.0 can be done dynamically or in codebehind. The following code will will add a reset input element in the output html.

HtmlInputReset reset = new HtmlInputReset();
reset.ID = "ResetButton";
reset.Value = "Reset";
PlaceHolder1.Controls.Add(reset);

The above code will add a reset tag like below,
<input name="ResetButton" type="reset" id="ResetButton" value="Reset" />

This class is new in 2.0 while this also can be achieved thro HtmlInputButton class in 1.x.