Hi muhammad12,
Please refer below sample.
HTML
Default
<table>
<tr>
<td>UserName:
</td>
<td>
<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>Password:
</td>
<td>
<asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td></td>
<td>
<asp:Button ID="btnLogin" runat="server" Text="Login" OnClick="Login" />
</td>
</tr>
</table>
Home
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField HeaderText="StationId" DataField="StationId" />
<asp:BoundField HeaderText="Name" DataField="Name" />
<asp:BoundField HeaderText="Origin" DataField="Origin" />
<asp:BoundField HeaderText="Destination" DataField="Destination" />
<asp:BoundField HeaderText="Date" DataField="Date" />
</Columns>
</asp:GridView>
<asp:Label ID="lblUserName" runat="server"></asp:Label>
Namespaces
C#
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data.SqlClient
Code
C#
Default
protected void Login(object sender, EventArgs e)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("SELECT Origin FROM Users WHERE Username = @UserName AND Password = @Password", con))
{
cmd.Parameters.AddWithValue("@Username", txtUserName.Text.Trim());
cmd.Parameters.AddWithValue("@Password", txtPassword.Text.Trim());
con.Open();
string origin = Convert.ToString(cmd.ExecuteScalar());
con.Close();
if (!string.IsNullOrEmpty(origin))
{
Session["Origin"] = origin;
Response.Redirect("Home.aspx");
}
else
{
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Invalid UserName or Password.')", true);
}
}
}
}
Home
protected void Page_Load(object sender, EventArgs e)
{
if(!this.IsPostBack)
{
string origin = Session["Origin"].ToString();
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM [tblStation] WHERE Origin = @Origin ", con))
{
cmd.Parameters.AddWithValue("@Origin", origin);
con.Open();
gvCustomers.DataSource = cmd.ExecuteReader();
gvCustomers.DataBind();
con.Close();
}
}
}
}
VB.Net
Default
Protected Sub Login(ByVal sender As Object, ByVal e As EventArgs)
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand("SELECT Origin FROM Users WHERE Username = @UserName AND Password = @Password", con)
cmd.Parameters.AddWithValue("@Username", txtUserName.Text.Trim())
cmd.Parameters.AddWithValue("@Password", txtPassword.Text.Trim())
con.Open()
Dim origin As String = Convert.ToString(cmd.ExecuteScalar())
con.Close()
If Not String.IsNullOrEmpty(origin) Then
Session("Origin") = origin
Response.Redirect("Home.aspx")
Else
ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('Invalid UserName or Password.')", True)
End If
End Using
End Using
End Sub
Home
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim origin As String = Session("Origin").ToString()
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand("SELECT * FROM tblStation WHERE Origin = @Origin ", con)
cmd.Parameters.AddWithValue("@Origin", origin)
con.Open()
gvCustomers.DataSource = cmd.ExecuteReader()
gvCustomers.DataBind()
con.Close()
End Using
End Using
End If
End Sub
Screenshot