问题描述
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.
推荐答案
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在ASP.NET文本框上显示数据。但页面上没有任何反应。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!