当我想关闭表单时,cmbProspecten_SelectedIndexChanged
会调用该方法。但是 cmbProspecten 中的值是空的,所以我得到了
NullReferenceException:对象引用未设置为对象的实例。
关于我如何解决这个问题的任何想法?我在该行收到错误消息:"if(setInMail(Convert.ToInt32(dr[0]), Convert.ToInt32(cmbProspecten.SelectedValue.ToString())) == true)
private void cmbProspecten_SelectedIndexChanged(object sender, EventArgs e)
{
lstOntvangen.Items.Clear();
OleDbConnection con = new OleDbConnection(connstring);
string query = "SELECT Ontwikkeldossier.OntwikkeldossierID, [E-mailcampagne].CampagneID FROM [E-mailcampagne], Ontwikkeldossier WHERE (([Ontwikkeldossier].[Startdatum]>=[E-mailcampagne].[StartdatumOpl] And [Ontwikkeldossier].[Startdatum]<=[E-mailcampagne].[EinddatumOpl])) ORDER BY [E-mailcampagne].Naam";
OleDbCommand cmd = new OleDbCommand(query, con);
con.Open();
OleDbDataAdapter da = new OleDbDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
DataTable dt = new DataTable();
da.Fill(ds);
dt = ds.Tables[0];
con.Close();
int i = 0;
foreach (DataRow dr in ds.Tables[0].Rows)
{
EmailMarketingDataset._E_mailcampagneRow emailrow = emailMarketingDataSet._E_mailcampagne.FindByCampagneID(Convert.ToInt32(dr[1]));
EmailMarketingDataset.OntwikkeldossierRow ontwikkelrow = emailMarketingDataSet.Ontwikkeldossier.FindByOntwikkeldossierID(Convert.ToInt32(dr[0]));
if (i != 0)
{
if (lstOntvangen.Items[i - 1].ToString() == emailrow.Naam)
{
if(setInMail(Convert.ToInt32(dr[0]),Convert.ToInt32(cmbProspecten.SelectedValue.ToString())) == true)
lstOntvangen.Items.Add(ontwikkelrow.Titel);
}
else
{
if (setInMail(Convert.ToInt32(dr[0]), Convert.ToInt32(cmbProspecten.SelectedValue.ToString())) == true)
{
lstOntvangen.Items.Add(" ");
lstOntvangen.Items.Add(emailrow.Naam);
lstOntvangen.Items.Add(ontwikkelrow.Titel);
}
i = lstOntvangen.Items.Count - 1;
}
}
else
{
if (setInMail(Convert.ToInt32(dr[0]), Convert.ToInt32((cmbProspecten.SelectedValue.ToString())) == true)
{
lstOntvangen.Items.Add(emailrow.Naam);
lstOntvangen.Items.Add(ontwikkelrow.Titel);
i++;
}
}
}
}