Display User Name in DropDownList after Successful Login in ASP.Net using C# and VB.Net

Last Reply 7 days ago By pandeyism

Posted 7 days ago

I want to display employee name in dropdownlist after login from login page, from login page i created session to store user empid and want to pass this empid on every page in dropdownlist.

I want to display user name in DDLFrom dropdownlist redirect from login page

Posted 7 days ago

Hi akhter,

Refer below sample.

HTML

Login.aspx

Name: <asp:TextBox runat="server" ID="txtName" /><br />
Id : <asp:TextBox runat="server" ID="txtId" /><br />
<asp:Button Text="Login" runat="server" OnClick="Login" />

Default.aspx

<asp:DropDownList ID="ddlName" runat="server">
</asp:DropDownList>

Namespaces

C#

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

VB.Net

Imports System.Data.SqlClient
Imports System.Data
Imports System.Configuration

Code

C#

Login.aspx.cs

protected void Login(object sender, EventArgs e)
{
    Session["Id"] = txtId.Text;
    Response.Redirect("Default.aspx");
}

Default.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        string id = Session["id"].ToString();
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand("SELECT CustomerId ,Name FROM Customers WHERE CustomerId = @Id", con))
            {
                cmd.Parameters.AddWithValue("@Id", id);
                using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                {
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    ddlName.DataSource = dt;
                    ddlName.DataTextField = "Name";
                    ddlName.DataValueField = "CustomerId";
                    ddlName.DataBind();
                }
            }
        }
    }
}

VB.Net

Login.aspx.vb

Protected Sub Login(ByVal sender As Object, ByVal e As EventArgs)
    Session("Id") = txtId.Text
    Response.Redirect("DefaultVB.aspx")
End Sub

Default.aspx.vb

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim id As String = Session("id").ToString()
        Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
        Using con As SqlConnection = New SqlConnection(constr)
            Using cmd As SqlCommand = New SqlCommand("SELECT CustomerId ,Name FROM Customers WHERE CustomerId = @Id", con)
                cmd.Parameters.AddWithValue("@Id", id)
                Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
                    Dim dt As DataTable = New DataTable()
                    da.Fill(dt)
                    ddlName.DataSource = dt
                    ddlName.DataTextField = "Name"
                    ddlName.DataValueField = "CustomerId"
                    ddlName.DataBind()
                End Using
            End Using
        End Using
    End If
End Sub

Screenshot