异常:212,1不是Decimal的有效值

本文关键字:Decimal 有效值 不是 异常 | 更新日期: 2023-09-27 18:25:39

我在MSSQL 2005 Express表中创建具有PK BudgetID(INT)和BudgetValue(MONEY)的Budgets然后我从数据库创建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

有什么想法吗?

谢谢!

异常:212,1不是Decimal的有效值

好的。

我检查我当前的文化。这是UKUA(乌克兰人-就像上一任妻子Dr.House)

我在web.config 中设置

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

值为123.5,具有点插入井

谢谢大家!