ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2017 www.aspforums.com. All rights reserved.Get UserId on page load after login in ASP.Net<p>Hi <a class="username" rel="micah">micah</a>,</p> <p>I have created sample code which fullfill requirement.</p> <p><span style="text-decoration: underline;">HTML</span></p> <pre class="brush: html">&lt;div&gt; UserName:&lt;asp:TextBox ID=&#34;txtUserName&#34; runat=&#34;server&#34; /&gt; &lt;br /&gt; &lt;br /&gt; Password:&lt;asp:TextBox ID=&#34;txtpassword&#34; TextMode=&#34;Password&#34; runat=&#34;server&#34; /&gt; &lt;br /&gt; &lt;br /&gt; &lt;asp:Button ID=&#34;btnSubmit&#34; Text=&#34;Submit&#34; runat=&#34;server&#34; OnClick=&#34;btnSubmit_Click&#34; /&gt; &lt;/div&gt; &lt;div&gt; &lt;br /&gt; &lt;br /&gt; &lt;asp:Label ID=&#34;lblUserId&#34; Text=&#34;&#34; runat=&#34;server&#34; /&gt; &lt;asp:Label ID=&#34;lblUserName&#34; Text=&#34;&#34; runat=&#34;server&#34; /&gt; &lt;/div&gt;</pre> <p><span style="text-decoration: underline;">C#</span></p> <pre class="brush: csharp">private string constr = ConfigurationManager.ConnectionStrings["constr"].ToString(); protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { } } protected void btnSubmit_Click(object sender, EventArgs e) { using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand("SELECT * FROM Users WHERE Username='" + txtUserName.Text + "'And password='" + txtpassword.Text + "'", con)) { DataTable dt = new DataTable(); using (SqlDataAdapter sda = new SqlDataAdapter(cmd)) { sda.Fill(dt); if (dt.Rows.Count &gt; 0) { Session["UserId"] = dt.Rows[0]["Username"].ToString(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Response.Redirect("~/Default2.aspx"); } } } } }</pre> <p><span style="text-decoration: underline;">Second Page</span></p> <p><span style="text-decoration: underline;">HTML</span></p> <pre class="brush: html">&lt;div&gt; UserId:&lt;asp:Label ID=&#34;lblUserId&#34; Text=&#34;&#34; runat=&#34;server&#34; /&gt; &lt;/div&gt;</pre> <p><span style="text-decoration: underline;">C#</span></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { lblUserId.Text = Session[&#34;UserId&#34;].ToString(); } }</pre> <p><span style="text-decoration: underline;">Screenshot</span></p> <p><span style="text-decoration: underline;"><img src="http://i988.photobucket.com/albums/af3/mudassarkhan/711778_2.gif" alt="" width="472" height="320" /></span></p>http://www.aspforums.net/Threads/711778/Get-UserId-on-page-load-after-login-in-ASPNet/http://www.aspforums.net/Threads/711778/Get-UserId-on-page-load-after-login-in-ASPNet/Mon, 19 Sep 2016 03:56:05 GMTGet UserId on page load after login in ASP.Net<p>This solution is not addressing my question as well, if I login with username and password but I will pass the Userid to the landing page and then fetch it on the new page</p>http://www.aspforums.net/Threads/711778/Get-UserId-on-page-load-after-login-in-ASPNet/http://www.aspforums.net/Threads/711778/Get-UserId-on-page-load-after-login-in-ASPNet/Mon, 19 Sep 2016 05:55:25 GMTGet UserId on page load after login in ASP.Net<p>Hi <a class="username" rel="micah">micah</a>,</p> <p>Modified the code.Please recheck</p>http://www.aspforums.net/Threads/711778/Get-UserId-on-page-load-after-login-in-ASPNet/http://www.aspforums.net/Threads/711778/Get-UserId-on-page-load-after-login-in-ASPNet/Mon, 19 Sep 2016 06:28:45 GMTGet UserId on page load after login in ASP.Net<p>Will session work with Asp.Net web form authentication?, &nbsp;because that what you used and Am told it doesn't.</p>http://www.aspforums.net/Threads/711778/Get-UserId-on-page-load-after-login-in-ASPNet/http://www.aspforums.net/Threads/711778/Get-UserId-on-page-load-after-login-in-ASPNet/Mon, 19 Sep 2016 06:36:02 GMTGet UserId on page load after login in ASP.Net<p>Instead of Session["UserId"] use HttpContext.Current.Session["UserId"]. It will work.</p>http://www.aspforums.net/Threads/711778/Get-UserId-on-page-load-after-login-in-ASPNet/http://www.aspforums.net/Threads/711778/Get-UserId-on-page-load-after-login-in-ASPNet/Mon, 19 Sep 2016 06:41:34 GMTGet UserId on page load after login in ASP.Net<p>but adding that to paramater how do you place that, because i use parameter alot</p> <p>&nbsp;</p> <pre class="brush: csharp"> cmd.Parameters.AddWithValue(&#34;@id&#34;, userid);</pre> <p>this is my code</p> <p>&nbsp;</p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { if (!this.Page.User.Identity.IsAuthenticated) { FormsAuthentication.RedirectToLoginPage(); } int userid = this.Page.User.Identity.Name; GetProfile(userid); } } public void GetProfile(int userid) { string str = ConfigurationManager.ConnectionStrings["conn"].ConnectionString; string getADPOST = "GetUSERPRO"; using (SqlConnection con = new SqlConnection(str)) { con.Open(); using (SqlCommand cmd = new SqlCommand(getADPOST, con)) { cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue(&#34;@id&#34;, userid); cmd.Parameters.AddWithValue(&#34;@id&#34;, userid); //cmd.Parameters.AddWithValue("@Name", userid); SqlDataAdapter da = new SqlDataAdapter(cmd); DataTable ds = new DataTable(); da.Fill(ds); } } } }</pre> <p>&nbsp;this is the error</p> <p>&nbsp;</p> <p>&nbsp;</p> <pre class="brush: csharp">Server Error in &#39;/&#39; Application. Compilation Error Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. Compiler Error Message: CS0029: Cannot implicitly convert type &#39;string&#39; to &#39;int&#39; Source Error: Line 21: } Line 22: Line 23: int userid = this.Page.User.Identity.Name; Line 24: GetProfile(userid); Line 25: }</pre> <p>&nbsp;</p>http://www.aspforums.net/Threads/711778/Get-UserId-on-page-load-after-login-in-ASPNet/http://www.aspforums.net/Threads/711778/Get-UserId-on-page-load-after-login-in-ASPNet/Mon, 19 Sep 2016 07:15:24 GMTGet UserId on page load after login in ASP.Net<blockquote><cite>micah says:</cite> <pre><code class="csharp keyword">this</code><code class="csharp plain">.Page.User.Identity.Name</code></pre> </blockquote> <p>Above returns string value and you are trying to store as int. So the error. So as per the return value change your code and if it always returns string then change your so as well.</p>http://www.aspforums.net/Threads/711778/Get-UserId-on-page-load-after-login-in-ASPNet/http://www.aspforums.net/Threads/711778/Get-UserId-on-page-load-after-login-in-ASPNet/Mon, 19 Sep 2016 07:32:44 GMTGet UserId on page load after login in ASP.Net<p>This what I have been trying to do but it keep giving me such error, I used username to login but I want to fetch userid on landing page. That's the idea&nbsp;</p>http://www.aspforums.net/Threads/711778/Get-UserId-on-page-load-after-login-in-ASPNet/http://www.aspforums.net/Threads/711778/Get-UserId-on-page-load-after-login-in-ASPNet/Mon, 19 Sep 2016 07:38:13 GMTGet UserId on page load after login in ASP.Net<p>We don't know what your sp returning and what type of parameter you are passing to the sp. indresh code is the way you have to fetch the record. So as per the sp you have to pass the exact parameter from code to get your desired result.</p>http://www.aspforums.net/Threads/711778/Get-UserId-on-page-load-after-login-in-ASPNet/http://www.aspforums.net/Threads/711778/Get-UserId-on-page-load-after-login-in-ASPNet/Mon, 19 Sep 2016 08:12:12 GMT