VS 2008 C# Window Issue

rwheeler23 used Ask the Experts™
I have several VS 2008 C# applications that I have developed that interface with an accounting application. They all work fine with one exception. What I have noticed is if any of these programs are called and the resulting data set is empty, all subesequent calls to any other programs are disabled. The only solution is to leave the accounting applicaiton and come back in. When I say disabled, what I mean is what you execute the keystroke to call them they return no data even though there is qualifying data to be returned. It seems like somehow my windows do not close properly when the dataset is null. How do I insure that the windows do close properly even if the dataset is null? Do I need to add some code to an event someplace?
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
I would suggest that you have an unhandled error. The null data set is causing indeterminate behaviour.
You should wrap the call that tries to create and fill the dataset within a try-catch block and then add code in the catch section to either close the program down cleany, or clean up and return to a starting point that the user can perform actions.


I was going through the code last night finding where the data sets get loaded. I check for an empty dataset and I have added a message box alerting the user that no data was found for the criteria for which they were searching and right after that I dispose of the form. Hopefully this will correct the issue.
Is it possible to paste your code?
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.



Here it is. Just look for the Rows.Count string.
private void DisplayJobLinker()
                /* Define dataset for Transaction Types */
                TrxTypeDataSet = new System.Data.DataSet();
                TrxTypeDataSet.CaseSensitive = false;

                TrxTypeDataCommand = new System.Data.SqlClient.SqlCommand();
                TrxTypeDataCommand.Connection = DataConnection;

                TrxTypeDataCommand.CommandText = "select LTRIM(DISTTRXTYPE) AS DISTTRXTYPE from JOB_TRX_TYPES where formtype='COST' ORDER BY DISTTRXTYPE";

                TrxTypeDataAdapter = new System.Data.SqlClient.SqlDataAdapter();
                TrxTypeDataAdapter.SelectCommand = TrxTypeDataCommand;

                /* Define dataset for GL distribution amounts */
                DataDataSet = new System.Data.DataSet();
                DataDataSet.CaseSensitive = false;

                DataCommand = new System.Data.SqlClient.SqlCommand();
                DataCommand.Connection = DataConnection;

                    "from JOB_LINKER WHERE COMPANYID = '" + CompanyID + "' AND TRANSNMBR ='" + VoucherNumber + "' AND MASTERTYPE = '" + MasterType + "' ORDER BY DSTSQNUM";

                DataDataAdapter = new System.Data.SqlClient.SqlDataAdapter();
                DataDataAdapter.SelectCommand = DataCommand;
                _commandBuilder = new SqlCommandBuilder(DataDataAdapter);

                if (DataDataSet.Tables[0].Rows.Count == 0)
                    MessageBox.Show("No distributions found for this transaction in Job Linker");
                    /* Dispose of the connection */
                    GPConnObj = null;

                    /* Shutdown the connection */
                    resp = GPConnection.Shutdown();
                dgvViewJobLinker.DataError += new DataGridViewDataErrorEventHandler(dgvJobLinker_DataError);

                dgvViewJobLinker.RowHeadersVisible = false;
                dgvViewJobLinker.DataSource = DataDataSet.Tables[0].DefaultView;
                dgvViewJobLinker.AutoGenerateColumns = true;
                dgvViewJobLinker.DefaultCellStyle.NullValue = ' ';
                dgvViewJobLinker.AllowUserToAddRows = false;

                DataGridViewButtonColumn colButton = new DataGridViewButtonColumn();
                colButton.HeaderText = "";
                colButton.Name = "btnLookupJob";
                colButton.Text = "Lookup Job";
                colButton.UseColumnTextForButtonValue = true;

                dgvViewJobLinker.Columns.Insert(0, colButton);
                dgvViewJobLinker.Columns[0].Width = 80;

                dgvViewJobLinker.Columns[1].Width = 100;
                dgvViewJobLinker.Columns[1].ReadOnly = false;
                dgvViewJobLinker.Columns[1].HeaderText = "Job Number";

                dgvViewJobLinker.Columns[2].Width = 100;
                dgvViewJobLinker.Columns[2].ReadOnly = true;
                dgvViewJobLinker.Columns[2].HeaderText = "GL Account";

                dgvViewJobLinker.Columns[3].Width = 84;
                dgvViewJobLinker.Columns[3].ReadOnly = true;
                dgvViewJobLinker.Columns[3].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                dgvViewJobLinker.Columns[3].DefaultCellStyle.Format = "c";
                dgvViewJobLinker.Columns[3].HeaderText = "Debit Amount";

                dgvViewJobLinker.Columns[4].Width = 84;
                dgvViewJobLinker.Columns[4].ReadOnly = true;
                dgvViewJobLinker.Columns[4].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                dgvViewJobLinker.Columns[4].DefaultCellStyle.Format = "c";
                dgvViewJobLinker.Columns[4].HeaderText = "Credit Amount";

                dgvViewJobLinker.Columns[5].Width = 100;
                dgvViewJobLinker.Columns[5].ReadOnly = true;
                dgvViewJobLinker.Columns[5].HeaderText = "Dist Type";

                /* Add a new Column (ComboBox) for the Transaction Type column */
                DataGridViewComboBoxColumn colType = new DataGridViewComboBoxColumn();

                colType.DefaultCellStyle.NullValue = " ";
                colType.DataSource = TrxTypeDataSet.Tables[0];
                colType.ValueMember = DataDataSet.Tables[0].Columns[5].ColumnName.ToString();
                colType.DisplayMember = colType.ValueMember;
                colType.HeaderText = "Transaction Type";
                colType.DropDownWidth = 90;
                colType.Width = 90;
                colType.MaxDropDownItems = 7;
                colType.DataPropertyName = DataDataSet.Tables[0].Columns[5].ColumnName.ToString();
                colType.Name = DataDataSet.Tables[0].Columns[5].ColumnName.ToString();
                dgvViewJobLinker.Columns.Insert(6, colType);

                dgvViewJobLinker.Columns[7].Width = 292;
                dgvViewJobLinker.Columns[7].ReadOnly = false;
                dgvViewJobLinker.Columns[7].HeaderText = "Dist Reference";

                dgvViewJobLinker.Columns[8].Visible = false;
                dgvViewJobLinker.Columns[9].Visible = false;
                dgvViewJobLinker.Columns[10].Visible = false;
                dgvViewJobLinker.Columns[11].Visible = false;
                dgvViewJobLinker.Columns[12].Visible = false;
                dgvViewJobLinker.Columns[13].Visible = false;
                dgvViewJobLinker.Columns[14].Visible = false;
                dgvViewJobLinker.Columns[15].Visible = false;

                dgvViewJobLinker.EditMode = DataGridViewEditMode.EditOnEnter;
            catch (Exception ex)
                string eMsg = "004E: ERROR: " + ex.Message;
                if (stackTraceWanted) eMsg += "\n" + ex.StackTrace;

Open in new window

if condition should be this way

 if ( (DataDataSet == null) ||  (DataDataSet.Tables.Count == 0) || (DataDataSet.Tables[0].Rows.Count == 0))
your code


I see what you are saying but can you please explain the reasoning behind the checking for Null and Tables.Count =0? What is the diffrerence between these other checks?
If the query failed, then the dataset will be nothing.

If the query runs fine, dataset will not be nothing and table count will be 1 ( depending on how your logic populates dataset. In your case it is 1)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial