使用EntityDataSource更新自定义绑定格式datetime

时间:2013-11-11 03:41:34

标签: c# asp.net date datetime gridview

也许这是ASP.NET开发人员团队所犯的错误之一。

我已经制作了数据模型 Celengan ,格式( ColumnName:Type ):

Celengan
CelenganID: System.Int32 [PK]
..  // means other field
..  // means other field
TanggalPemberian: System.DateTime
TanggalPengembalian: System.DateTime
..

然后,我通过创建一个GridView和EntityDataSource在GridView上创建了非常简单的CRUD应用程序。 这是我的页面中的entitydatasource设计代码(Celengan.aspx):

<asp:EntityDataSource ID="CelenganEntityDataSource" runat="server" ConnectionString="name=SmatContext" DefaultContainerName="SmatContext" EnableFlattening="False" EntitySetName="Celengans" Include="Perusahaan" EnableDelete="True" EnableUpdate="True"></asp:EntityDataSource>

看,我启用了两个命令:EnableDelete="True"EnableUpdate="True"

这是我的GridView设计代码:

<asp:GridView ID="CelenganGridView" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" CellPadding="5" DataKeyNames="CelenganID" DataSourceID="CelenganEntityDataSource" ShowHeaderWhenEmpty="True">
    <Columns>
        <asp:BoundField DataField="CelenganID" HeaderText="ID" ReadOnly="True" SortExpression="CelenganID" >
        <asp:TemplateField HeaderText="Tanggal Pemberian" SortExpression="TanggalPemberian">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("TanggalPemberian", "{0:dd/MM/yyyy}") %>' Width="80px"></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label3" runat="server" Text='<%# Bind("TanggalPemberian", "{0:dd/MM/yyyy}") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Tanggal Pengembalian" SortExpression="TanggalPengembalian">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("TanggalPengembalian", "{0:dd/MM/yyyy}") %>' Width="80px"></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label4" runat="server" Text='<%# Bind("TanggalPengembalian", "{0:dd/MM/yyyy}") %>'></asp:Label>
            </ItemTemplate>
        <asp:TemplateField ShowHeader="False">
            <EditItemTemplate>
                <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Update" Text="Save" ></asp:LinkButton>
                <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" ></asp:LinkButton>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:LinkButton ID="LinkButton3" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit" ></asp:LinkButton>
                <asp:LinkButton ID="LinkButton4" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete" OnClientClick="return confirm('Apakah celengan ini akan di hapus ?');"></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

由于我的国家(印度尼西亚)使用非标准日期时间格式(dd / MM / yyyy),我用"{0:dd/MM/yyyy}"格式修改了绑定功能。

但是,现在大问题,修改后的绑定功能使CelenganEntityDataSource无法更新,而我尝试点击LinkButton1 CommandName=Update

我尝试更新时出现以下错误消息:

  

设置属性'TanggalPemberian'时出错:'无法将参数'TanggalPemberian'的值转换为'System.DateTime'类型。'。

来源错误:

  

在执行当前Web请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常的起源和位置的信息

堆栈追踪:

[EntityDataSourceValidationException: Error while setting property 'TanggalPemberian': 'Cannot convert the value of parameter 'TanggalPemberian' to the type 'System.DateTime'.'.]
   System.Web.UI.WebControls.EntityDataSourceView.ConvertProperties(IDictionary values, PropertyDescriptorCollection propertyDescriptors, ParameterCollection referenceParameters, Dictionary`2 convertedValues) +74592
   System.Web.UI.WebControls.EntityDataSourceView.ExecuteUpdate(IDictionary keys, IDictionary values, IDictionary oldValues) +355
   System.Web.UI.DataSourceView.Update(IDictionary keys, IDictionary values, IDictionary oldValues, DataSourceViewOperationCallback callback) +87
   System.Web.UI.WebControls.GridView.HandleUpdate(GridViewRow row, Int32 rowIndex, Boolean causesValidation) +1210
   System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +738
   System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source, EventArgs e) +89
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
   System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e) +88
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
   System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +121
   System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +156
   System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +9642610
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724

0 个答案:

没有答案