0

在将另一个页面上的条目插入数据库之后,查询它的值时出现错误。错误如下:

Value cannot be null or an empty string.
Parameter name: commandText

堆栈跟踪看起来像这样(不确定这是否有帮助):

[ArgumentException: Value cannot be null or an empty string.
Parameter name: commandText]
   WebMatrix.Data.Database.QueryValue(String commandText, Object[] args) +20791
   ASP._Page_EntryViewer_cshtml.Execute() in c:\Users\administrator.OKMCITY\Documents\My Web Sites\Persons Of Interest\EntryViewer.cshtml:101
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +197
   System.Web.WebPages.WebPage.ExecutePageHierarchy(IEnumerable`1 executors) +69
   System.Web.WebPages.WebPage.ExecutePageHierarchy() +151
   System.Web.WebPages.StartPage.RunPage() +17
   System.Web.WebPages.StartPage.ExecutePageHierarchy() +62
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +76
   System.Web.WebPages.WebPageHttpHandler.ProcessRequestInternal(HttpContextBase httpContext) +114

发生错误的行(第 101 行):

Line 99:     var db = Database.Open("PersonsOfInterest");
Line 100:
Line 101:    var activeID = db.QueryValue((string)Session["activeEntrySelectQueryString"], (string)Session["gPOIName"], (string)Session["gDateLastModified"], (string)Session["gGender"], (string)Session["gRace"], (string)Session["gHeight"], (string)Session["gWeight"], (string)Session["gHairColor"], (string)Session["gEyeColor"], (string)Session["gDOB"], (string)Session["gAge"], (string)Session["gSS"], (string)Session["gDL"], (string)Session["gDOC"], (string)Session["gVehicleTag"], (string)Session["gFBI"], (string)Session["gOfficer"], (string)Session["gAdditionalDescriptors"], (string)Session["gHomePhone"], (string)Session["gAliases"], (string)Session["gSourceOfInformation"], (string)Session["gAddress"], (string)Session["gAddressInformation"], (string)Session["gKnownAssociates"], (string)Session["gVehicleDescription"], (string)Session["gCellPhone"], (string)Session["gCellPhone2"], (string)Session["gCellPhone3"], (string)Session["gPOICautions"], (string)Session["gComments"], (string)Session["gWorkPhone"], (string)Session["gSummarizedIncidents"], (string)Session["gPOILastName"]);
Line 102:    Session["gEntryID"] = activeID;
Line 103:   

我似乎找不到这个错误的原因,因为它发生在用户“保存”他/她进入数据库之后,但偶尔发生并且看似随机。该页面尝试查询该条目的唯一主键的值。您在上面看到的会话变量中存储的值是从以前刚刚用于将新条目插入数据库的值中存储的(我必须这样做,因为主键“是身份”并且值会自动递增每个条目添加)。

可能有更好(更简单)的方法来做我想做的事情,但我认为这样做比仅在主键上使用 MAX(聚合函数)更一致。

关于这个问题,我可能可以提供更多信息,但我真的不想用不必要的信息填充这个页面(如果我还没有的话)。只要让我知道是否还有其他我可以提供的。

还有一些注意事项:

'Session["gDateLastModified"]' 是从使用 JavaScript 自动绘制的表单上的字段中获取的,因此该值(精确到秒)应该(与此查询中的所有其他变量一样)与值匹配输入到数据库中(是的,我确信用户已打开 JavaScript 并正常运行)。

此外,这可能很明显,但是第 101 行的 QueryValue 命令将第一个参数作为选择查询字符串,当然使用参数化查询,其余参数是检索数据库中的行所必须匹配的。

请记住,此错误仅偶尔发生。

如果我添加了太多信息(或遗漏了一些信息),我真的很抱歉,但我不知道从哪里开始假设这个问题可能是什么,所以不确定所有可能会帮助任何试图帮助我的人。只要让我知道是否还有其他我可以提供的。

更新

我现在所知道的是,只要它是“IsPost”,就会故意填充一个字符串,然后该字符串用于直接填充有问题的 Session 变量。我知道会话变量实际上是 cookie,但可以肯定没有人手动清除它们(他们必须在点击提交按钮后和加载下一页之前,在这个过程的中间某个地方这样做),还有什么可能导致该值被清除?我假设 commandText 是一个变量,它在 db.QueryValue 期间传递了我的查询字符串的值,这是正确的吗?

4

1 回答 1

1

在使用它们之前,我会检查会话是否确实具有这些值:

var activeEntrySelectQuery = (string) Session["activeEntrySelectQueryString"];
if (string.IsNullOrEmpty(activeEntrySelectQuery)) {
    throw new ArgumentException("The active entry select query has gone missing.");
}

您可以添加适当的附加信息(并创建您自己的异常类型)来帮助您找到问题。但我怀疑这是问题所在 - 会话不包含您期望的值。

于 2012-12-10T14:42:56.160 回答