0

for example inserting (0.1234) by the manual query is fine but while using the mvc5 form to insert the value from the form.

1- configurations of column in database is :

    RateBuy (decimal(18, 4))

2- the model is setup like , while inserting the data from the from

    [DisplayFormat(DataFormatString = "{0:n4}", ApplyFormatInEditMode = true)]
    public decimal RateBuy { get; set; }

result is 0.1200 in the database

3- the model is setup like, while inserting the data from the from

       [DisplayFormat(DataFormatString = "{0:0.0000}", ApplyFormatInEditMode = true)]

result is 0.1200 in the database

Code to save data :

 public ActionResult SaveNew(Rate tbl)
        {
            Rate t = new Rate ();
            t.RateBuy = tbl.RateBuy;
            dbConnection.Entry(t).State = System.Data.Entity.EntityState.Added;
            dbConnection.SaveChanges();
            return View();

        }

any solution a big head ache...

SAR
  • 1,765
  • 3
  • 18
  • 42
  • The code you have shown is not concerned with databases. – GSerg Mar 29 '21 at 07:38
  • there is one update check please, but the database must support decimal too – SAR Mar 29 '21 at 07:39
  • Still no database related code. – GSerg Mar 29 '21 at 07:40
  • here we required to have the decimal(18, 4) in database, in order to support 4 points – SAR Mar 29 '21 at 07:41
  • You have shown no code where the value is saved in a database. – GSerg Mar 29 '21 at 07:41
  • Have you verified what `tbl.RateBuy` contains in `SaveNew`? – GSerg Mar 29 '21 at 08:12
  • it's saving the data, in the database but the issue is not allowing 4 decimal place for example i insert 0.1234 and inside the database it gets 0.1200 and yes i check it's 0.1234 – SAR Mar 29 '21 at 08:13
  • If `tbl.RateBuy` is indeed 0.1234 in `SaveNew` when it executes, then check how your database model is set up and which data properties are assigned for that field. – GSerg Mar 29 '21 at 08:16
  • yes, this is strange for me while debug i check the value is correct only after insert to database it's change to that. may be it's because of web.config or some other issue on the entityframework – SAR Mar 29 '21 at 08:18
  • 1
    So check what properties you have specified in EF for that column (which is different to the properties you have specified in the database). They should match. – GSerg Mar 29 '21 at 09:46

1 Answers1

0

For this i have changed the decimal to float and use this in the model

        [RegularExpression(@"^\d+.\d{0,4}$", ErrorMessage = "Rate can't have more than 4 decimal places")]
        public float RateBuy { get; set; }
SAR
  • 1,765
  • 3
  • 18
  • 42
  • Using `float` for money is going to [get you in trouble](https://stackoverflow.com/q/3730019/11683). Fix your `decimal` definition instead in the EF properties. – GSerg Mar 29 '21 at 10:54