简体   繁体   中英

I want to display data from SQL Server to an ASP.NET textbox based on session of users id

I have tried the coding below but nothing happened. All of the textbox are blank.

protected void Page_Load(object sender, EventArgs e)
{

    SqlConnection con = new SqlConnection("Data Source=USER-PC;Initial Catalog=1GCAttendanceManagementSystem;Integrated Security=True");

    DataTable dt = new DataTable();

    con.Open();

    SqlDataReader myReader = null;

    SqlCommand myCommand = new SqlCommand("select * from Employee where EmpUsername='" + Session["id"] + "'", con);

    myReader = myCommand.ExecuteReader();

    while (myReader.Read())
    {
        txtCode.Text = (myReader["EmployeeId"].ToString());
        txtUsername.Text = (myReader["EmpUsername"].ToString());
        txtPass.Text = (myReader["EmpPassword"].ToString());
        txtEmail.Text = (myReader["EmpEmail"].ToString());
        txtFirstname.Text = (myReader["EmpFirstName"].ToString());
        txtLastname.Text = (myReader["EmpLastName"].ToString());
        txtGender.Text = (myReader["EmpGender"].ToString());
        txtContact.Text = (myReader["EmpContact"].ToString());
        txtAddress.Text = (myReader["EmpAddress"].ToString());
        txtDept.Text = (myReader["EmpDept"].ToString());
    }

    con.Close();
}

Can you try like following.

For the better implementation, I have done few changes as following.

  • Removed the SQL Injection vulnerability.
  • Connection changed to using.
  • while(myReader.Read()) changed to if(myReader.Read())

If you are getting any error, update your question.

protected void Page_Load(object sender, EventArgs e)
        {

            using (SqlConnection con = new SqlConnection("Data Source=USER-PC;Initial Catalog=1GCAttendanceManagementSystem;Integrated Security=True"))
            {

                con.Open();

                SqlDataReader myReader = null;

                var salaryParam = new SqlParameter("EmpUsername", SqlDbType.VarChar);
                salaryParam.Value = Session["id"];

                SqlCommand myCommand = new SqlCommand("select TOP 1 * from Employee where EmpUsername='@EmpUsername'", con);
                myCommand.Parameters.Add(salaryParam);

                myReader = myCommand.ExecuteReader();

                if (myReader.Read())
                {
                    txtCode.Text = (myReader["EmployeeId"].ToString());
                    txtUsername.Text = (myReader["EmpUsername"].ToString());
                    txtPass.Text = (myReader["EmpPassword"].ToString());
                    txtEmail.Text = (myReader["EmpEmail"].ToString());
                    txtFirstname.Text = (myReader["EmpFirstName"].ToString());
                    txtLastname.Text = (myReader["EmpLastName"].ToString());
                    txtGender.Text = (myReader["EmpGender"].ToString());
                    txtContact.Text = (myReader["EmpContact"].ToString());
                    txtAddress.Text = (myReader["EmpAddress"].ToString());
                    txtDept.Text = (myReader["EmpDept"].ToString());
                }

            }
        }

If your connection string ,Query and retrieving field names are correct try this code in page load ...it will work

if (!IsPostBack)
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("select * from Employee where EmpUsername='" + Session["id"] + "'",con);
                    SqlDataReader dr = cmd.ExecuteReader();
                    while (dr.Read())
                    {
                         txtCode.Text = (dr["EmployeeId"].ToString());
                    txtUsername.Text = (dr["EmpUsername"].ToString());
                    txtPass.Text = (dr["EmpPassword"].ToString());
                    txtEmail.Text = (dr["EmpEmail"].ToString());
                    txtFirstname.Text = (dr["EmpFirstName"].ToString());
                    txtLastname.Text = (dr["EmpLastName"].ToString());
                    txtGender.Text = (dr["EmpGender"].ToString());
                    txtContact.Text = (dr["EmpContact"].ToString());
                    txtAddress.Text = (dr["EmpAddress"].ToString());
                    txtDept.Text = (dr["EmpDept"].ToString());
                    }
                    dr.Close();
                    con.Close();
                }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM