I'm having an odd problem with a datagrid in a c# program. The program
maintains a schedule of jobs that need to be run. As jobs run, the status
and next run date/time info are updated in a SQL database. On the initial
load, the datagrid fields are defined. The 1st time the dataset is reloaded,
an error is returned stating that the 1st field in the list is not found. On
all subsequent reloads, everything is fine. Here's the relevant code (I
chopped out as much as I thought I could and still illustrate the problem
areas). It seems like the problem is being caused by using the Process
object. If I comment out the portion of the routine that sets up and runs
the jobs, the grid always works. Any help solving this would be appreciated.
private void RunThisJob()
{
try
{
DataSet statusSet = midConn.GetData("JobMaster", "select * from
JobMaster where id = " + jobRow["id"].ToString());
statusSet.WriteXml(jobRow["xmlName"].ToString(),
XmlWriteMode.WriteSchema);
lastJobName = jobRow["jobTitle"].ToString();
theStatus.Text = DateTime.Now.ToShortDateString() + " " +
DateTime.Now.ToShortTimeString() +
" running " + lastJobName;
Process myProcess = new Process();
myProcess.StartInfo.FileName = jobRow["programPath"].ToString();
myProcess.StartInfo.Arguments = jobRow["configName"].ToString() + " " +
jobRow["xmlName"].ToString();
myProcess.Start();
myProcess.WaitForExit();
resultSet = new DataSet();
resultSet.ReadXml(jobRow["xmlName"].ToString(), XmlReadMode.ReadSchema);
resultRow = resultSet.Tables[0].Rows[0];
DoStatus((bool)resultRow["lastStatus"],
resultRow["lastMessage"].ToString());
}
catch(Exception ex)
{
SendConfirmation("Exception executing job " + lastJobName + ": " +
ex.Message, false);
theStatus.Text = "Exception executing job " + lastJobName + ": " +
ex.Message;
resultRow = null;
DoStatus(false, ex.Message);
}
finally
{
jobSet = midConn.GetData("JobMaster", "select * from JobMaster where id
= " + jobRow["id"].ToString());
jobRow = jobSet.Tables[0].Rows[0];
jobRow["lastRun"] = DateTime.Now;
ScheduleJob();
}
}
private void LoadSchedule(DateTime nextDate)
{
try
{
jobRow["nextRun"] = nextDate;
jobRow["retryCount"] = 0;
DataSet rd = midConn.PutData(jobSet.GetChanges());
jobSet = midConn.GetData("JobMaster", "select * from JobMaster order by
nextRun, jobTitle");
ShowJobs();
jobRow = null;
}
catch (Exception ex)
{
SendConfirmation("Exception writing next scheduled date for " +
lastJobName + ": " + ex.Message, false);
}
}
private void ShowJobs()
{
try
{
if (jobGrid.TableStyles.Count == 0)
{
jobStyle = new DataGridTableStyle();
jobStyle.MappingName = "JobMaster";
jobGrid.TableStyles.Add(jobStyle);
DataColumn jobID = (DataColumn)jobSet.Tables[0].Columns["id"];
jobSet.Tables[0].PrimaryKey = new DataColumn[1]{jobID};
jobGrid.SetDataBinding(jobSet, jobSet.Tables[0].TableName);
jobStyle.GridColumnStyles.Clear();
DataGridTextBoxColumn cs = new DataGridTextBoxColumn();
cs.MappingName = "jobTitle";
cs.HeaderText = "Job Name";
cs.Alignment = HorizontalAlignment.Left;
cs.Width = 350;
jobStyle.GridColumnStyles.Add(cs);
DataGridTextBoxColumn cs2 = new DataGridTextBoxColumn();
cs2.MappingName = "lastRun";
cs2.HeaderText = "Last Run";
cs2.Alignment = HorizontalAlignment.Left;
cs2.Width = 125;
cs2.Format = "MM/dd/yy hh:mm tt";
jobStyle.GridColumnStyles.Add(cs2);
DataGridTextBoxColumn cs3 = new DataGridTextBoxColumn();
cs3.MappingName = "nextRun";
cs3.HeaderText = "Next Run";
cs3.Alignment = HorizontalAlignment.Left;
cs3.Width = 125;
cs3.Format = "MM/dd/yy hh:mm tt";
jobStyle.GridColumnStyles.Add(cs3);
DataGridBoolColumn bs = new DataGridBoolColumn();
bs.MappingName = "active";
bs.HeaderText = "Active";
bs.Alignment = HorizontalAlignment.Left;
bs.Width = 50;
jobStyle.GridColumnStyles.Add(bs);
}
else
jobGrid.SetDataBinding(jobSet, jobSet.Tables[0].TableName);
}
catch (Exception ex)
{
theStatus.Text = "Error in ShowJobs - " + ex.Message;
}
}