需要帮助来解决和理解此错误:异常详细信息:System.InvalidOperationException:2

时间:2012-04-23 18:44:08

标签: c# database razor webmatrix

我收到了这个错误:

  

异常详细信息:System.InvalidOperationException:2:字符串截断:max = 0,len = 1,value ='3'。

从这组代码:

var reqcategory=Request["category"];
foreach (var cat in reqcategory)
{
    var sql5 = "SELECT Type.PreReq1, Type.PreReq2, 
                       (CASE WHEN (Type.PreReq1 IS NOT NULL)
                             AND (PermitApp1.RPClass IS NULL) 
                             AND (PermitApp1.RPCategory IS NULL) 
                        THEN 1 ELSE 0 END) AS missing1, 
                       (CASE WHEN (Type.PreReq2 IS NOT NULL) 
                             AND (PermitApp2.RPClass IS NULL)
                             AND (PermitApp2.RPCategory IS NULL) 
                        THEN 1 ELSE 0 END) AS missing2 
                FROM Type LEFT JOIN PermitApp AS PermitApp1 
                     ON (Type.PreReq1=PermitApp1.RPClass) 
                       OR (Type.PreReq1=PermitApp1.RPCategory) 
                           AND ( PermitApp1.CDSID = @0 ) 
                           AND (PermitApp1.MDecision='1') 
                          LEFT JOIN PermitApp AS PermitApp2 
                     ON (Type.PreReq2=PermitApp2.RPClass) 
                       OR (Type.PreReq2=PermitApp2.RPCategory) 
                           AND ( PermitApp2.CDSID = @1 ) 
                           AND (PermitApp2.MDecision='1') 
                WHERE Type.PType = @2";

    var result = db.QuerySingle(sql5, myCDSID, username, cat);
    var miss1 = result.missing1;
    var miss2 = result.missing2;

    if(miss1 == '1' || miss2 == '1'){
        ModelState.AddError("missing", 
                  "You have not met the Pre-Requisites for "+ cat +" yet.");
    } else if (miss1 == '0' && miss2 == '0'){
        Session["license"] = Request["licence"];
        Session["from"] = Request["from"];
        Session["to"] = Request["to"];
        Session["group"] = Request["group"];
        Session["class1"] = Request["class1"];
        Session["category1"] = Request["category1"];
        Session["class"] = Request["class"];
        Session["category"] = Request["category"];
        Response.Redirect("~/Questionnaire");
    }
}

突出显示的一行是:

var result = db.QuerySingle(sql5, myCDSID, username, cat);

我不明白错误页面上提到了什么,我找不到与我相同的情况。感谢

1 个答案:

答案 0 :(得分:0)

好吧,我不能代表您的SQL查询,因为我认为我从未见过这样的查询,但我的经验是,在进行查询并返回错误时,提供它根本就是一个有效的sql查询,这意味着一个字符串必须被“截断”,因为数据库字段被设置为存储的字符少于你试图传入字段的字符数。

也就是说,作为一个例子,一个字符串,比如长60个字符,被传递到一个字段集中,比如nvarchar(50)。尝试调整允许存储在字符串字段中的字节数。

不完全确定这是否是你的问题,因为我自己还是很多新手,但我认为这可能是一种可能性,因为我已经看到过几次发生在我身上的事情之前。

希望我帮忙!