3

クエリ文字列でフィルタリングしたいLinqDataSourceに接続されたFormViewがあります。基本的に、IDがクエリ文字列を介して渡される場合は、そのレコードのみを表示します。それ以外の場合は、IDが指定されていない場合は、すべてを表示します。いくつかの異なる方法を使用してこれを機能させることができましたが、FormViewにデータを保存しようとするたびに、例外が発生しますOperator '==' incompatible with operand types 'Int32?' and 'Object'

上記の例外は、を呼び出すと発生しますFormView.UpdateItem(true)。LinqDataSourceをフィルタリングする方法は次のとおりです。

protected void ldsIncidents_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
    if (!String.IsNullOrEmpty(Request.QueryString["id"]))
    {
        e.Result = db.Incidents.Where(i => i.Incident_Number == Convert.ToInt32(Request.QueryString["id"]));
    }
}

を呼び出すと例外がスローされますFormView.UpdateItem(true)。上記のイベントからコードを削除するとすぐにSelecting、すべてが正常に機能しますが、データをフィルタリングできなくなります。また、WhereParametersをLinqDataSourceに動的に追加しようとしましたが、同じことが起こります。なぜこれが起こっているのか、それを修正する方法を誰かが知っていますか?それが役立つ場合は、これが私のスタックトレースです:

Exception Type: System.Web.Query.Dynamic.ParseException
Message: Operator '==' incompatible with operand types 'Int32?' and 'Object'
Stack Trace:
   at System.Web.Query.Dynamic.ExpressionParser.CheckAndPromoteOperands(Type signatures, String opName, Expression& left, Expression& right, Int32 errorPos)
   at System.Web.Query.Dynamic.ExpressionParser.ParseComparison()
   at System.Web.Query.Dynamic.ExpressionParser.ParseLogicalAnd()
   at System.Web.Query.Dynamic.ExpressionParser.ParseLogicalOr()
   at System.Web.Query.Dynamic.ExpressionParser.ParseExpression()
   at System.Web.Query.Dynamic.ExpressionParser.Parse(Type resultType)
   at System.Web.Query.Dynamic.DynamicExpression.ParseLambda(ParameterExpression[] parameters, Type resultType, String expression, Object[] values)
   at System.Web.Query.Dynamic.DynamicExpression.ParseLambda(Type itType, Type resultType, String expression, Object[] values)
   at System.Web.Query.Dynamic.DynamicQueryable.Where(IQueryable source, String predicate, Object[] values)
   at System.Web.UI.WebControls.DynamicQueryableWrapper.Where(IQueryable source, String predicate, Object[] values)
   at System.Web.UI.WebControls.QueryableDataSourceView.ExecuteQueryExpressions(IQueryable source, QueryContext context)
   at System.Web.UI.WebControls.QueryableDataSourceView.ExecuteQuery(IQueryable source, QueryContext context)
   at System.Web.UI.WebControls.LinqDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments)
   at System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback)
   at System.Web.UI.WebControls.DataBoundControl.PerformSelect()
   at System.Web.UI.WebControls.BaseDataBoundControl.DataBind()
   at System.Web.UI.WebControls.GridView.DataBind()
   at System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound()
   at System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls()
   at System.Web.UI.Control.EnsureChildControls()
   at System.Web.UI.WebControls.CompositeDataBoundControl.get_Controls()
   at System.Web.UI.WebControls.DataBoundControlHelper.ExtractValuesFromBindableControls(IOrderedDictionary dictionary, Control container)
   at System.Web.UI.WebControls.DataBoundControlHelper.ExtractValuesFromBindableControls(IOrderedDictionary dictionary, Control container)
   at System.Web.UI.WebControls.DataBoundControlHelper.ExtractValuesFromBindableControls(IOrderedDictionary dictionary, Control container)
   at System.Web.UI.WebControls.DataBoundControlHelper.ExtractValuesFromBindableControls(IOrderedDictionary dictionary, Control container)
   at System.Web.UI.WebControls.DataBoundControlHelper.ExtractValuesFromBindableControls(IOrderedDictionary dictionary, Control container)
   at System.Web.UI.WebControls.DataBoundControlHelper.ExtractValuesFromBindableControls(IOrderedDictionary dictionary, Control container)
   at System.Web.UI.WebControls.DataBoundControlHelper.ExtractValuesFromBindableControls(IOrderedDictionary dictionary, Control container)
   at System.Web.UI.WebControls.DataBoundControlHelper.ExtractValuesFromBindableControls(IOrderedDictionary dictionary, Control container)
   at System.Web.UI.WebControls.FormView.ExtractRowValues(IOrderedDictionary fieldValues, Boolean includeKeys)
   at System.Web.UI.WebControls.FormView.HandleUpdate(String commandArg, Boolean causesValidation)
   at System.Web.UI.WebControls.FormView.UpdateItem(Boolean causesValidation)
   at PRIDE.Pages.Incidents.View.btnSaveIncident_Click(Object sender, EventArgs e) in C:\Users\hopfnejo\Documents\Development Projects\PRIDE\PRIDE\Pages\Incidents\View.aspx.cs:line 317

編集

FormViewのItemUpdatingイベントの内容は次のとおりです。

protected void fvIncident_ItemUpdating(object sender, FormViewUpdateEventArgs e)
{
    TabContainer tc = fvIncident.FindControl("tcMain") as TabContainer;

    e.NewValues["Division"] = (tc.Tabs[0].FindControl("cboDivision") as DropDownList).SelectedValue;
    e.NewValues["Safety"] = (tc.Tabs[0].FindControl("chkSafety") as CheckBox).Checked;
    e.NewValues["Quality"] = (tc.Tabs[0].FindControl("chkQuality") as CheckBox).Checked;
    e.NewValues["Cost"] = (tc.Tabs[0].FindControl("chkCost") as CheckBox).Checked;
    e.NewValues["Production"] = (tc.Tabs[0].FindControl("chkProduction") as CheckBox).Checked;
    e.NewValues["Environment"] = (tc.Tabs[0].FindControl("chkEnvironment") as CheckBox).Checked;
    e.NewValues["Department"] = (tc.Tabs[0].FindControl("cboDepartment") as DropDownList).SelectedValue;
    e.NewValues["Area"] = (tc.Tabs[0].FindControl("cboArea") as DropDownList).SelectedValue;
    e.NewValues["Initiated_By"] = (tc.Tabs[0].FindControl("txtInitiatedBy") as TextBox).Text;
    e.NewValues["KRA_Notes"] = (tc.Tabs[0].FindControl("txtKRANotes") as TextBox).Text;
    e.NewValues["Incident_type"] = (tc.Tabs[0].FindControl("cboIncidentType") as DropDownList).SelectedValue;
    e.NewValues["Aspect_Affected"] = (tc.Tabs[0].FindControl("cboAspectAffected") as DropDownList).SelectedValue;

    e.NewValues["Incident_Level"] = (tc.Tabs[1].FindControl("cboIncidentLevel") as DropDownList).SelectedValue;
    e.NewValues["ADM_Testing_Within_Guidelines"] = (tc.Tabs[1].FindControl("chkADMTestingWithinGuidelines") as CheckBox).Checked;
    e.NewValues["ADM_Testing_Required"] = (tc.Tabs[1].FindControl("chkADMTestingRequired") as CheckBox).Checked;
    e.NewValues["Number_Of_People_Involved"] = (tc.Tabs[1].FindControl("txtNumPeopleInvolved") as TextBox).Text;

    e.NewValues["Mill_State"] = (tc.Tabs[2].FindControl("cboMillState") as DropDownList).SelectedValue;
    e.NewValues["Area_Downtime"] = (tc.Tabs[2].FindControl("txtDowntime") as TextBox).Text;
    e.NewValues["Production_Amount_Lost"] = (tc.Tabs[2].FindControl("txtProductionAmountLost") as TextBox).Text;
    e.NewValues["Production_Actual_or_Estimate"] = (tc.Tabs[2].FindControl("cboProductionActualOrEstimate") as DropDownList).SelectedValue;
    e.NewValues["Production_Units"] = (tc.Tabs[2].FindControl("cboProductionUnits") as DropDownList).SelectedValue;

    e.NewValues["Environmental_Impact"] = (tc.Tabs[3].FindControl("txtEnvironmentalImpact") as TextBox).Text;
    e.NewValues["Environmental_Specialist_Notified"] = (tc.Tabs[3].FindControl("cboEnvironmentalSpecialistNotified") as DropDownList).SelectedValue;
    e.NewValues["Environmental_Incident_Reportable_to_Government"] = (tc.Tabs[3].FindControl("cboEnvironmentalIncidentReportableToGovernment") as DropDownList).SelectedValue;
    e.NewValues["Environmental_Governement_Agency_Involved"] = (tc.Tabs[3].FindControl("txtGovernmentAgencyInvolved") as TextBox).Text;
    if (String.IsNullOrEmpty((tc.Tabs[3].FindControl("txtDateReported") as TextBox).Text))
        e.NewValues["Environmental_Date_Reported"] = null;
    else
        e.NewValues["Environmental_Date_Reported"] = (tc.Tabs[3].FindControl("txtDateReported") as TextBox).Text;
    e.NewValues["Environmental_Action_Taken"] = (tc.Tabs[3].FindControl("txtEnforcementActionTaken") as TextBox).Text;
    e.NewValues["Environmental_Reference_Number"] = (tc.Tabs[3].FindControl("txtReferenceNumber") as TextBox).Text;

    e.NewValues["Procedures"] = (tc.Tabs[4].FindControl("txtProceduresAffected") as TextBox).Text;
    e.NewValues["Referances"] = (tc.Tabs[4].FindControl("txtReferences") as TextBox).Text;

    e.NewValues["Reviewed_By__BUL_"] = (tc.Tabs[8].FindControl("txtReviewedByBUL") as TextBox).Text;
    if (String.IsNullOrEmpty((tc.Tabs[8].FindControl("txtSignOffDateBUL") as TextBox).Text))
        e.NewValues["Sign_Off_Date__BUL_"] = null;
    else
        e.NewValues["Sign_Off_Date__BUL_"] = (tc.Tabs[8].FindControl("txtSignOffDateBUL") as TextBox).Text;
    if (String.IsNullOrEmpty((tc.Tabs[8].FindControl("txtPlannedReviewDate") as TextBox).Text))
        e.NewValues["Planned_Review_Date"] = null;
    else
        e.NewValues["Planned_Review_Date"] = (tc.Tabs[8].FindControl("txtPlannedReviewDate") as TextBox).Text;
    if (String.IsNullOrEmpty((tc.Tabs[8].FindControl("txtPlannedCompletionDate") as TextBox).Text))
        e.NewValues["Planned_Completion_Date"] = null;
    else
        e.NewValues["Planned_Completion_Date"] = (tc.Tabs[8].FindControl("txtPlannedCompletionDate") as TextBox).Text;
    e.NewValues["Signed_Off_By__BGL_"] = (tc.Tabs[8].FindControl("txtSignedOffByBGL") as TextBox).Text;
    if (String.IsNullOrEmpty((tc.Tabs[8].FindControl("txtSignOffDateBGL") as TextBox).Text))
        e.NewValues["Sign_Off_Date__BGL_"] = null;
    else
        e.NewValues["Sign_Off_Date__BGL_"] = (tc.Tabs[8].FindControl("txtSignOffDateBGL") as TextBox).Text;
    if (String.IsNullOrEmpty((tc.Tabs[8].FindControl("txtActualReviewDate") as TextBox).Text))
        e.NewValues["Actual_Review_Date"] = null;
    else
        e.NewValues["Actual_Review_Date"] = (tc.Tabs[8].FindControl("txtActualReviewDate") as TextBox).Text;
    if (String.IsNullOrEmpty((tc.Tabs[8].FindControl("txtActualCompletionDate") as TextBox).Text))
        e.NewValues["Actual_Completion_Date"] = null;
    else
        e.NewValues["Actual_Completion_Date"] = (tc.Tabs[8].FindControl("txtActualCompletionDate") as TextBox).Text;
    e.NewValues["Signed_Off_By__Mill_Manager_"] = (tc.Tabs[8].FindControl("txtSignedOffByMillManager") as TextBox).Text;
    if (String.IsNullOrEmpty((tc.Tabs[8].FindControl("txtSignOffDateMillManager") as TextBox).Text))
        e.NewValues["Sign_Off_Date__Mill_Manager_"] = null;
    else
        e.NewValues["Sign_Off_Date__Mill_Manager_"] = (tc.Tabs[8].FindControl("txtSignOffDateMillManager") as TextBox).Text;
}

編集2

LinqDataSourceとFormViewに関連するハンドラーがある唯一のイベントにブレークポイントを設定しようとしましたが、例外が発生する前にイベントが発生することはありません。イベントは次のとおりです。

  • LinqDataSource_Selecting
  • FormView_ItemUpdating
  • FormView_DataBound

[保存]ボタンをクリックしても、ブレークポイントに到達しません。ここで爆発しますが、あまり役に立ちません。

スクリーンショット

これが私のFormView宣言です:

<asp:FormView ID="fvIncident" runat="server" DefaultMode="Edit" 
    DataSourceID="ldsIncidents" DataKeyNames="Incident_Number" 
    AllowPaging="True" CssClass="full" ondatabound="fvIncident_DataBound" 
    onitemupdating="fvIncident_ItemUpdating">

そして私のLinqDataSource宣言:

<asp:LinqDataSource ID="ldsIncidents" runat="server" 
    ContextTypeName="PRIDE.PRIDEDataContext" EnableUpdate="True" EntityTypeName="" 
    TableName="Incidents" OrderBy="Incident_Number DESC" 
    onselecting="ldsIncidents_Selecting">
</asp:LinqDataSource>
4

4 に答える 4

0

多分あなたはTryParseを使うべきです

protected void ldsIncidents_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
    if (!String.IsNullOrEmpty(Request.QueryString["id"]))
    {
        int id;
        e.Result = db.Incidents.Where(i => i.Incident_Number == Int32.TryParse(Request.QueryString["id"], out id) ? id : 0);
    }
}

編集

以下の私の提案を確認するには、これを実行して値を確認できます

    protected void ldsIncidents_Selecting(object sender, LinqDataSourceSelectEventArgs e)
    {
        if (!String.IsNullOrEmpty(Request.QueryString["id"]))
        {
            int id;

            // now you can check the values you gat;
            var queryString = Request.QueryString["id"];
            int iNr =Int32.TryParse(queryString, out id) ? id : 0;

            e.Result = db.Incidents.Where(i => i.Incident_Number == iNr);
        }
    }
于 2013-03-15T16:08:19.043 に答える
0

これを試して。

IQueryable<Incident> query = this.context.Incidents;
string whereCaluse = String.Format("Incident_Value=={0}",Request.QueryString["id"]);
query.Where(whereClause);
e.Result=query;
于 2013-03-11T21:16:19.487 に答える
0

i.Incident_Number例外の詳細を見ると、それはオブジェクト型である可能性があると言えます。

i.Incident_Numberしたがって、 linq 式で int への変換も試すことができます。お気に入り:

protected void ldsIncidents_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
    if (!String.IsNullOrEmpty(Request.QueryString["id"]))
    {
        e.Result = db.Incidents.Where(i => Convert.ToInt32(i.Incident_Number) == Convert.ToInt32(Request.QueryString["id"]));
    }
}
于 2013-03-15T08:20:56.183 に答える