【发布时间】:2016-02-21 18:50:13
【问题描述】:
试图获取当前登录的用户用户 ID,并在后台代码的 SQL 查询中使用它来填充网格视图,但它没有任何想法我做错了什么。我不断收到空值错误。另外,如果可能的话,我会尝试仅使用 Microsoft.AspNet.Identity
页面加载
if (!IsPostBack)
{
SqlDataSource SqlDataSource1 = new SqlDataSource();
SqlDataSource1.ID = "SqlDataSource1";
this.Page.Controls.Add(SqlDataSource1);
SqlDataSource1.InsertParameters["luser"].DefaultValue = User.Identity.GetUserId();
SqlDataSource1.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
SqlDataSource1.SelectCommand = "SELECT top 3 UserId, Friending from Friendreq Where Friending = @luser";
GridView1.DataSource = SqlDataSource1;
GridView1.DataBind();
}
更新:
通过将请求直接添加到 SQL 查询中,我能够得到我想要的结果。有什么理由我不应该添加它吗
SqlDataSource SqlDataSource1 = new SqlDataSource();
SqlDataSource1.ID = "SqlDataSource1";
this.Page.Controls.Add(SqlDataSource1);
SqlDataSource1.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
SqlDataSource1.SelectCommand = "SELECT top 3 UserId, Friending from Friendreq Where Friending = '"+User.Identity.GetUserId()+"'";
GridView1.DataSource = SqlDataSource1;
GridView1.DataBind();
解决方案 = Friending = '"+User.Identity.GetUserId()+"'";
【问题讨论】:
-
@Andrei 我在 SqlDataSource1.InsertParameters["luser"].DefaultValue = User.Identity.GetUserId(); 行上得到错误,错误是“异常App_Web_3lu2kaoo.dll 中发生了“System.NullReferenceException”类型,但未在用户代码中处理”