在调试代码时,我收到错误#NULL is not a valid value for Int32
。
private void satelliteComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
Helper.SetWaitCursor();
if (satelliteComboBox.SelectedValue != null)
{
var satelliteId = Convert.ToInt32(satelliteComboBox.SelectedValue);
satelliteStatusUserControl.DataSource =
_satelliteStatusBusinessService.GetSingleSatellite(
new Dictionary<string, object> { { "SatelliteID", satelliteId } }, true);
//2012.07.07 get colors for all machine status
satelliteStatusUserControl.DataSource.DefectColors =
_satelliteStatusBusinessService.GetDefectColors().ToList();
foreach (var defectColor in
satelliteStatusUserControl.DataSource.DefectColors)
{
MachineStatusCtrl.AddMachineStatusColors(
defectColor.DefectTypeID,
defectColor.DefectType,
defectColor.OEEColor);
}
//2012.07.07
satelliteStatusUserControl.DataBind();
}
Helper.SetDefaultCursor();
}
我在foreach
循环
更新:以下是AddMachineStatusColors
public static void AddMachineStatusColors(int statusColorId, string StatusName, string oeeColor)
{
MacStatusColors macStatusColor;
//add dummy colors with unknow till the next defectId so that it will be easy to get color later while painting.
for(int Index = StatusColors.Count; Index < statusColorId ; Index++)
{
macStatusColor = new MacStatusColors();
StatusColors.Add(macStatusColor);
}
macStatusColor = new MacStatusColors();
macStatusColor.DefectTypeID = statusColorId;
macStatusColor.DefectType = StatusName;
macStatusColor.OEEColor1 = ControlPaint.Dark(getColorFromString(oeeColor));
macStatusColor.OEEColor2 = ControlPaint.Light(getColorFromString(oeeColor));
StatusColors.Add(macStatusColor);
}
public static Color getColorFromString(string oeeColor)
{
if (oeeColor[0] != '#') { oeeColor = '#' + oeeColor; }
return System.Drawing.ColorTranslator.FromHtml(oeeColor);
}
答案 0 :(得分:2)
好吧,defectColor.OEEColor
似乎是null
,您的数据模型不允许该值为null
。三种可能的解决方案:
null
defectColor.OEEColor
不是null
将行更改为:
MachineStatusCtrl.AddMachineStatusColors(..., ..., defectColor.OEEColor ?? <default value>);
<default value>
为0或任何其他int
值,您希望用它来表示“颜色为null
”。
您添加了AddMachineStatusColors
方法的代码。谢谢,但我仍然说您的代码假定defectColor.OEEColor
不是null
。您的代码中至少有两行我认为defectColor.OEEColor
可能不是null
:
macStatusColor.OEEColor1 = ControlPaint.Dark(getColorFromString(oeeColor));
macStatusColor.OEEColor2 = ControlPaint.Light(getColorFromString(oeeColor));
请显示getColorFromString
的代码,或者告诉我们AddMachineStatusColors
方法错误发生的确切位置!
defectColor.OEEColor
包含字符串值“null”!!这意味着,defectColor.OEEColor
本身不是null
,但包含单词“null”。
因此,以下行将单词“null”变为“#null”(因此关于“#null不是有效值...”的奇怪问题标题):
if (oeeColor[0] != '#') { oeeColor = '#' + oeeColor; }
之后oeeColor
的值为“#null”,然后是以下行(我想,因为你没有给我们任何关于异常真正发生的行的详细信息......)会抛出一个错误:
return System.Drawing.ColorTranslator.FromHtml(oeeColor);
您必须确保defectColor.OEEColor
既不是null
(即“没有值”),并且它的值是有效的HTML颜色字符串!
答案 1 :(得分:0)
如果您收到的整数可能为空,则可能是Nullable<Integer>
,这意味着它将具有.HasValue
属性...
foreach (var defectColor in satelliteStatusUserControl.DataSource.DefectColors)
{
if(defectColor.OEEColor.HasValue) {
MachineStatusCtrl.AddMachineStatusColors(defectColor.DefectTypeID, defectColor.DefectType, defectColor.OEEColor);
} else {
//Use a default
MachineStatusCtrl.AddMachineStatusColors(defectColor.DefectTypeID, defectColor.DefectType, 0);
}
}
答案 2 :(得分:-1)
int? satelliteId;
int i;
if (satelliteComboBox.SelectedValue != null && int.tryParse(satelliteComboBox.SelectedValue, out i))
{
satellideId = i;
}