ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Display User Name in DropDownList after Successful Login in ASP.Net using C# and VB.Net<p>Hi <a class="username" rel="akhter"> akhter</a>,</p> <p>Refer below sample.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <p><strong>Login.aspx</strong></p> <pre class="brush: html">Name: &lt;asp:TextBox runat=&#34;server&#34; ID=&#34;txtName&#34; /&gt;&lt;br /&gt; Id : &lt;asp:TextBox runat=&#34;server&#34; ID=&#34;txtId&#34; /&gt;&lt;br /&gt; &lt;asp:Button Text=&#34;Login&#34; runat=&#34;server&#34; OnClick=&#34;Login&#34; /&gt;</pre> <p><strong>Default.aspx</strong></p> <pre class="brush: html">&lt;asp:DropDownList ID=&#34;ddlName&#34; runat=&#34;server&#34;&gt; &lt;/asp:DropDownList&gt;</pre> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Data; using System.Data.SqlClient; using System.Configuration;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data.SqlClient Imports System.Data Imports&nbsp;System.Configuration</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <p><strong>Login.aspx.cs</strong></p> <pre class="brush: csharp">protected void Login(object sender, EventArgs e) { Session[&#34;Id&#34;] = txtId.Text; Response.Redirect(&#34;Default.aspx&#34;); }</pre> <p><strong>Default.aspx.cs</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { string id = Session[&#34;id&#34;].ToString(); string constr = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand(&#34;SELECT CustomerId ,Name FROM Customers WHERE CustomerId = @Id&#34;, con)) { cmd.Parameters.AddWithValue(&#34;@Id&#34;, id); using (SqlDataAdapter da = new SqlDataAdapter(cmd)) { DataTable dt = new DataTable(); da.Fill(dt); ddlName.DataSource = dt; ddlName.DataTextField = &#34;Name&#34;; ddlName.DataValueField = &#34;CustomerId&#34;; ddlName.DataBind(); } } } } }</pre> <p><strong>VB.Net</strong></p> <p><strong>Login.aspx.vb</strong></p> <pre class="brush: vb">Protected Sub Login(ByVal sender As Object, ByVal e As EventArgs) Session(&#34;Id&#34;) = txtId.Text Response.Redirect(&#34;DefaultVB.aspx&#34;) End Sub</pre> <p><strong>Default.aspx.vb</strong></p> <pre class="brush: 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(&#34;id&#34;).ToString() Dim constr As String = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString Using con As SqlConnection = New SqlConnection(constr) Using cmd As SqlCommand = New SqlCommand(&#34;SELECT CustomerId ,Name FROM Customers WHERE CustomerId = @Id&#34;, con) cmd.Parameters.AddWithValue(&#34;@Id&#34;, id) Using da As SqlDataAdapter = New SqlDataAdapter(cmd) Dim dt As DataTable = New DataTable() da.Fill(dt) ddlName.DataSource = dt ddlName.DataTextField = &#34;Name&#34; ddlName.DataValueField = &#34;CustomerId&#34; ddlName.DataBind() End Using End Using End Using End If End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://imgur.com/CxcZat5.gif" alt="" width="239" height="238" /></p>https://www.aspforums.net:443/Threads/525099/Display-User-Name-in-DropDownList-after-Successful-Login-in-ASPNet-using-C-and-VBNet/https://www.aspforums.net:443/Threads/525099/Display-User-Name-in-DropDownList-after-Successful-Login-in-ASPNet-using-C-and-VBNet/Mon, 13 May 2019 07:13:09 GMT