I want to display the users emp_id from my database on my post page after the login? Can anyone help me out? Can I pull the variable from this select statement and pass it to the next page?

my login.aspx.cs code

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }

    protected void LoginButton_Click(object sender, EventArgs e)
    {
        {
            // Initialize FormsAuthentication (reads the configuration and gets
            // the cookie values and encryption keys for the given application)
            FormsAuthentication.Initialize();

            // Create connection and command objects
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString.ToString());
            con.Open();
            SqlCommand cmd = con.CreateCommand();
            cmd.CommandText = "SELECT * FROM employee WHERE emp_id=@username " + "AND password=@password"; // (this should really be a stored procedure, shown here for simplicity)

            // Fill our parameters
            cmd.Parameters.Add("@username", SqlDbType.NVarChar, 64).Value = UserName.Text;
            cmd.Parameters.Add("@password", SqlDbType.NVarChar, 64).Value = Password.Text;
            
           
            FormsAuthentication.HashPasswordForStoringInConfigFile(Password.Text, "sha1");
            // you can use the above method for encrypting passwords to be stored in the database
            // Execute the command
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader.Read())
            {
                FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
               1, // Ticket version
               UserName.Text, // Username to be associated with this ticket
               DateTime.Now, // Date/time issued
               DateTime.Now.AddMinutes(30), // Date/time to expire
               true, // "true" for a persistent user cookie (could be a checkbox on form)
               reader.GetString(0), // User-data (the roles from this user record in our database)
               FormsAuthentication.FormsCookiePath); // Path cookie is valid for

                // Hash the cookie for transport over the wire
                string hash = FormsAuthentication.Encrypt(ticket);
                HttpCookie cookie = new HttpCookie(
                FormsAuthentication.FormsCookieName, // Name of auth cookie (it's the name specified in web.config)
                 hash); // Hashed ticket

                // Add the cookie to the list for outbound response
                Response.Cookies.Add(cookie);

                // Redirect to requested URL, or homepage if no previous page requested
                string returnUrl = Request.QueryString["ReturnUrl"];
                if (returnUrl == null) returnUrl = "Default.aspx";

                // Don't call the FormsAuthentication.RedirectFromLoginPage here, since it could
                // replace the custom authentication ticket we just added...
                Response.Redirect(returnUrl);
                
                

            }
            else
            {
                // Username and or password not found in our database...
                ErrorLabel.Text = "Username / password incorrect. Please login again.";
                ErrorLabel.Visible = true;
            }
            // (normally you'd put all this db stuff in a try / catch / finally block)
            reader.Close();
            con.Close();
            cmd.Dispose();
        }
    }
        private bool IsAuthenticated( string UserName, string Password )
{
  // Lookup code omitted for clarity
  // This code would typically validate the user name and password
  // combination against a SQL database or Active Directory
  // Simulate an authenticated user
    bool isAuthenticated = IsAuthenticated(UserName , Password);

  return true;
}

    }

my default.aspx.cs code

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using web = System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.Data.SqlClient;

public partial class Default : System.Web.UI.Page
{
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.IsAuthenticated)
        {
            Label2.Text = Application["activeUsers"].ToString();
(I WANT LABEL3.TEXT = FIRSTNAME , LASTNAME FROM MY DATABASE)
                       

        }
        else
        {
            Response.Redirect("./Login.aspx");
        }
        
    }


    	


    protected void FormView2_PageIndexChanging(object sender, System.Web.UI.WebControls.FormViewPageEventArgs e)
    {
       
    }

  

   

    
    protected void FormView2_PageIndexChanging1(object sender, System.Web.UI.WebControls.FormViewPageEventArgs e)
    {
       

    }
}

I got using a session variable

Session["sessFirstName"] = UserName.Text;

Hello freshfitz,
your method is correct but you can also try
UserName.Text=user.identity.name
on your default page..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.