Linq数据源WhereParameters的值为“\”

时间:2013-05-14 20:00:49

标签: c# asp.net sql sql-server linq

使用LinQ to SQL的ASP.NET / C#项目 我有listbox,其中包含来自数据库的值,用于过滤Linq数据源。 一切正常,直到我在列表框中包含“\”的值。例如“DOMAIN \ Group Name”。任何帮助,将不胜感激!

if (ddlAppGroup.SelectedValue != "*")
            {
                LinqDataSource1.WhereParameters.Add(new Parameter("ApplicationGroup", System.Data.DbType.Int16));
                LinqDataSource1.WhereParameters["ApplicationGroup"].DefaultValue = ddlAppGroup.SelectedValue;

                if (LinqDataSource1.Where == "")
                {
                    LinqDataSource1.Where = "ApplicationGroup == @ApplicationGroup";
                }
                else
                { LinqDataSource1.Where = LinqDataSource1.Where + " && ApplicationGroup == @ApplicationGroup"; }
            }

这是参数的样子:

    LinqDataSource1.WhereParameters[0]
{ApplicationGroup}
    ConvertEmptyStringToNull: true
    DbType: Int16
    DefaultValue: "DOMAIN\\Group-Name"
    Direction: Input
    Name: "ApplicationGroup"
    Size: 0
    Type: Empty

这就是报告的错误

    {"Input string was not in a correct format."}
    [System.FormatException]: {"Input string was not in a correct format."}
    Data: {System.Collections.ListDictionaryInternal}
    HelpLink: null
    HResult: -2146233033
    InnerException: null
    Message: "Input string was not in a correct format."
    Source: "mscorlib"
    StackTrace: "   at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)\r\n   at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)\r\n   at System.Int16.Parse(String s, NumberStyles style, NumberFormatInfo info)\r\n   at System.String.System.IConvertible.ToInt16(IFormatProvider provider)\r\n   at System.Convert.ChangeType(Object value, TypeCode typeCode, IFormatProvider provider)\r\n   at System.Web.UI.WebControls.Parameter.GetValue(Object value, String defaultValue, TypeCode type, Boolean convertEmptyStringToNull, Boolean ignoreNullableTypeChanges)\r\n   at System.Web.UI.WebControls.Parameter.GetValue(Object value, String defaultValue, DbType dbType, Boolean convertEmptyStringToNull, Boolean ignoreNullableTypeChanges)\r\n   at System.Web.UI.WebControls.Parameter.GetValue(Object value, Boolean ignoreNullableTypeChanges)\r\n   at System.Web.UI.WebControls.Parameter.get_ParameterValue()\r\n   at System.We
b.UI.WebControls.ParameterCollection.GetValues(HttpContext context, Control control)\r\n   at System.Web.UI.WebControls.QueryableDataSourceView.CreateQueryContext(DataSourceSelectArguments arguments)\r\n   at System.Web.UI.WebControls.LinqDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments)\r\n   at System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback)\r\n   at System.Web.UI.WebControls.DataBoundControl.PerformSelect()\r\n   at System.Web.UI.WebControls.BaseDataBoundControl.DataBind()\r\n   at System.Web.UI.WebControls.GridView.DataBind()\r\n   at PYOCEnrollment.Admin.AllRequests.btnApply_Click(Object sender, EventArgs e) in C:\\Projects\\PYOCEnrollment\\PYOCEnrollment\\EUE\\AllRequests.aspx.cs:line 265"
    TargetSite: {Void StringToNumber(System.String, System.Globalization.NumberStyles, NumberBuffer ByRef, System.Globalization.NumberFormatInfo, Boolean)}

1 个答案:

答案 0 :(得分:1)

这不会飞:

DbType: Int16
DefaultValue: "DOMAIN\\Group-Name"

考虑将DbType更改为nvarchar类型之一。