Avatar of rwheeler23
rwheeler23
Flag for United States of America asked on

VS 2008 C# Window Issue

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?
.NET Programming

Avatar of undefined
Last Comment
srikanthreddyn143

8/22/2022 - Mon
Orcbighter

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.
rwheeler23

ASKER
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.
srikanthreddyn143

Is it possible to paste your code?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
rwheeler23

ASKER
Here it is. Just look for the Rows.Count string.
private void DisplayJobLinker()
        {
            try
            {
                /* 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;

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

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

                DataCommand.CommandText = "select JOBNUMBER,ACTNUMST,DEBITAMT,CRDTAMNT,DISTTYPE,DISTTRXTYPE,DISTREF,BCHSOURC,TRANSNMBR,TRXTYPE,DSTSQNUM,CNTRLTYP,APTVCHNM,SPCLDIST,COMPANYID " +
                    "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);

                DataDataAdapter.Fill(DataDataSet);
                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();
                    DataConnection.Close();
                    this.Hide();
                    this.Dispose();
                }
                
                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.RemoveAt(6);
                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;
                dgvViewJobLinker.Focus();
            }
            catch (Exception ex)
            {
                string eMsg = "004E: ERROR: " + ex.Message;
                if (stackTraceWanted) eMsg += "\n" + ex.StackTrace;
                MessageBox.Show(eMsg);

Open in new window

ASKER CERTIFIED SOLUTION
srikanthreddyn143

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
rwheeler23

ASKER
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?
srikanthreddyn143

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)
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.