2

实体

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 时,计算对象中的操作列在 DAL 中为 NULL。

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

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

提前致谢。如果您有解决此更新的方法.. !!:(

4

0 回答 0