【问题标题】:stackoverflow exception in DataView row filterDataView行过滤器中的stackoverflow异常
【发布时间】:2010-09-13 14:11:08
【问题描述】:

我在 DataView 行过滤器属性中收到“System.Data.dll 中发生类型为 'System.StackOverflowException' 的未处理异常”。我没有得到任何堆栈跟踪。 所以任何人都可以帮助我。请在下面找到我在 filterView 中遇到错误的代码。

                DataSet metalAttributeDS = LoadItemData();    //loads the static dataset
                DataTable metalDataTable = metalAttributeDS.Tables["FilterTable"];
                DataView metalfilterView = new DataView(metalDataTable);
                metalfilterView.ApplyDefaultSort = true;
                metalfilterView.RowFilter = queryBuilder +
                                            string.Format(
                                                " And AttributeName='Metal' and AttributeValueID in ({0})",
                                                string.Join(",", AttributeValueID.ToArray()));      //forms query condition dynamically.

                var res = from DataRowView rowView in metalfilterView select rowView["ItemID"].ToString();

                int countParam = 0;
                queryBuilder.AppendFormat(" and (");
                foreach (string id in res)
                {
                    countParam++;
                    queryBuilder.AppendFormat(" ItemID = '{0}'", id);
                    if (res.Count() > countParam)
                    {
                        queryBuilder.Append(" Or");
                    }
                }
                queryBuilder.Append(" )");
            }


            DataSet dataSet = LoadItemData();       //loads the static dataset
            DataTable dataTable = dataSet.Tables["FilterTable"];
            DataView filterView = new DataView(dataTable);
            filterView.ApplyDefaultSort = true;

                LogHelper.LogInfo(GetType(), "filterView.RowFilter");
                filterView.RowFilter = queryBuilder.ToString(); //      throws error

谢谢, Mehul Makwana。

【问题讨论】:

  • 问题出在哪里。我认为,过滤器正在尝试将字符串 id 与 Guid id 进行比较并得到错误。
  • 堆栈溢出异常不是因为这个。我已经用 aristos 建议的内容更改了我的代码,这给了我无法在 System.Guid 上执行 = 操作的错误。

标签: c# asp.net stack-overflow dataview system.data


【解决方案1】:

也许你用 or 创建了一条大线,无法处理......如果你试试这个......?

   StringBuilder sbTheOr = new StringBuilder();

    foreach (string id in res)
    {
        sbTheOr.Append(',');
        sbTheOr.Append(id);
    }


    if (sbTheOr.Length > 0)
    {
        // remove the first ,
        sbTheOr.Remove(0, 1);
        queryBuilder.AppendFormat(" and ItemID IN (" + sbTheOr.ToString() + ")");
    }

【讨论】:

  • 是 res 包含一个巨大的结果集。所以这可能是问题?好吧,尝试使用您提供的 sn-p 并让您知道。感谢帮助。 :)
  • 嘿朋友,我尝试了你的建议。它现在给出“'dbb' 运算符后缺少操作数”错误。
  • 在堆栈跟踪中它告诉我“无法对 System.Guid 和 System.String 执行 '=' 操作。”
【解决方案2】:

您能否验证一下“queryBuilder”中内置了什么?我认为最后可能会有一些 'And'/'Or'/开或闭大括号

【讨论】:

  • 我检查了我的查询生成器值。它包含格式完美的查询。 :(
【解决方案3】:

我在 Aristos 的帮助下解决了这个问题,但是我对 Aristos sn-p 做了一些修改,

        foreach (string id in res)
                {
                    sbTheOr.Append(',');
                    Guid guid = new Guid(id);
                    sbTheOr.AppendFormat("Convert('{0}','System.Guid')",guid);
                }

                if (sbTheOr.Length > 0)
                {
                    // remove the first ,
                    sbTheOr.Remove(0, 1);
                    queryBuilder.AppendFormat(" and ItemID in ({0})",sbTheOr.ToString());
                }

所以堆栈溢出异常只是因为巨大的结果。并且发现了新的东西,我们可以使用 Convert(expression, type) 语法在 Guid 列上使用 RowFilter。

谢谢每一位,

梅胡尔·马克瓦纳。

【讨论】:

    【解决方案4】:

    简化解决方案

     StringBuilder sb =new StringBuilder();
     res.ToList().ForEach(x => sb.Append(", " + String.Format("Convert('{0}','System.Guid')", new Guid(x))));
     var output = sb.ToString().Trim(',');
    

    【讨论】:

      猜你喜欢
      • 2012-02-26
      • 1970-01-01
      • 2016-09-01
      • 1970-01-01
      • 2011-05-18
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      相关资源
      最近更新 更多