我想基于会话ID在ASP.NET文本框上显示数据。但页面上没有任何反应。 [英] I want to display data on ASP.NET textbox based on session id. But nothing happen on the page.

查看:75
本文介绍了我想基于会话ID在ASP.NET文本框上显示数据。但页面上没有任何反应。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我尝试了什么:



我尝试过编码。但是所有文本框都是空白的。什么都没发生。

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

What I have tried:

I have tried the coding. But all the textbox are blank. Nothing happen at all.

推荐答案

0)将你的代码放在 try / catch 块中。



1)在尝试块的顶部,将其设置为



0) Put your code in a try/catch block.

1) At the top of the try block, put this like

if (string.IsNullOrEmpty(Session["id"])) { throw new Exception("Session ID has not been set");}





2)运行调试器下的代码,并在 catch 阻止。



3)你的代码通常不是很好。这样做是这样的:





2) Run the code under the debugger with a breakpoint set in the catch block.

3) Your code is generally not really built well. Do it this way instead:

try
{
    if (string.IsNullOrEmpty(Session["id"])) 
    {
        // throw an exception so that the code doesn't perform an unnecessary 
        // database query
        throw new Exception("Session ID has not been set");
    }
    using (SqlConnection con = new SqlConnection("Data Source=USER-PC;Initial Catalog=1GCAttendanceManagementSystem;Integrated Security=True"))
    {
        DataTable dt = new DataTable();
        con.Open();
        SqlDataReader myReader = null;
        using (SqlCommand myCommand = new SqlCommand("select * from Employee where EmpUsername='" + Session["id"] + "'", con){CommandType=CommandType.Text})
        {
            myReader = myCommand.ExecuteReader();
            if (!myReader.HasRows)
            {
                // Do something if the reader doesn't have data. This could be an exception 
                // or other indication to the user that something unexpected happened.
            }
            while (myReader.Read())
            {
                // The exception handler will trigger if the expected fields don't exist 
                // in the returned row
                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());
            }
        }
    }
    // Put a breakpoint on the following curly brace so you can establish that the 
    // code is working without problems (assuming your ducks are otherwise all in a row)
}
catch (Exception ex)
{
    // do something appropriate to indicate whatever problem arises
    // if you're debugging, put a breakpoint on either of the curly braces to stup exceution
}


什么类型的数据会话[id]包含?



您要将empusername与id进行比较。



你必须将empuserid与id进行比较。

what type of data session["id"] consist?

you are comparing empusername with id.

you have to compare empuserid with id.


这篇关于我想基于会话ID在ASP.NET文本框上显示数据。但页面上没有任何反应。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆