2012-02-24 37 views
0

有效值我在PK BudgetID(INT)和BUDGETVALUE(MONEY)MSSQL 2005 Express的預算表創建 然後我從數據庫例外:212,1不是小數

創建Entitty型號

和現場BUDGETVALUE是映射爲十進制

然後創建repository類這樣

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中0

和類型化控制

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 

任何想法?

謝謝!

+0

您是否設定了文化?默認文化需要使用'.'而不是'''。 – 2012-02-25 00:51:21

+0

因爲不變文化的十進制分隔符_(即ObjectDataSourceView.ConvertType中'BaseNumberConverter.ConvertFrom')的參數_不是''',而是'''。 '123.45'將是一個有效的輸入。雖然我不熟悉_ASP.NET_,但我無法提供解決方案。 – ordag 2012-02-25 01:01:54

+0

請參閱以下鏈接可能會幫助你[鏈接] http://stackoverflow.com/questions/831727/c-sharp-decimal-parse-issue-with-commas [link] http://stackoverflow.com/questions/1354924/C-怎麼辦-I-解析 - 一個串上帶有一個十進制點到一個雙 – VIJAY 2012-02-25 01:02:31

回答

0

好的。

我檢查我目前的文化。這是英國UA(烏克蘭 - 像去年的妻子Dr.House)

我在web.config中

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

,並像123.5點與設定值以及插入

謝謝大家!