I am learning C# and keep getting an error “Cannot implicitly convert type ‘int’ to ‘short'”.
In Microsoft SQL Server where my database is, U_Weeks_In_Month is defined as smallint. What do I need to do to solve this error?
I tried to use short.Parse just like in the year textfields but this does not work for the item, i.e
U_Weeks_In_Month = short.Parse(item.WeekNumber),
Any solution?
My code is :
var weekInfos = SAPUtility.GetWeekInfo(dateTimePicker1.Value, 52);
// Create object
foreach (var item in weekInfos)
{
// Get new code
var newWeeklyCode = weeklyPeriodService.GenerateSAPCode();
var weeklyPeriodAdd =
new WeeklyPeriod
{
Code = newWeeklyCode,
Name = newWeeklyCode,
U_Tax_Year = short.Parse(txt_tax_year.Text),
U_Month = item.Month.ToString(),
U_Pay_Process_Status = "N",
U_Payroll_Year = short.Parse(txt_tax_year.Text),
U_Weeks_In_Month = item.WeekNumber,
U_Starting_date = item.FirstDayOfWeek,
U_Ending_date = item.LastDayOfWeek,
};
// Save record
weeklyPeriodService.AddWeeklyPeriod(weeklyPeriodAdd);
}
I suspect you just need:
An alternative is to change your
WeekNumberproperty toshortto start with.Additionally, I’d pull the parsing of
txt_tax_year.Textoutside the loop: