How can I save the info that the ViewModel/Model
passes to the View
? I need this info saved to a database but just to ensure the values are being retrieved, I've set up a MessageBox
to show if the value is being retrieved.
This is the code for my ViewModel/Model:
namespace OcelotPayroll
{
public class PayslipModel : EmployeeModel
{
private decimal statIncome;
private string totalDed;
private string _netpay;
private string _amount;
public string Amount
{
get
{
return _amount;
}
set
{
_amount = value;
OnPropertyChanged("Amount");
}
}
public decimal StautoryIncome
{
get
{
return statIncome;
}
set
{
statIncome = value;
}
}
public PayslipModel()
{
Date = DateTime.Today.Date;
SelectAll = new DelegateCommand(Select, () => CanSelect);
UnSelectAll = new DelegateCommand(UnSelect, () => CanUnSelect);
SaveToDatabase = new DelegateCommand(Save, () => CanSave);
scon = new MichaelAllenEntities();
}
public ICommand SaveToDatabase
{
get; set;
}
private bool CanSave
{
get { return Workspaces.Count > 0; }
}
private async void Save()
{
try
{
MessageBox.Show(StautoryIncome.ToString()); //returns 0
MessageBox.Show(Amount.ToString()); //NullPointerException
}
catch (DbEntityValidationException ex)
{
foreach (var en in ex.EntityValidationErrors)
{
var exceptionDialog = new MessageDialog
{
Message = { Text = string.Format("{0}, {1}", en.Entry.Entity.GetType().Name, en.Entry.State) }
};
await DialogHost.Show(exceptionDialog, "RootDialog");
foreach (var ve in en.ValidationErrors)
{
exceptionDialog = new MessageDialog
{
Message = { Text = string.Format("{0}, {1}", ve.PropertyName, ve.ErrorMessage) }
};
await DialogHost.Show(exceptionDialog, "RootDialog");
}
}
}
catch(Exception ex)
{
var exceptionDialog = new MessageDialog
{
Message = { Text = string.Format("{0}", ex) }
};
await DialogHost.Show(exceptionDialog, "RootDialog");
}
}
public async void CalcNis()
{
try
{
float nis = 0;
NisName = "N.I.S.";
SqlConnection con = new SqlConnection(scon.Database.Connection.ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("select NIS from Deductions where TaxId='1'", con);
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
nis = float.Parse(sdr.GetValue(0).ToString());
}
con.Close();
if (Amount != string.Empty)
{
NisVal = (decimal.Parse(Amount.ToString()) * decimal.Parse(nis.ToString())).ToString("N2");
StautoryIncome = (decimal.Parse(Amount.ToString()) - decimal.Parse(NisVal.ToString())); //value gets assigned here
}
}
catch(Exception ex)
{
var exceptionDialog = new MessageDialog
{
Message = { Text = ex.ToString() }
};
await DialogHost.Show(exceptionDialog, "RootDialog");
}
}
}
}
Databinding for Amount
:
<TextBlock Foreground="Black" Margin="0 0 0 0" VerticalAlignment="Top" Grid.Column="3" Text="{Binding Path=Amount, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
All the calculations that you see, work correctly and all values pass over to the View
perfectly. However when I try saving, I get an Object Reference not set to instance of an object
Exception
.
How can I retrieve the values from the view for saving to database?
Edit: Ok so I've updated the code to make it more understandable. I receive the error in this line MessageBox.Show(StautoryIncome.ToString());
. It holds the value after it's calculated in the CalcNis()
method and performs other calculations with that same value. However, as soon as I try saving, it's like StautoryIncome
returns 0 and any of the databindings returns the NullPointerException
. Binding for Amount
works perfectly, as soon as I start entering text in a Textbox
, the TextBlock
updates with same value.