简体   繁体   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

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. 消除了SQL注入漏洞。
  • Connection changed to using. 连接已更改为正在使用。
  • while(myReader.Read()) changed to if(myReader.Read()) while(myReader.Read())更改为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();
                }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想在ASP.NET中的页面加载时绑定来自SQL Server表列的数据,但是它不起作用 - I want to bind data from SQL Server table column at page load in ASP.NET, but it is not working 从ASP.NET中的SQL Server通过单选按钮显示数据 - Display data in radio button from SQL Server in ASP.NET 如何让 ListBox 显示来自文本框的“ID”的所有信息(ASP.NET C#) - How do I get a ListBox to display all info for an "ID" from a Textbox (ASP.NET C#) 如何使用ASP.NET从C#中的SQL Server表中自动完成TextBox中的值 - How can I autocomplete values in a TextBox from SQL Server table in C# with ASP.NET 如何在ASP.NET C#中从SQL Server选择日期到TextBox - How can I select date from SQL Server to TextBox in asp.net c# 我想在 ASP.NET MVC 中使用复选框做一个多重选择器,它从 Sql 数据库中获取员工 ID - I want to do a multiple selector with checkboxes in ASP.NET MVC it fetches the employee ID from a Sql database 从ASP.NET文本框将数据写入SQL数据库 - Writing data to SQL database from ASP.NET textbox 如何基于数据/ id关系从SQL查询中提取数据。 使用ASP.net WebMatrix - How to pull data from a SQL Query based on data/id relationship. Using ASP.net WebMatrix 我无法在从IIS Windows 7运行的ASP.net网站上显示来自sql server 2005的数据 - I cannot display data from sql server 2005 express on an ASP.net website running from IIS windows 7 我想在asp.net的标签中显示数据库中的图像 - I want to display an image from the database in a label in asp.net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM