mdarling
mdarling

Reputation: 77

Trying to enter information into database column based off of form entry

I am not even sure how to word this question so bear with me please. I will edit if needed.

Lets start with my code:

var assetMonth = "assest."+Month;
var billableAssests = (from s in db.Assets
                              where s.SystemPosition == 1
                              select s).ToList();
        try {

            foreach (var assests in billableAssests)
            {
                    assests.PreviousDateBilled = assests.LastDateBilled;
                    assests.LastDateBilled = today;
                    assetMonth = assests.MonthlyChargeRate; <===HERE
                    assests.RunnungLeaseTotal = assests.RunnungLeaseTotal + assests.MonthlyChargeRate;
                    if (assests.MonthBilled == null)
                    {
                        assests.MonthBilled =  1;
                    }
                    else
                    {
                        assests.MonthBilled = assests.MonthBilled + 1;
                    }

                    //db.Entry(billableAssests).State = EntityState.Modified;
                    db.SaveChanges();

                }
         }

I am getting the month from the user that they want to bill and want to insert the monthly charge rate into that months column. I cannot figure out how to do this for the life of me. Any suggestions or tell me where i can start looking?

Thanks!

EDIT*** Model for table

public class Asset
{
    [Key]
    public string AssetTag { get; set; }

    public string SerialNumber { get; set; }
    public int WarrantyPeriod { get; set; }
    public string DeviceApprover { get; set; }
    public int Dept_id { get; set; }
    public string AssetLocation { get; set; }
    public string DeliveryLocation { get; set; }
    public int SectionKey { get; set; }
    public int VendorKey { get; set; }
    public int DeviceInformationKey { get; set; }
    public int EmployeeKey { get; set; }
    public decimal PurchasePrice { get; set; }
    public decimal? BaseLeaseRate { get; set; }
    public decimal? MonthlyChargeRate { get; set; }
    public decimal? LeaseTotal { get; set; }
    public int? MonthBilled { get; set; }
    public DateTime? LeaseStartDate { get; set; }
    public DateTime? LeaseEndDate { get; set; }
    public decimal? RunnungLeaseTotal { get; set; }
    public int deliveryEmployeeKey { get; set; }
    public DateTime? InstallDate { get; set; }
    public Boolean? Confirm { get; set; }
    public string Status { get; set; }
    public int? SystemPosition { get; set; }
    public DateTime? LastDateBilled { get; set; }
    public DateTime? PreviousDateBilled { get; set; }
    public DateTime? ReturnedDate { get; set; }
    public int Account { get; set; }
    public decimal? Jan { get; set; }
    public decimal? Feb { get; set; }
    public decimal? Mar { get; set; }
    public decimal? Apr { get; set; }
    public decimal? May { get; set; }
    public decimal? June { get; set; }
    public decimal? July { get; set; }
    public decimal? Aug { get; set; }
    public decimal? Sept { get; set; }
    public decimal? Oct { get; set; }
    public decimal? Nov { get; set; }
    public decimal? Dec { get; set; }


    public virtual Section Section { get; set; }
    public virtual Vendor Vendor { get; set; }
    public virtual DeviceInformation DeviceInformation { get; set; }
    public virtual Employee Employee { get; set; }

Upvotes: 0

Views: 76

Answers (1)

Ann L.
Ann L.

Reputation: 13975

Well, you have a couple of options.

The first is to refactor your model and possibly your database, so that rather than having 12 fields for 12 months, you have a collection of month values. That would be easier to update, since you could just use the passed-in month to figure out which entry you want, if it exists, or add it if it doesn't.

 public class AssetMonth
 {
     string AssetTag { get;set;}
     DateTime Month { get;set;} // DateTime would *probably* be the best choice for this
     Decimal? MonthlyChargeRate { get;set;}
 }

The second is to make use of a good old-fashioned switch statement:

 switch(Month) // I don't know what type this is; I'm assuming string.
 {
    case "Jan":
         model.Jan = assets.MonthlyChargeRate;
         break;
    case "Feb":
         model.Feb = assets.MonthlyChargeRate;
    // etc.
}

Third, if you're really feelin' fancy, you could use reflection to set the value.

 PropertyInfo prop = typeof(Asset).GetProperty(Month); // again, assuming string
 prop.SetValue(model, assets.MonthlyChargeRage);

But I would encourage you to refactor the model. That will give you the best design, and remove the need for workarounds like options 2 and 3.

Upvotes: 2

Related Questions