ObjectDatasource更新方法 - 使用包含子对象的Generic对象参数不起作用

时间:2013-04-02 13:19:45

标签: c# gridview parameters objectdatasource business-logic

实体

public class Calculation
{
    public int UniqueID { get; set; }
    public int Value1 { get; set; }
    public int Value2 { get; set; }
    public int Result { get; set; }
    public Operation Operation { get; set; }
    public DateTime EntryDateTime { get; set; }
}

 public class Operation
{
    public int OperationID { get; set; }
    public string OperationName { get; set; }
}

业务逻辑层

public bool UpdateCalculation(Calculation calcObject)
    {
        bool returnVal = false;
        try
        {
            if (GetCalculationByID(calcObject) != null)
            {
                returnVal = calcDAL.UpdateCalculation(calcObject);
                if (returnVal)
                    return returnVal;
                else
                    throw new ApplicationException(ResourceHelper.GetErrorResourceValue("MSG003"));
            }
            else
                throw new ApplicationException(ResourceHelper.GetErrorResourceValue("MSG006"));
        }
        catch (Exception exp)
        {
            LoggingHelper.WriteLog(exp, MethodBase.GetCurrentMethod(), TraceEventType.Error);
            throw exp;
        }
    }

数据访问层:

public bool UpdateCalculation(Calculation calcObject)
    {
        try
        {
            object[] parameters = new object[5];
            parameters.SetValue(calcObject.UniqueID, 0);
            parameters.SetValue(calcObject.Value1, 1);
            parameters.SetValue(calcObject.Value2, 2);
            parameters.SetValue(calcObject.Result, 3);
            parameters.SetValue(calcObject.Operation.OperationID, 4);
            int returnVal = _db.ExecuteNonQuery(ResourceHelper.GetSpResourceValue("UPD_CALC"), parameters);
            if (returnVal > 0)
                return true;
            else
                return false;
        }
        catch (Exception exp)
        {
            Utilities.LoggingHelper.WriteLog(exp, MethodBase.GetCurrentMethod(), TraceEventType.Error);
            throw exp;
        }
    }

我正在使用Enterprise Library 5.0从DAL执行数据库操作。

以下代码显示了如何将对象绑定到Web项目中的HTML。

网站

 <asp:GridView ID="GridView1" runat="server" AllowPaging="True" PageSize="3" AutoGenerateColumns="False"
        DataSourceID="CalculationDS" AllowSorting="True" OnRowDeleted="GridView1_RowDeleted"
        OnRowUpdated="GridView1_RowUpdated" OnSorting="GridView1_Sorting" 
        onrowupdating="GridView1_RowUpdating" DataKeyNames="UniqueID">
        <Columns>
            <asp:CommandField ShowEditButton="True" />
            <asp:BoundField DataField="UniqueID" HeaderText="UniqueID" 
                SortExpression="UniqueID" />
            <asp:BoundField DataField="Value1" HeaderText="Value1" 
                SortExpression="Value1" />
            <asp:BoundField DataField="Value2" HeaderText="Value2" 
                SortExpression="Value2" />
            <asp:BoundField DataField="Result" HeaderText="Result" 
                SortExpression="Result" />
            <asp:TemplateField HeaderText="Operation" 
                SortExpression="OperationName">
                <EditItemTemplate>
                    <asp:DropDownList ID="drpOperations" runat="server" DataSourceID="OperationDS" 
                        DataTextField="OperationName" DataValueField="OperationID" 
                        SelectedValue='<%# Eval("Operation.OperationID") %>'>
                    </asp:DropDownList>
                    <asp:ObjectDataSource ID="OperationDS" runat="server" 
                        SelectMethod="GetAllOperations" TypeName="SampleBLL.OperationBLL">
                        <SelectParameters>
                            <asp:Parameter DefaultValue="OperationName" Name="sortExpression" 
                                Type="String" />
                            <asp:Parameter DefaultValue="Ascending" Name="sortDirection" Type="String" />
                        </SelectParameters>
                    </asp:ObjectDataSource>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" 
                        Text='<%# Eval("Operation.OperationName") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="EntryDateTime" HeaderText="EntryDateTime" 
                SortExpression="EntryDateTime" />
        </Columns>
    </asp:GridView>
    <asp:ObjectDataSource ID="CalculationDS" runat="server" DataObjectTypeName="SampleEntities.Calculation"
        DeleteMethod="DeleteCalculation" InsertMethod="InsertCalculation" SelectMethod="GetAllCalculations"
        TypeName="SampleBLL.CalculationBLL" UpdateMethod="UpdateCalculation" OnSelecting="CalculationDS_Selecting"
        OnUpdating="CalculationDS_Updating">
        <SelectParameters>
            <asp:ControlParameter ControlID="GridView1" DefaultValue="UniqueID" Name="sortExpression"
                PropertyName="SortExpression" Type="String" />
            <asp:ControlParameter ControlID="GridView1" Name="sortDirection" PropertyName="SortDirection"
                Type="String" />
        </SelectParameters>
    </asp:ObjectDataSource>

当我尝试编辑行并提交时,当我读取计算对象操作ID为NULL时,CALCULATION对象中的OPERATION列在DAL中为NULL。

我试图从CalculationDs(ObjectDatasource)Updating事件中读取值 和GridView更新事件但没有用。

OPERATION对象列不会自动映射到GridView列。

提前致谢。如果你有这个更新的解决方案工作.. !! :(

0 个答案:

没有答案