簡體   English   中英

我想根據用戶ID將數據從SQL Server顯示到ASP.NET文本框

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

我已經嘗試了下面的編碼,但沒有任何反應。 所有文本框均為空白。

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();
}

你能像下面這樣嘗試嗎?

為了更好的實現,我做了以下幾處更改。

  • 消除了SQL注入漏洞。
  • 連接已更改為正在使用。
  • while(myReader.Read())更改為if(myReader.Read())

如果遇到任何錯誤,請更新您的問題。

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 (!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();
                }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM