Unable to Add/Update using 3-Tier Architecture

432 views Asked by At

I have a table called Currency with two properties to be inserted which are Unit and Rate.

When I press add or edit, only Unit is saved but Rate remain 0.

When I press delete, the record is deleted successfully.

Below is the code from Data Layer

public interface ICurrencyRepository
{
    List<Currency> GetAll();

    Currency GetById(int id);

    Currency Insert(Currency obj);

    void Update(Currency obj);

    void Delete(Currency obj);
}

public class CurrencyRepository : ICurrencyRepository
{
    public void Delete(Currency obj)
    {
        using (ApplicationDbContext db = new ApplicationDbContext())
        {
            db.Currencies.Attach(obj);
            db.Currencies.Remove(obj);
            db.SaveChanges();
        }
    }

    public List<Currency> GetAll()
    {
        using (ApplicationDbContext db = new ApplicationDbContext())
        {
            return db.Currencies.ToList();
        }
    }

    public Currency GetById(int id)
    {
        using (ApplicationDbContext db = new ApplicationDbContext())
        {
            return db.Currencies.Find(id);
        }
    }

    public Currency Insert(Currency obj)
    {
        using (ApplicationDbContext db = new ApplicationDbContext())
        {
            db.Currencies.Add(obj);
            db.SaveChanges();
            return obj;
        }
    }

    public void Update(Currency obj)
    {
        using (ApplicationDbContext db = new ApplicationDbContext())
        {
            db.Currencies.Attach(obj);
            db.Entry(obj).State = System.Data.Entity.EntityState.Modified;
            db.SaveChanges();
        }
    }
}

Below is the code from Business Layer

public static class CurrencyServices
{
    static ICurrencyRepository repository;

    static CurrencyServices()
    {
        repository = new CurrencyRepository();
    }

    public static List<Currency> GetAll()
    {
        return repository.GetAll();
    }

    public static Currency GetById(int id)
    {
        return repository.GetById(id);
    }

    public static Currency Insert(Currency obj)
    {
        return repository.Insert(obj);
    }

    public static void Update(Currency obj)
    {
        repository.Update(obj);
    }

    public static void Delete(Currency obj)
    {
        repository.Delete(obj);
    }
}

Below is the code from my UI (Page with grid)

    private void btnNew_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
    {
        DocumentController.ActivateForm(typeof(Test), null);
        currencyBindingSource.DataSource = CurrencyServices.GetAll();
    }

    private void btnEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
    {
        if (currencyBindingSource.Current == null)
        {
            return;
        }

        else
        {
            DocumentController.ActivateForm(typeof(Test), currencyBindingSource.Current as Currency);
            currencyBindingSource.DataSource = CurrencyServices.GetAll();
        }
    }

Below is the code from my UI (Edit Page)

    bool isNew;
    public CurrencyEdit(Currency obj)
    {
        InitializeComponent();
        if (obj == null)
        {
            currencyBindingSource.DataSource = new Currency();
            isNew = true;
        }

        else
        {
            currencyBindingSource.DataSource = obj;
            isNew = false;
        }
    }

    private void btnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
    {
        if (isNew)
        {
            CurrencyServices.Insert(currencyBindingSource.Current as Currency);
        }

        else
        {
            CurrencyServices.Update(currencyBindingSource.Current as Currency);
        }
    }

Below is how I currencyBindingSource is getting created and bound in UI code.

  1. Add bindingSource from toolbox.
  2. Go to properties -> DataSource -> Add Project Data Source -> Object -> Choose currency table.
  3. Add two textboxes -> Properties -> DataBindings -> EditValue -> Choose Unit and Rate from currencyBindingSource.
1

There are 1 answers

2
RBT On BEST ANSWER

Here is what you need to do. I believe you are missing some appropriate casts which are required:

private void btnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
    EfTestDataSet.CurrencyRow currencyRow = (EfTestDataSet.CurrencyRow)  ( currencyBindingSource.Current as DataRowView).Row;

    if (isNew)
    {
        CurrencyServices.Insert(new Currency { Unit = currencyRow.Unit, Rate = currencyRow.Rate } );
    }
    else
    {
        CurrencyServices.Update(new Currency { Unit = currencyRow.Unit, Rate = currencyRow.Rate } );
    }
}

Note: EfTestDataSet is the name of the data source data set which got created while adding binding source in my application. It might be different in your application.

Hope this helps!