【发布时间】:2017-07-26 07:11:42
【问题描述】:
我是 ASP.Net 的新手,
从上一页传递用户 ID 时,我在从数据库中检索员工信息时遇到问题。 从第 1 页的数据网格中选择时,我设法传递了用户 ID,但是在加载第 2 页时,它给了我一个错误:无效的列名“传递的字符串值”
下面是我的代码:
第 1 页:
protected void imgBtnView_Click(object sender, ImageClickEventArgs e)
{
ImageButton imgBtn = (ImageButton)sender;
string UserID = imgBtn.CommandArgument;
Response.Redirect("Employee.aspx?UserID=" + UserID);
}
第 2 页:
页面加载
protected void Page_Load(object sender, EventArgs e)
{
txtEmployeeNo.Text = Request.QueryString["UserID"];
FillFields(txtEmployeeNo.Text);
}
方法填充字段
private void FillFields(string User_ID)
{
String commandString = @"SELECT * FROM [dbo].[Tbl_Employee] WHERE
[UserID] = "+ User_ID;
DataRow dr = Global.StartQuery(commandString).Rows[0]; -- > global class
txtEmployeeNo.Text = User_ID;
txtFirstName.Text = dr["FirstName"].ToString();
txtLastName.Text = dr["LastName"].ToString();
}
请告诉我代码中有什么问题。
提前谢谢你
【问题讨论】:
-
您能在查询字符串中看到您的用户 ID。在查询字符串中传递用户 ID 是个坏主意,而是使用会话。如果您正在获取用户 ID,则在 commandString 中通过 Conver.ToString() 方法或 User_ID.ToString() 将 User_ID 转换为 String
-
你能提供更多关于会话方法的建议吗?
-
Response.Redirect("Employee.aspx?UserID=" + UserID);更好:Response.Redirect("Employee.aspx?UserID=" + Server.UrlEncode(UserID)); -
private void FillFields(string User_ID)更好:private void FillFields(string userId) -
txtFirstName.Text = dr["FirstName"].ToString();可空字段异常。检查 dr["foo"] 是否为空