Tuesday, July 31, 2012

How to get RowIndex on GridViewRowcommand Event

.aspx page
CommandArgument='<%# DataBinder.Eval(Container, "RowIndex") %>'


.CS page (on row commond)
int iRowIndex = Convert.ToInt32(e.CommandArgument.ToString())
OR
Dim iRowIndex As Integer
iRowIndex = CType(CType(sender, Control).Parent.Parent, GridViewRow).RowIndex

Thursday, May 17, 2012

Get date diff in Year, Months, and Days in oracle

with t as (select to_date('30-JAN-1995') as date1
                     ,sysdate as date2 from dual)
                    
    select
      trunc((date2-date1)/365) as Years,
      trunc((trunc(date2-date1) - trunc((date2-date1)/365) * 365)/30) as Months,
      trunc((date2-date1) - ((trunc((trunc(date2-date1) - trunc((date2-date1)/365) * 365)/30) *30 ) + (trunc((date2-date1)/365) * 365))) days
   from t

Differences among Int32.Parse, Covert.ToInt32, and Int32.TryParse

Int32.Parse (string )

It converts the string representation of a number to its 32-bit signed integer equivalent.
When s is a null reference, it will throw ArgumentNullException. If s is other than integer value, it will throw FormatException.
When s represents a number less than MinValue or greater than MaxValue, it will throw OverflowException.

For example:

string n1 = "3233";
string n2 = "3233.33";
string n3 = null;
string n4 = "12340056789123456789123456789123456789123456789";

int result;
bool success;
result = Int32.Parse(n1); //-- 3233
result = Int32.Parse(n2); //-- FormatException
result = Int32.Parse(n3); //-- ArgumentNullException
result = Int32.Parse(n4); //-- OverflowException

Convert.ToInt32(string)
It converts the specified string representation of 32-bit signed integer equivalent.
This calls in turn Int32.Parse () method. When s is a null reference, it will return 0 rather than throw ArgumentNullException.
If s is other than integer value, it will throw FormatException. When s represents a number less than MinValue or greater than MaxValue,
it will throw OverflowException.

For example:

result = Convert.ToInt32(n1); //-- 3233
result = Convert.ToInt32(n2); //-- FormatException
result = Convert.ToInt32(n3); //-- 0
result = Convert.ToInt32(n4); //-- OverflowException

Int32.TryParse(string, out int)
It method converts the specified string representation of 32-bit signed integer equivalent to out variable,
and returns true if it is parsed successfully, false otherwise. This method is available in C# 2.0. When s is a null reference,
it will return 0 rather than throw ArgumentNullException. If s is other than an integer value, the out variable will have 0 rather
than FormatException. When s represents a number less than MinValue or greater than MaxValue, the out variable will have 0 rather than
OverflowException.

For example:

 success = Int32.TryParse(s1, out result); //-- success => true; result => 3233
 success = Int32.TryParse(s2, out result); //-- success => false; result => 0
 success = Int32.TryParse(s3, out result); //-- success => false; result => 0
 success = Int32.TryParse(s4, out result); //-- success => false; result => 0

Int32.TryParse is better than that of Int32.Parse and Convert.ToInt32.
Convert.ToInt32 is better than Int32.Parse.

Saturday, January 21, 2012

Disabling the Back Button


When i was doing coding for sign in and sign out for my clients application, i found that after signing out from the application i transfered the control to login page e.g. login.aspx. At this point if i click the Back button of Browser it shows the content of previous page user was viewing.

As there was important data displayed on page it is security threat.

It is a threat for web applications displaying important information like credit card numbers  or bank account details.

I tried to find the solution on net but could not get satisfactory answer.

On searching i found following problem, and i think it is important to share this issue-

What happens when Back Button clicked

When we visit a page it is stored in a cache i.e. history on a local machine. Whenever user clicks the Back button, previous page is taken from this cache and displayed; request does not go to the server to check the login information as page is found on local cache. If we submit the page or refresh the page then only page is sent to the server side.

Caching-

Caching of Web Pages can happen in three separate entities in a Web environment.
When you think about caching, you usually think about the Web pages cached locally in your temporary Internet files of the profile that was used to log into local machine as a result of having visited the page. But caching can also occur within the Internet Information Server (IIS) Server, and If a proxy server is present, it can be configured to cache the pages.

Solution-

To avoid the displaying of page on click of back button we have to remove it from cache, or have to tell the server not to cache this page.

So if we do not cache the page then on click of back button request goes to server side and validation can be done whether session exist's or not.


This can be achieved by adding following code in the page load of the page for which we do not want to cache the page in history.

Response.Buffer=<SPAN style="COLOR: blue">true;<o:p></o:p>
Response.ExpiresAbsolute=DateTime.Now.AddDays(-1d);
Response.Expires =-1500;
Response.CacheControl = "no-cache";
 if(Session["SessionId"] == null)
    {
    Response.Redirect ("Login.aspx");
    }
}

Code in Detail-  

Response.ExpiresAbsolute=DateTime.Now.AddDays(-1d);

In this instead of giving the current date we gave the date in the past so it confirms the expiration of page. So that allowing for time differences, rather than specify a static date. If your page is being viewed by a browser in a very different time-zone.


Response.Expires = -1500;    

Some IIS internals experts revealed this can be a very touchy parameter to rely upon and usually requires a rather"large" negative number or pedantically, that would be a very small number.

Response.CacheControl = "no-cache";

It tells the browser not to cache the page.

Things can work with only one line of code
i.e.     Response.CacheControl = "no-cache";

But it is good practice to delete the existing page from cache.


This code will tell the server not to cache this page, due to this when user clicks the Back button browser will not find the page in cache and then will go to server side to get the page.

Disabling cache can also be done by adding following line in Meta section of page


But when I tried this it does not worked for me.
Proxy Server Caching-

Response.CacheControl = "private";

It disables the proxy server caching and page is cached on local machine.

Response.CacheControl = "public";

Proxy server cache is enabled.

Users request pages from a local server instead of direct from the source.

So if the information displayed is critical information extra care should be taken to remove the page from cache on sign out.

Hence for such applications keeping pages non caching is good solution.

Monday, January 9, 2012

When you install the 32-bit version of Microsoft ASP.NET 2.0 on a 64-bit computer, the ASP.NET state service (Aspnet_state.exe) is not installed.

This problem may occur when one of the following conditions is true:
  • You install only the 32-bit version of ASP.NET 2.0 on a 64-bit computer where Microsoft Internet Information Services (IIS) is already configured to run in Microsoft Windows on Windows 64 (WOW64) mode.
  • You uninstall the 64-bit version of ASP.NET 2.0 before you install the 32-bit version of ASP.NET 2.0.
 
To resolve this problem, install the 64-bit version of ASP.NET 2.0 before you install the 32-bit version of ASP.NET 2.0.

Note The information in this article applies only to 64-bit computers that are running the 32-bit version of ASP.NET 2.0 and IIS in WOW64 mode. Additionally, the following steps configure the computer to run the 32-bit version of ASP.NET 2.0 and IIS in WOW64 mode.

To resolve this problem, follow these steps:
  1. If you already installed the 32-bit version of ASP.NET 2.0 on the computer, run the following command to uninstall the 32-bit version of ASP.NET 2.0:
    Framework\v2.0.50727\aspnet_regiis -u
  2. Run the following command to switch IIS to run in native mode:
    cscript DriveLetter:\inetpub\AdminScripts\adsutil.vbs set w3svc/AppPools/Enable32BitAppOnWin64 0
  3. Run the following command to install the 64-bit version of ASP.NET 2.0:
    Framework64\v2.0.50727\aspnet_regiis -i
  4. Run the following command to switch IIS to run in WOW64 mode:
    cscript DriveLetter:\inetpub\AdminScripts\adsutil.vbs set w3svc/AppPools/Enable32BitAppOnWin64 1
  5. Run the following command to install the 32-bit version of ASP.NET 2.0:
    Framework\v2.0.50727\aspnet_regiis -i

Wednesday, December 28, 2011

how to create a proxy from webservice?

There are two ways to create proxy from web Service:

1. Add web reference in your web application and use them.


2. Open the command prompt of visual studio and copy & paste bellow mentioned line:

For VB
=====
wsdl /language:VB /n:"<destination>" http://<source&lgt;?WSDL

For C# 
=====
wsdl /language:CS /n:"<destination>" http://
<source&lgt;?WSDL

Friday, December 2, 2011

how to download a file

        Response.ContentType = "application/pdf";
        Response.AppendHeader("Content-Disposition", "attachment; filename=aaa-13.pdf");
        Response.TransmitFile(Server.MapPath("~/Image/UserDetails-13.pdf"));
        Response.End();