Hey nauna,
Please refer below smaple.
HTML
Login.aspx
<div>
Name:
<asp:TextBox runat="server" ID="txtName" />
<asp:Button Text="LoginUser" runat="server" OnClick="Loginn" />
</div>
CS.aspx
<div>
<asp:DropDownList runat="server" ID="ddlCountry">
<asp:ListItem Text="Select Country" />
<asp:ListItem Text="USA" Value="USA" />
<asp:ListItem Text="Russia" Value="Russia" />
<asp:ListItem Text="India" Value="India" />
</asp:DropDownList>
</div>
Namespaces
C#
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
VB.Net
Imports System.Data.SqlClient
Imports System.Data
Code
C#
Login.aspx.cs
protected void Loginn(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT EmployeeId, FirstName FROM Employees", conn))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
da.Fill(dt);
foreach (DataRow row in dt.Rows)
{
if (txtName.Text == row["FirstName"].ToString())
{
Session["Name"] = txtName.Text;
Response.Redirect("CS.aspx");
}
}
}
}
}
}
}
CS.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string country = GetCountryBasedOnLogedInUser();
if (ddlCountry.Items.FindByText(GetCountryBasedOnLogedInUser()) != null)
{
ddlCountry.Items.FindByText(GetCountryBasedOnLogedInUser()).Selected = true;
}
}
}
private string GetCountryBasedOnLogedInUser()
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
string query = "SELECT Country FROM Employees WHERE FirstName = @FirstName";
SqlCommand cmd = new SqlCommand(query, conn);
cmd.Parameters.AddWithValue("@FirstName", Session["Name"]);
conn.Open();
string country = Convert.ToString(cmd.ExecuteScalar());
conn.Close();
return country;
}
}
VB.Net
Login.aspx.vb
Protected Sub Loginn(ByVal sender As Object, ByVal e As EventArgs)
Using conn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Using cmd As SqlCommand = New SqlCommand("SELECT EmployeeId, FirstName FROM Employees", conn)
Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
Using dt As DataTable = New DataTable()
da.Fill(dt)
For Each row As DataRow In dt.Rows
If txtName.Text = row("FirstName").ToString() Then
Session("Name") = txtName.Text
Response.Redirect("VB.aspx")
End If
Next
End Using
End Using
End Using
End Using
End Sub
VB.aspx.vb
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim country As String = GetCountryBasedOnLogedInUser()
If ddlCountry.Items.FindByText(GetCountryBasedOnLogedInUser()) IsNot Nothing Then
ddlCountry.Items.FindByText(GetCountryBasedOnLogedInUser()).Selected = True
End If
End If
End Sub
Private Function GetCountryBasedOnLogedInUser() As String
Using conn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Dim query As String = "SELECT Country FROM Employees WHERE FirstName = @FirstName"
Dim cmd As SqlCommand = New SqlCommand(query, conn)
cmd.Parameters.AddWithValue("@FirstName", Session("Name"))
conn.Open()
Dim country As String = Convert.ToString(cmd.ExecuteScalar())
conn.Close()
Return country
End Using
End Function
Screenshot