【发布时间】:2013-05-02 17:19:25
【问题描述】:
在调试代码时,我收到错误#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);
}
【问题讨论】:
-
请先调试,然后告诉我们错误发生在哪一行。另外,
satelliteComboBox.SelectedValue的值是多少。 -
你在哪一行得到错误?
-
您在哪一行代码上遇到此错误?
-
当我在 sql 中将颜色设置为 null 时,每个循环 defectColor.OEEColor 都会出现此错误
-
您是从数据库查询中绑定列表吗?查询是否返回
DBNull.Value,即NULL作为结果之一?