【问题标题】:Get a Value from a SqlDataSource object's SelectCommand, then use the value in an If Statement从 SqlDataSource 对象的 SelectCommand 中获取值,然后在 If 语句中使用该值
【发布时间】:2019-04-06 05:29:50
【问题描述】:

所以我需要从查询中获取值 - 在 AS TrueOrFalse 字段中。 我很困惑我应该如何去做。

在基本思想/措辞中:我需要查看 BookID 中是否存在一个值(与 RequestQuery 匹配),然后从我的 RatingsReviews 表中查看 UserID 中是否存在一个值(匹配/来自会话变量)。

我无权访问 .ExecuteScalar 方法。

我知道查询应该可以工作,它可以在 Access 中工作(稍作修改以使用参数值),并且我正在使用“BookID”RequestQuery 和“UserID”会话变量用于其他按预期工作的事情。

这是我的 WebPageName.aspx.cs 文件中的内容:

  SqlDataSource objDS2 = new SqlDataSource();

    objDS2.ProviderName = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ProviderName;
    objDS2.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

    //Puts  the Amount of times that the Session User ID (who's logged in) appears with the Requested Query String of the Book ID
    objDS2.SelectCommand = "SELECT COUNT (*) AS TrueOrFalse FROM [RatingsReviews] WHERE ([UserID]" + Session["UserID"].ToString() + " AND [BookID] =" + Request.QueryString["id"].ToString() + ")";

    //Get Value from the TrueOrFalse Field from the QUERY somehow


    //IF the The value in TrueOrFalse Field is 0, 
     // then allow and show the Panel, else hide panel - so they cannot enter a review

     // if (TrueOrFalse == 0/false) 
    // { 

          //is someone logged in?
        if (Session["UserID"] != null)
        {
          //if someone is logged in, and they are visiting
            if (Session["UserID"].ToString().Equals(Request.QueryString["id"]))
            {
            PanelUserRating.Visible = true;
            }
    else
       {
         PanelUserRating.Visible = false;
        }

【问题讨论】:

    标签: c# sql asp.net .net ms-access


    【解决方案1】:

    我看到您只填充了 SelectCommand 属性,因为您从未执行过选择命令。您可以使用 Select 方法执行它并返回结果。

    IEnumerable result = _objDS2.Select(new DataSourceSelectArguments());
    

    点击此链接以正确实施: here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-27
      • 2019-07-27
      • 1970-01-01
      • 1970-01-01
      • 2021-08-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多