我想基于用户id的会话将数据从SQL Server显示到ASP.NET文本框

时间:2018-02-11 10:45:15

标签: c# asp.net sql-server

我在下面尝试过编码但没有发生任何事情。所有文本框都是空白的。

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

2 个答案:

答案 0 :(得分:1)

你能尝试下面的事吗。

为了更好的实施,我做了以下几点修改。

  • 删除了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());
                }

            }
        }

答案 1 :(得分:1)

如果你的连接字符串,查询和检索字段名称是正确的,请在页面加载中尝试此代码...它将起作用

func recursionUpload() {

.. some other code

var s3 = new AWS.S3({apiVersion: '2006-03-01', region: 'us-west-2'});
var params = {
  Bucket: 'bucket',
  Key: 'example2.txt',
  Body: 'Uploaded text using the promise-based method!'
};
var putObjectPromise = s3.putObject(params).promise();
putObjectPromise.then(function(data) {
  console.log('Success');

  //Upload next image
  recursionUpload()


}).catch(function(err) {
  console.log(err);
});


}