Hi Bhavesh23,
Please refer below sample.
Note: For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
Login
Name:
<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
<asp:Button ID="btnSubmit" runat="server" Text="Login" OnClick="OnLogin">
</asp:Button>
Default
<asp:GridView ID="gvOrders" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="ShipName" HeaderText="Ship Name" />
<asp:BoundField DataField="Freight" HeaderText="Price" DataFormatString="{0:N2}" />
</Columns>
</asp:GridView>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Code
C#
Login
protected void OnLogin(object sender, EventArgs e)
{
string firstName = txtFirstName.Text.Trim();
string constring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("SELECT EmployeeID FROM Employees WHERE [FirstName] = @FirstName", con))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@FirstName", firstName);
con.Open();
int employeeId = Convert.ToInt32(cmd.ExecuteScalar());
Session["Id"] = employeeId;
con.Close();
}
}
Response.Redirect("Default.aspx");
}
Default
protected void Page_Load(object sender, EventArgs e)
{
if (Session["Id"] != null)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("SELECT TOP 5 ShipName, Freight FROM Orders WHERE EmployeeID = @Id", con))
{
con.Open();
cmd.Parameters.AddWithValue("@Id", Session["Id"]);
gvOrders.DataSource = cmd.ExecuteReader();
gvOrders.DataBind();
con.Close();
}
}
Session["Id"] = null;
}
else
{
Response.Redirect("Login.aspx");
}
}
VB.Net
Login
Protected Sub OnLogin(ByVal sender As Object, ByVal e As EventArgs)
Dim firstName As String = txtFirstName.Text.Trim()
Dim constring As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constring)
Using cmd As SqlCommand = New SqlCommand("SELECT EmployeeID FROM Employees WHERE [FirstName] = @FirstName", con)
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("@FirstName", firstName)
con.Open()
Dim employeeId As Integer = Convert.ToInt32(cmd.ExecuteScalar())
Session("Id") = employeeId
con.Close()
End Using
End Using
Response.Redirect("DefaultVB.aspx")
End Sub
Default
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Session("Id") IsNot Nothing Then
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand("SELECT TOP 5 ShipName, Freight FROM Orders WHERE EmployeeID = @Id", con)
con.Open()
cmd.Parameters.AddWithValue("@Id", Session("Id"))
gvOrders.DataSource = cmd.ExecuteReader()
gvOrders.DataBind()
con.Close()
End Using
End Using
Session("Id") = Nothing
Else
Response.Redirect("Login.aspx")
End If
End Sub
Screenshot