例外:212,1不是Decimal的有效值

时间:2012-02-24 21:40:47

标签: c# entity-framework-4 repository decimal

我使用PK BudgetID(INT)和BudgetValue(MONEY)在MSSQL 2005 Express表中创建预算 然后我从DataBase创建Entitty模型

字段BudgetValue已映射为Decimal

然后我像这样创建存储库类

public class BudgetRepository : IDisposable
{
    private moneytestEntities context = new moneytestEntities();

    public IEnumerable<Budgets> GetBudgets()
    {
        return context.BudgetsSet.ToList();
    }

    public void InsertBudgets(Budgets budgets)
    {
        try
        {
            context.BudgetsSet.AddObject(budgets);
            context.SaveChanges();
        }
        catch (Exception err)
        {
            throw err;
        }
    }

    public void DeleteBudgets(Budgets budgets)
    {
        try
        {
            context.BudgetsSet.Attach(budgets);
            context.BudgetsSet.DeleteObject(budgets);
            context.SaveChanges();
        }
        catch (Exception err)
        {
            throw err;
        }
    }

    private bool disposedValue = false;

    protected virtual void Dispose(bool disposing)
    {
        if (!this.disposedValue)
        {
            if (disposing)
            {
                context.Dispose();
            }
        }
        this.disposedValue = true;
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }
}

并像这样创建WebForm1

<form id="form1" runat="server">
    <div>
        <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
            SelectMethod="GetBudgets" TypeName="WebApplication13.DAL.BudgetRepository" 
            DataObjectTypeName="WebApplication13.DAL.Budgets" 
            InsertMethod="InsertBudgets" DeleteMethod="DeleteBudgets" 
            oninserting="ObjectDataSource1_Inserting">
        </asp:ObjectDataSource>
        <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" 
            DataSourceID="ObjectDataSource1" Height="50px" 
            oniteminserted="DetailsView1_ItemInserted" Width="125px" DefaultMode="Insert">
            <Fields>
                <asp:CommandField ShowInsertButton="True" />
                <asp:DynamicField DataField="BudgetValue" HeaderText="BudgetValue" />
            </Fields>
        </asp:DetailsView>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataSourceID="ObjectDataSource1" DataKeyNames="BudgetID">
            <Columns>
                <asp:CommandField ShowDeleteButton="True" />
                <asp:DynamicField DataField="BudgetValue" HeaderText="BudgetValue" />
            </Columns>
        </asp:GridView>
        <br />
        <asp:Label ID="ExceptionLabel" runat="server" Text="" ForeColor="Red"></asp:Label>
    </div>
</form>

和键入控件

protected void Page_Init(object sender, EventArgs e)
{
    DetailsView1.EnableDynamicData(typeof(Budgets));
    GridView1.EnableDynamicData(typeof(Budgets));
}

当我插入像123这样的值时一切正常 但我插入值如123,45我有异常

堆栈追踪:

[FormatException: Input string was not in a correct format.]
   System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) +9591983
   System.Number.ParseDecimal(String value, NumberStyles options, NumberFormatInfo numfmt) +146
   System.ComponentModel.DecimalConverter.FromString(String value, NumberFormatInfo formatInfo) +53
   System.ComponentModel.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value) +302

[Exception: 212,1 is not a valid value for Decimal.]
   System.ComponentModel.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value) +489
   System.Web.UI.WebControls.ObjectDataSourceView.ConvertType(Object value, Type type, String paramName) +117
   System.Web.UI.WebControls.ObjectDataSourceView.BuildObjectValue(Object value, Type destinationType, String paramName) +167
   System.Web.UI.WebControls.ObjectDataSourceView.BuildDataObject(Type dataObjectType, IDictionary inputParameters) +229
   System.Web.UI.WebControls.ObjectDataSourceView.ExecuteInsert(IDictionary values) +290
   System.Web.UI.DataSourceView.Insert(IDictionary values, DataSourceViewOperationCallback callback) +89
   System.Web.UI.WebControls.DetailsView.HandleInsert(String commandArg, Boolean causesValidation) +379
   System.Web.UI.WebControls.DetailsView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +611
   System.Web.UI.WebControls.DetailsView.OnBubbleEvent(Object source, EventArgs e) +95
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
   System.Web.UI.WebControls.DetailsViewRow.OnBubbleEvent(Object source, EventArgs e) +112
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
   System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +125
   System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +169
   System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +9
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +176
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563

有什么想法吗?

感谢!

1 个答案:

答案 0 :(得分:1)

确定。

我检查我目前的文化。它是uk-UA(乌克兰语 - 就像最后的妻子Dr.House)

我在web.config中设置

<system.web>
    <globalization uiCulture="en-US" culture="en-US"/>

和像插入点好的123.5一样的值

谢谢大家!