Tuesday, January 24, 2012

how can session information/data be stored using a SQL server?


At work we use an Oracle 9i db to store our session info by removing the standard session provided by .NET and forcing our own session implementation in the web.config file.
So to answer your question, in order to store ASP.NET session information inside SQL Server you must:
1) Have SQL Server version 7 or 2000 installed.
2) Execute the following script using SQL Server’s Query Analyzer, which creates the database that ASP.NET uses to store Session:
\%winroot%\Microsoft.NET\Framework\versionNum\InstallSqlState.sql
3. Modify the web.config file appropriately (where the connectionString attribute denotes the connection string that should be used to open the database):
<sessionState mode="SQLServer" sqlConnectionString=
"data source=127.0.0.1;user sa;password=" />
Storing Session inside SQL Server is the most durable option, as information persists even if the machine crashes. This is the slowest option of the three, however, because session data must be accessed from disk rather than directly from memory, a process thousands of times slower. Note that as with the Windows Service option, you can point ASP.NET to a Session store on a different machine, which is also appropriate in Web farm settings.

Saturday, October 8, 2011

Screen Scraping (AKA Web Fetching) using ASP.NET

Screen Scraping, in terms of programmer means fetching data from a website into your application. To talk more technically, it is actually a way by which your application extracts data from the output of some other program. This technique is basically making the request and parsing the response.



his can help you in a tremendous way. You can scrape all products from a website and put them in your application or save them in a spreadsheet, you can do comparisons by scraping data from multiple sites and do research or analysis.
To perform Screen scraping in ASP.NET, we will be using the WebResponse and theWebRequest objects. For this you will need to import System.Net namespace.
I am attaching the code, you can download example Screen Scraping Visual Studio 2005 project.
The start page (i.e. startpage.aspx) looks like as shown in the figure below:
And as you click the button "Click to view", the data from my html page is fetched to the second page of the application i.e.(WebForm1.aspx) as shown in the figure below:
In this application I have screen scraped one of the html pages designed by me, and it is hardcoded, so you can simply go to the line:
//you need to replace this string with any web site url that you require
stringstr="C:/Documents and Settings/Sairam/Desktop/agro_prod.htm";
home.Text = screenscrape(str);

and put the URL you wish to fetch the content from. Or if you wish to work as this is, you will need to change the URL, depending on the location you save the html file. I have attached both the .net project as well as the "agro_prod.html". Just go and try it.
 Here the "screenscrape" method is the method defined by me which performs the major functionality.
private string screenscrape(string url)
{
  WebResponse obj;
  WebRequest obj1=System.Net .HttpWebRequest .Create (url);
  obj=obj1.GetResponse ();
  using(StreamReader sr=new StreamReader (obj.GetResponseStream ()))
  {
    r = sr.ReadToEnd ();
    sr.Close ();
  }
  return r;
}
Once you get the whole content from some site, you can now parse the data there. Extract table from there and many things as per your requirements.

In this demo I have only concentrated on one way of fetching data. There are other methods too. Two of them are listed below:

  • WebClient: This is a class and is part of the System.Net namespace. The main functionality of scraping is performed by the DownloadData method of this class.You can do this by the following lines of code:
WebClient obj = new WebClient();
Byte[] result;
result=obj.DownloadData("http://myssiteToScrape.com");
UTF8Encoding encoding;
String strResult;
strResult=encoding.GetString(result);
Label1.Text=result;
  • HttpServerUtility: This is a seldom used technique. You can use this technique if intend to extract your data from some other page in your own application, by implementing through the following lines of code:
TextWriter writer = new StringWriter();
Server.Execute ("startpage.aspx",writer);
Response.Output.Write(writer.ToString());
           It is to note here that TextWriter is an abstract class so it cannot be instantiated.

Sunday, September 25, 2011

Display alert before redirect in asp.net

 ScriptManager.RegisterStartupScript(Me, Me.GetType(), "message", "alert('Thank for posting your comment. You will now be redirected to our home page.');location.href = 'EntryMaster.aspx'", True)

Saturday, September 24, 2011

How To Block F5(Refresh) Key In IE and Firefox


<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>Block F5 Key In IE & Mozilla</title>

    <script language="JavaScript">

        var version = navigator.appVersion;

        function showKeyCode(e) {
            var keycode = (window.event) ? event.keyCode : e.keyCode;

            if ((version.indexOf('MSIE') != -1)) {
                if (keycode == 116) {
                    event.keyCode = 0;
                    event.returnValue = false;
                    return false;
                }
            }
            else {
                if (keycode == 116) {
                    return false;
                }
            }
        }

    </script>

</head>
<body onload="JavaScript:document.body.focus();" onkeydown="return showKeyCode(event)">
</body>
</html>

How To Stopping Your User From Right -Clicking


Want to prevent your user from performing any of the other commands available by right-clicking on a Web page in Internet Explorer? It’s not foolproof, but this neat little HTML edit usually does the trick.
Just alter the opening <body> tag of your HTML to the following:


<body oncontextmenu="return false">

When the menu is requested, the oncontextmenu event runs, and we instantly cancel it using JavaScript. This is especially potent as a method for stopping the user from viewing your source, when used in conjunction with a menu-less browser window. Great stuff!

Get list of all active session variables in ASP.NET


In this post I am going to discuss about  how you can get list of all active Session Variables in ASP.NET Application. The easiest way to get the details of session variable is using “Tracing” . If you enable the “Tracing” for your application, you can get list of all  Active Session variables. Another alternative way is, get all the list of session variable using “Session.Contents”.
To illustrate,  Let me store some dummy data to in some session variables,
image
Now, if you enable tracing in your page and inspect the Session State section, you will get the list of all session variable along with their type and values.
image
Now, if you want to read the same session variable programmatically, you have to use “Session.Contents”. Session.Contents returns the current System.Web.SessionState.HttpSessionState
image

Once done, you will get below details (here sessionItems is a multiline Text here )
image
Hope this will help you !!

Maintain Scroll Position After Postback in Asp.Net 2.0 3.5

Method 1 .

Write below mention directive in page directive section of html source of aspx page to maintain scroll position of only one page or selected pages rather then whole web application.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"  
         MaintainScrollPositionOnPostback="true"  Inherits="_Default" %>



Method 2. 

To maintain scroll position programmatically use code mentione below.
System.Web.UI.Page.MaintainScrollPositionOnPostBack = true;


Method3.

To maintain scroll position application wide or for all pages of web application we can write below mentioned code in pages section of web.config file so that we don't need to add page directive in each and every page.

<pages maintainScrollPositionOnPostBack="true">