Friday, May 17, 2013

Distinct Keyword in SQL Server

Introduction

In this article I will explain how to use distinct keyword in SQL server. The DISTINCT keyword can be used to return only different values.

Query
 

CREATE TABLE [dbo].[Emp_Info](
      [Emp_Id] [int] NULL,
      [Name] [varchar](50) NULL,
      [Salary] [int] NULL,
      [City] [varchar](50) NULL
) ON [PRIMARY]
Insert some record in table. see the record by run this query
 

Select * from Emp_Info         


Now you will see in above fig table does not contain any primary key column because of that duplicate records exist in table.


Eliminates duplicate rows

The DISTINCT keyword can be used to return only different values. The DISTINCT keyword can be used with SELECT Statement.
Now If you want eliminate duplicate record. Run below query


Select distinct * from Emp_Info


DISTINCT use with multiple column values

In below query distinct will apply for all the mentioned columns it will return distinct column values of Name, Salary and City.

Select distinct Name,Salary,City from Emp_Info

Now you will see below image. table have some column record which specify in query

Suppose I use Distinct keyword with Top statement in SQL Server. 

Query

select distinct top 3 * from Emp_Info

you will see only 3 column value show in table


Thursday, May 16, 2013

How to delete duplicate record from datatable in SQL Server

Introduction

In this article I will explain how to delete duplicate records from a datatable in SQL server.

Query

CREATE TABLE [dbo].[Emp_Info](
      [Emp_Id] [int] NULL,
      [Name] [varchar](50) NULL,
      [Salary] [int] NULL,
      [City] [varchar](50) NULL
) ON [PRIMARY]
This table does not contain any primary key column because of that it contains duplicate records. Insert some record in table  then



Now you will see in above fig table does not contain any primary key column because of that duplicate records exist in table.


Now I want to get duplicate records from datatable.

Query

with TableTemp as
(
select row_number() over(partition by Name,Salary order by Name) as RowNumber,* from Emp_Info
)
select * from Tabletemp
Return data by above query



If you examine above table RowNumber column added in table this column is used to know which record contains duplicate values based on rows with RowNumber greater than 1.

Now if we want to get unique value table from datatable. write below query

Query

with TableTemp as
(
select row_number() over(partition by Name,Salary order by Name) as RowNumber,* from Emp_Info
)
delete from TableTemp where RowNumber > 1
Select * from Emp_Info order by Emp_Id asc
All duplicate records will delete from our table and that would be like this


Tuesday, May 14, 2013

Application State in Asp.net


Introduction

The Application object is used to store and access variables from any page, just like the Session object. The difference between "Application and Session" is that In Application, all user share one Application object and In Sessions, there is one Session object for each user.

The first time a user requests a page that resides in an application's directory, ASP.NET initializes the application. During that process, ASP.NET creates an application object from the HttpApplication  class. This object is represented by a special class file named global.aspx.
The application object can be accessed by any of the application's pages. 

Application state is typically used to store application-specific data that changes infrequently. In other word The information can also be changed in one place, and the changes will automatically be reflected on all pages.

How to work with Application events

you may be wondering how to initialize the values of application state items. To do that, you first add a Global.asax file to the project. By default, this file contains method declarations for five event handler. like below




When an web page load, this event occurs only one time in an application’s life cycle. It occurs again when you restart the IIS. When IIS reset then application count also reset to zero (0). It’s the best place to count number of visitors.

Program Code

global.asax


<%@ Application Language="C#" %>
<script runat="server">
     void Application_Start(object sender, EventArgs e) 
    {
        // Code that runs on application startup
       Application["PageHit"] = 0;
    } 
</script>

VisitorPage.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="VisitorCount.aspx.cs" Inherits="VisitorCount" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <h3 style="color: #006600">Visit page</h3>
    <div>
    <asp:Label ID="countlbl" runat="server" Text="Label" Font-Bold="True" Font-Size="Large"></asp:Label>
        <asp:Button ID="Button1" runat="server" Text="Next Page" OnClick="Button1_Click" style="z-index: 1; left: 9px; top: 101px; position: absolute; height: 26px" />
    </div>
    </form>
</body>
</html>
VisitorPage.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class VisitorCount : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Application["PageHit"] = Convert.ToInt32(Application["PageHit"].ToString()) + 1;
        }
        countlbl.Text = "Page hit -> " + Convert.ToInt32(Application["PageHit"].ToString());
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("Page2.aspx");
    }
}

Page2.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Page2.aspx.cs" Inherits="Page2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <h3 style="color: #0000FF">Page 2</h3>
    <div>
    <asp:Label ID="countlbl" runat="server" Text="Label" Font-Bold="True" Font-Size="Large"></asp:Label>
    </div>
        <br />
        <asp:Button ID="Button1" runat="server" Font-Bold="False" OnClick="Button1_Click" Text="Back" />
    </form>
</body>
</html>
Page2.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class Page2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        countlbl.Text = "Page hit ->  " + Convert.ToInt32(Application["PageHit"].ToString());
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("VisitorCount.aspx");
    }
}
Output 1


click on Next Page button

Output 2
Now you will see page count does not change but redirect on "Page2.aspx" page.

Now click on Back button.

Output 3
 you will see page count has increase by 1.



Output 4
Reload or Refresh the "VisitorPage.aspx" page. you will see page count has increase by 1 for every time.





Monday, May 13, 2013

LinkedIn login in our website using JavaScript in Asp.net

Introduction

In this article, I will explain how to associate LinkedIn login in our website using JavaScript in Asp.net.
First we need to create LinkedIn application and get api key from LinkedIn. so click on following link

https://www.linkedin.com/secure/developer

Use the following procedure.

Step 1

login in LinkedIn



Step 2

Click on "Add New Application"



Step 3

Following these steps



Step 4

Now api key generate by Linkedin and this api key write in javascript code


Step 5

Now write below code in our application


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="LinkedIn_Login.aspx.cs" Inherits="LinkedIn_Login" %>
<!DOCTYPE html>
 <html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Linkedin Login </title>
<script type="text/javascript" src="http://platform.linkedin.com/in.js">
api_key:************ // Write your api key
</script>
</head>
<body>
<form id="form1" runat="server">
<div style="font-size:large; font-weight:bold; color:blue">
<script type="in/Login">
 
Hello,<br>
   <?js= firstName ?> <?js= lastName ?> <br>
Welcome to Linkedin
</script>
 </div>
</form>
</body>
</html>

Output 1


Click on SignIn button

Output 2



Click on Allow

Output 3



Kashmir 370 and 35A : The Wound of india

क्या है जम्मू-कश्मीर में लागू धारा 370,35A पूर्ण विवरण Know about 370 act in Jammu एक बार फिर से राजनीति गलियारे में धारा 370,35A को ल...