Hi Indradeo,
Kindly refer below sample.
HTML
Default
<table>
<tr>
<th>Id:</th>
<td><asp:TextBox runat="server" ID="txtId" /></td>
</tr>
<tr>
<th>Name:</th>
<td><asp:TextBox runat="server" ID="txtName" /></td>
</tr>
<tr>
<th>Country:</th>
<td><asp:DropDownList runat="server" ID="ddlCountries">
<asp:ListItem Text="Select" Value="0" />
<asp:ListItem Text="United States" Value="1" />
<asp:ListItem Text="India" Value="2" />
<asp:ListItem Text="France" Value="3" />
<asp:ListItem Text="Russia" Value="4" />
</asp:DropDownList></td>
</tr>
<tr>
<td><asp:Button Text="Login" runat="server" OnClick="btnLogin_Click" /></td>
</tr>
<tr>
<td><asp:Label runat="server" ID="lblMessage" /></td>
</tr>
</table>
Index
<table>
<tr>
<th>CustomersId:</th>
<td><asp:Label ID="lblId" runat="server" /></td>
</tr>
<tr>
<th>Name:</th>
<td><asp:Label ID="lblName" runat="server" /></td>
</tr>
</table>
NameSpaces
C#
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Configuration
Imports System.Data.SqlClient
Code
C#
Default
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnLogin_Click(object sender, EventArgs e)
{
int countryId = 0;
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlConnection con = new SqlConnection(conString);
SqlCommand cmd = new SqlCommand("select * from CustomerTest where CustomerID=@Id and Name=@Name and CountryID=@Country", con);
cmd.Parameters.AddWithValue("@Id", txtId.Text);
cmd.Parameters.AddWithValue("@Name", txtName.Text);
cmd.Parameters.AddWithValue("@Country", ddlCountries.SelectedValue);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
Session["Id"] = Convert.ToInt32(reader["CustomerID"].ToString());
Session["Name"] = reader["Name"].ToString();
countryId = int.Parse(reader["CountryID"].ToString());
}
else
{
lblMessage.Font.Size = FontUnit.Large;
lblMessage.Text = "Invalid credentials";
lblMessage.ForeColor = System.Drawing.Color.Red;
}
reader.Close();
cmd.Dispose();
con.Close();
switch (countryId)
{
case 1:
Response.Redirect("Index.aspx");
break;
case 2:
Response.Redirect("Index1.aspx");
break;
case 3:
Response.Redirect("Index2.aspx");
break;
}
}
Index
protected void Page_Load(object sender, EventArgs e)
{
if(!this.IsPostBack)
{
lblId.Text = Session["Id"].ToString();
lblName.Text = Session["Name"].ToString();
}
}
VB.Net
Default
Protected Sub btnLogin_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim countryId As Integer = 0
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim con As SqlConnection = New SqlConnection(conString)
Dim cmd As SqlCommand = New SqlCommand("select * from CustomerTest where CustomerID=@Id and Name=@Name and CountryID=@Country", con)
cmd.Parameters.AddWithValue("@Id", txtId.Text)
cmd.Parameters.AddWithValue("@Name", txtName.Text)
cmd.Parameters.AddWithValue("@Country", ddlCountries.SelectedValue)
con.Open()
Dim reader As SqlDataReader = cmd.ExecuteReader()
If reader.Read() Then
Session("Id") = Convert.ToInt32(reader("CustomerID").ToString())
Session("Name") = reader("Name").ToString()
countryId = Integer.Parse(reader("CountryID").ToString())
Else
lblMessage.Font.Size = FontUnit.Large
lblMessage.Text = "Invalid credentials"
lblMessage.ForeColor = System.Drawing.Color.Red
End If
reader.Close()
cmd.Dispose()
con.Close()
Select Case countryId
Case 1
Response.Redirect("IndexVB.aspx")
Case 2
Response.Redirect("Index1VB.aspx")
Case 3
Response.Redirect("Index2VB.aspx")
End Select
End Sub
Index
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
lblId.Text = Session("Id").ToString()
lblName.Text = Session("Name").ToString()
End If
End Sub
Screenshot