【发布时间】:2011-07-05 06:41:07
【问题描述】:
我正在学习 C#,但一直收到错误消息“无法将类型 'int' 隐式转换为 'short'”。
在我的数据库所在的 Microsoft SQL Server 中,U_Weeks_In_Month 被定义为 smallint。我需要做什么来解决这个错误?
我尝试像在年份文本字段中一样使用 short.Parse,但这不适用于该项目,即
U_Weeks_In_Month = short.Parse(item.WeekNumber),
有什么办法吗?
我的代码是:
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);
}
【问题讨论】:
-
你能提供你班级的代码吗?你的字段是什么类型的?
标签: c# visual-studio-2010