1

I create in MSSQL 2005 Express table Budgets with PK BudgetID (INT) and BudgetValue (MONEY) Then i create Entitty Model from DataBase

And field BudgetValue was mapped as Decimal

Then i create repository class like this

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);
    }
}

and create WebForm1 like this

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

and typed control

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

When i insert value like 123 all ok But i when insert value like 123,45 i have exception

Stack Trace:

[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

Any ideas?

Thank!

Maciej
  • 7,871
  • 1
  • 31
  • 36
  • Have you set the culture? The default culture requires `.` instead of `,`. – John Pick Feb 25 '12 at 00:51
  • Because the decimal seperator for invariant cultures _(that is the parameter used for `BaseNumberConverter.ConvertFrom` in `ObjectDataSourceView.ConvertType`)_ is not `,` but `.`. `123.45` would be a valid input. I can not provide a solution, though, as I'm not familiar with _ASP.NET_. – ordag Feb 25 '12 at 01:01
  • Refer the following links might it will helps you [link] http://stackoverflow.com/questions/831727/c-sharp-decimal-parse-issue-with-commas [link]http://stackoverflow.com/questions/1354924/c-how-do-i-parse-a-string-with-a-decimal-point-to-a-double – VIJAY Feb 25 '12 at 01:02

1 Answers1

2

Ok.

I check my current culture. It was uk-UA (ukrainian - like last wife Dr.House)

I set in web.config

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

and value like 123.5 with point inserted well

Thank all!