Tuesday 30 October 2012

How to use ApplicationState or Global.asax file.

How to use ApplicationState or Global.asax file.

Application object is an object of httpApplicationState class.
whenever the information is common to several pages of website than we go for applicationstate object to show that information . may be that information is count user login or any display record which is common to all.

global.asax is carrying events, some event is start users and some event is start for application .there are several events to specific work.
there are session_start and session_end events, session_start event works like a constructor(session_start (login user) ) and session_end event works like a destructor (session_end(logout user).

if one user requested 3 times than how many time session start event fire...? .... only one time behave like constructor.

if there are 15 users login or hit the website than session object 15 times made for every users.

*** application_start event and application_end event fires only one times .

** if we want to know , how many time the wesite is visited than we use the applicationstate .
Global.asax

<%@ Application Language="C#" %>

<script runat="server">

    void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup

        Application["temp"] = 0;
       // Application["mycon"] = "initial catalog=practicecac; data source=om-pc; integrated security=yes";

    }
 
    void Application_End(object sender, EventArgs e)
    {
        //  Code that runs on application shutdown

    }
     
    void Application_Error(object sender, EventArgs e)
    {
        // Code that runs when an unhandled error occurs

    }

    void Session_Start(object sender, EventArgs e)
    {
        // Code that runs when a new session is started
        Application["ctr"] = Convert.ToInt32( Application["ctr"] )+ 1;
     
    }

    void Session_End(object sender, EventArgs e)
    {
        // Code that runs when a session ends.
        // Note: The Session_End event is raised only when the sessionstate mode
        // is set to InProc in the Web.config file. If session mode is set to StateServer
        // or SQLServer, the event is not raised.
        Application["ctr"] = Convert.ToInt32(Application["ctr"]) - 1;
    }
     
</script>
 Page.aspx

 protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = Application["ctr"].ToString();

        //SqlConnection con = new SqlConnection(Application["mycon"].ToString());
        //SqlCommand cmd = new SqlCommand("select * from tbl_StudentRecords",con);
        //con.Open();
        //SqlDataReader dr = cmd.ExecuteReader();
        //dr.Read();
        //Label1.Text=dr[1 ].ToString();
        //con.Close();
    }

Monday 15 October 2012

Using 3 Layer Architecture to Insert Data Into a Database

Friday 5 October 2012

Consume a Web Service in C#.Net


C#.Net How To: Consume a Web Service in C#.Net - Consume WebService in Windows Console Application

C#.Net How To: Consume a Web Service in C#.Net - Consume WebService in Windows Console Application


The Tutorial explains how to consume a web service in a C# console application program. I am assuming you have already created a web service. Please refer my previous post onhow to create a web service application.

1)   Create a new C# windows console application project.

Here, I am giving name of the project as “MyFirstWebServiceConsumerApp”. Click on Ok button.

2)   Go to Solution Explorer and right click on your Console Application Project Name. In this case, right click on “MyFirstWebServiceConsumerApp” and   select “Add Service Reference…” from the drop down menu.


3)   Click on “Advance” button.


4)   Click on “Add Web Reference..” button.


5)   An Add Web Reference window will appear. Enter here URL of the web service.



  6)   Enter the Web Service URL and click on -> button to check whether the Web Service URL is valid or not. If the URL is valid, it will show you the available Web Methods and the status will appear as “1 Service Found:
Enter web service reference name. I used “MyFirstWebServiceReference” as a web service reference name.


Now, click on “Add Reference” button.

7)   Now check the Solution Explorer. “MyFirstWebServiceReference” should be added under web reference folder.


8)   The next step is to add Reference to our c# code. Add following line

       using MyFirstWebServiceConsumerApp.MyFirstWebServiceReference;

9)   Add code to call the web method. The final code will appear as
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyFirstWebServiceConsumerApp.MyFirstWebServiceReference;

namespace MyFirstWebServiceConsumerApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Service1 webService = new Service1();
            Console.WriteLine(webService.MyFirstWebMethod("Bradd""Pitt"));
            Console.ReadLine();
        }
    }
}
10) Now hit F5 button on keyboard to execute the code. The result should appear as

Your fi­rst web service consumer console application is ready.

Thursday 4 October 2012

SQL SERVER – Definition, Comparison and Difference between HAVING and WHERE Clause


SQL SERVER – Definition, Comparison and Difference between HAVING and WHERE Clause

In recent interview sessions in hiring process I asked this question to every prospect who said they know basic SQL. Surprisingly, none answered me correct. They knew lots of things in details but not this simple one. One prospect said he does not know cause it is not on this Blog. Well, here we are with same topic online.
Answer in one line is : HAVING specifies a search condition for a group or an aggregate function used in SELECT statement.
HAVING can be used only with the SELECT statement. HAVING is typically used in a GROUP BY clause. When GROUP BY is not used, HAVING behaves like a WHERE clause.
A HAVING clause is like a WHERE clause, but applies only to groups as a whole, whereas the WHERE clause applies to individual rows. A query can contain both a WHERE clause and a HAVING clause. The WHERE clause is applied first to the individual rows in the tables . Only the rows that meet the conditions in the WHERE clause are grouped. The HAVING clause is then applied to the rows in the result set. Only the groups that meet the HAVING conditions appear in the query output. You can apply a HAVING clause only to columns that also appear in the GROUP BY clause or in an aggregate function. (Reference :BOL)
Example of HAVING and WHERE in one query:
SELECT titles.pub_idAVG(titles.price)FROM titles INNER JOIN publishersON titles.pub_id publishers.pub_idWHERE publishers.state 'CA'GROUP BY titles.pub_idHAVING AVG(titles.price) > 10
Sometimes you can specify the same set of rows using either a WHERE clause or a HAVING clause. In such cases, one method is not more or less efficient than the other. The optimizer always automatically analyzes each statement you enter and selects an efficient means of executing it. It is best to use the syntax that most clearly describes the desired result. In general, that means eliminating undesired rows in earlier clauses.