DB Null error - what is a good way to catch this error?

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 1289 Views - Last Post: 13 August 2012 - 09:09 AM Rate Topic: -----

#16 Cmore86  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 01-August 12

Re: DB Null error - what is a good way to catch this error?

Posted 10 August 2012 - 10:50 AM

View PostCurtis Rutland, on 10 August 2012 - 10:47 AM, said:

Ah, I see your issue here. You only want one row. You want to skip the null columns.

Yes, that should work, and yes you would need to do it for each column. However, you could do that in a loop.


Great thank you very much. I will try that out and stick into a loop. It happens rarely so I will see if this helps.
Was This Post Helpful? 0
  • +
  • -

#17 Cmore86  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 01-August 12

Re: DB Null error - what is a good way to catch this error?

Posted 10 August 2012 - 11:03 AM

This is what I attempted.

private void CheckNull()
              {
                  foreach(DataRow row in dvData.Rows[0]) 
                  {
                      object value = dvData;
                      if (value == DBNull.Value)
                          continue;
                      
} 
              }


I get an error: Unable to cast object type:

How do I use the continue; to skip inside the code if it finds the null. This way I don't have a loop looking for the nulls?
Was This Post Helpful? 0
  • +
  • -

#18 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1912
  • Posts: 5,708
  • Joined: 05-May 12

Re: DB Null error - what is a good way to catch this error?

Posted 10 August 2012 - 11:19 AM

A DataGridView.Rows contains DataGridViewRow's. A DataTable.Rows contains DataRow's.

Why are you checking the DataGridView? Why not check the DataTable right after you've loaded it, and not incur the cost of copying the data into the DataGridView and then just turn around and throw it away?

Additionally your continue statement in that for loop does nothing for you since it'll just continue the loop. You may want to make the function return true if a null is found, and return false if you get past the for loop.
Was This Post Helpful? 1
  • +
  • -

#19 Cmore86  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 01-August 12

Re: DB Null error - what is a good way to catch this error?

Posted 10 August 2012 - 11:30 AM

View PostSkydiver, on 10 August 2012 - 11:19 AM, said:

A DataGridView.Rows contains DataGridViewRow's. A DataTable.Rows contains DataRow's.

Why are you checking the DataGridView? Why not check the DataTable right after you've loaded it, and not incur the cost of copying the data into the DataGridView and then just turn around and throw it away?

Additionally your continue statement in that for loop does nothing for you since it'll just continue the loop. You may want to make the function return true if a null is found, and return false if you get past the for loop.


What is a good way to write out the structure of it?

Can you provide an example?

Thank you.
Was This Post Helpful? 0
  • +
  • -

#20 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1912
  • Posts: 5,708
  • Joined: 05-May 12

Re: DB Null error - what is a good way to catch this error?

Posted 10 August 2012 - 12:13 PM

In pseudo code:
bool CheckRowForNullColumnValues(DataRow row)
{
    if row.HasErrors
        return true;

    for each i where i is 0 to row.Table.Columns.Count - 1
    {
        if row[i] == null
            return true;
    }

    return false;
}


Was This Post Helpful? 1
  • +
  • -

#21 Cmore86  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 01-August 12

Re: DB Null error - what is a good way to catch this error?

Posted 10 August 2012 - 12:43 PM

View PostSkydiver, on 10 August 2012 - 12:13 PM, said:

In pseudo code:
bool CheckRowForNullColumnValues(DataRow row)
{
    if row.HasErrors
        return true;

    for each i where i is 0 to row.Table.Columns.Count - 1
    {
        if row[i] == null
            return true;
    }

    return false;
}


Attempted this and I get a whole bunch of errors that have to do with what is expected. I a missing the proper formating on it.
Was This Post Helpful? 0
  • +
  • -

#22 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1912
  • Posts: 5,708
  • Joined: 05-May 12

Re: DB Null error - what is a good way to catch this error?

Posted 10 August 2012 - 01:02 PM

Show us your code and what errors you are getting.
Was This Post Helpful? 0
  • +
  • -

#23 Cmore86  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 01-August 12

Re: DB Null error - what is a good way to catch this error?

Posted 10 August 2012 - 01:50 PM

I am getting a variety of syntax errors following your code.
bool CheckRowForNullColumnValues(DataRow row)
{
    if row.HasErrors
        return true;

    for each i where i is 0 to row.Table.Columns.Count - 1
    {
        if row[i] == null
            return true;
    }

    return false;
}


The code so far is:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Odbc;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace RSQ_App_V1
{
    public partial class RSQ : Form
    {
        public RSQ()
        {
            InitializeComponent();

            timer();
         
        }
      
        private void GetUCCXData(int queue)
        {
            
            try
            {
                //Initialize variables
                int contactsWaiting = 0;
                //rtcsqssummary = callswaiting
                int oldestContact = 0;
                //rtcsqssummary = oldestcontact
                int totalContacts = 0;
                //rtcsqssummary = totalcalls
                int contactsHandled = 0;
                //rtcsqssummary = callshandled
                int contactsAbandoned = 0;
                //rtcsqssummary = callsabandoned
                

                // Establishes Connection to UCCX
                OdbcConnection connection = new OdbcConnection();
                connection.ConnectionString = "DSN=UCCX";
            //Selects Data 3
                string queryString = "";
                if (queue == 1)
                {
                    queryString = "SELECT SUM(totalcalls) AS stotalcalls, SUM(callswaiting) AS scallswaiting, SUM(oldestcontact) AS soldestcontact, SUM(callshandled) AS scallshandled, SUM(callsabandoned) AS scallsabandoned, SUM(longesttalkduration) AS slongesttalkduration FROM rtcsqssummary WHERE csqname='RSQ-Spanish' OR csqname='RSQ-English'";
                }
                else if (queue == 2)
                {
                    queryString = "SELECT SUM(totalcalls) AS stotalcalls, SUM(callswaiting) AS scallswaiting, SUM(oldestcontact) AS soldestcontact, SUM(callshandled) AS scallshandled, SUM(callsabandoned) AS scallsabandoned, SUM(longesttalkduration) AS slongesttalkduration FROM rtcsqssummary WHERE csqname= 'RSQ-Elevator'";
                }
               
                // Query Command
                OdbcCommand command = new OdbcCommand(queryString, connection);
                //Open Connection
                connection.Open();
                //Executes Data Reader
                OdbcDataReader reader = command.ExecuteReader();
                DataTable dt = new DataTable("UCCXData");
                dt.Load(reader);
                reader.Close();
                connection.Close();
                //Fills the dataview
                dvData.DataSource = dt;
                
               
               
            

                if (queue == 1)
                {
                    //Resident Services                    
                    totalContacts = Convert.ToInt32(dvData.Rows[0].Cells["stotalcalls"].Value);
                    valCGTotal.Text = totalContacts.ToString();
                    contactsWaiting = Convert.ToInt32(dvData.Rows[0].Cells["scallswaiting"].Value);
                    valCGWaiting.Text = contactsWaiting.ToString();
                    oldestContact = Convert.ToInt32(dvData.Rows[0].Cells["soldestcontact"].Value);
                    valCGOldest.Text = CalculateTime(oldestContact).ToString();                
                    contactsHandled = Convert.ToInt32(dvData.Rows[0].Cells["scallshandled"].Value);
                    valCGHandled.Text = contactsHandled.ToString();
                    contactsAbandoned = Convert.ToInt32(dvData.Rows[0].Cells["scallsabandoned"].Value);
                    valCGAbandoned.Text = contactsAbandoned.ToString();                    
                    contactsHandled = Convert.ToInt32(dvData.Rows[0].Cells["scallshandled"].Value);
                    totalContacts = Convert.ToInt32(dvData.Rows[0].Cells["stotalcalls"].Value);
                    valCGPercentHandled.Text = CalculatePercentHandled(contactsHandled, totalContacts) + "%";                                    
                }
                else if (queue == 2)
                {
                    //Elevator 
                    totalContacts = Convert.ToInt32(dvData.Rows[0].Cells["stotalcalls"].Value);
                    valCETotal.Text = totalContacts.ToString();
                    contactsWaiting = Convert.ToInt32(dvData.Rows[0].Cells["scallswaiting"].Value);
                    valCEWaiting.Text = contactsWaiting.ToString();
                    oldestContact = Convert.ToInt32(dvData.Rows[0].Cells["soldestcontact"].Value);
                    valCEOldest.Text = CalculateTime(oldestContact).ToString();
                    contactsHandled = Convert.ToInt32(dvData.Rows[0].Cells["scallshandled"].Value);
                    valCEHandled.Text = contactsHandled.ToString();
                    contactsAbandoned = Convert.ToInt32(dvData.Rows[0].Cells["scallsabandoned"].Value);
                    valCEAbandoned.Text = contactsAbandoned.ToString();
                    contactsHandled = Convert.ToInt32(dvData.Rows[0].Cells["scallshandled"].Value);
                    totalContacts = Convert.ToInt32(dvData.Rows[0].Cells["stotalcalls"].Value);
                    valCEPercentHandled.Text = CalculatePercentHandled(contactsHandled, totalContacts) + "%";
                }
                //Render contacts waiting display - Change Color
                RenderContactsWaiting(oldestContact, contactsWaiting, queue);

            }
            catch (Exception ex)
            {
                //Error Textbox
                MessageBox.Show("there was an error - " + ex);
                errorBox1.Text = " " + ex;

            }
            
            
        }


        //Calculate Time
        private string CalculateTime(int millis)
        {

            int Hours = TimeSpan.FromMilliseconds(millis).Hours;
            int Minutes = TimeSpan.FromMilliseconds(millis).Minutes;
            int Seconds = TimeSpan.FromMilliseconds(millis).Seconds;

            return Hours.ToString("D2") + ":" + Minutes.ToString("D2") + ":" + Seconds.ToString("D2");

        }
        //Calculate Percent
        private string CalculatePercentHandled(double handled, int total)
        {

            if (total > 0)
            {
                return Math.Round(((handled / total) * 100)).ToString();
            }
            else
            {
                return "0";
            }

        }

        private void timer()
        {
            System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
            timer.Tick += new EventHandler(RSQ1_Load);
            timer.Interval = 3000;
            timer.Enabled = true;
            timer.Start();
        }

    
        private void RSQ1_Load(object sender, EventArgs e)
        {
            
            //Get Castle Group data - queue =1
            GetUCCXData(1);
            //Get Connection data - quueue =2
            GetUCCXData(2);
            
        }
        private void RenderContactsWaiting(int oldest, int waiting, int queue)
        {
            if (queue == 1)
            {
                if (oldest > 60000)
                {
                    valCGWaiting.ForeColor = System.Drawing.Color.Red;
                    playAlertSound();
                }
                else if (oldest > 40000 | waiting > 2)
                {
                    valCGWaiting.ForeColor = System.Drawing.Color.Yellow;
                }
                else
                {
                    valCGWaiting.ForeColor = System.Drawing.Color.Green;
                }
            }
            else if (queue == 2)
            {
                if (oldest > 60000)
                {
                    valCEWaiting.ForeColor = System.Drawing.Color.Red;
                    playAlertSound();
                }
                else if (oldest > 40000 | waiting > 2)
                {
                    valCEWaiting.ForeColor = System.Drawing.Color.Yellow;
                }
                else
                {
                    valCEWaiting.ForeColor = System.Drawing.Color.Green;
                }
            }

        }
        // Play Alert Sound
        private void playAlertSound()
        {
            System.Media.SoundPlayer Alert = new System.Media.SoundPlayer();
            Alert.SoundLocation = @"C:\Development\Resident Services App\RSQ App\Project\RSQ App\RSQ App Projects\RSQ App Simple\RSQ App V1\RSQ App V1\MGSAlert.wav";
            Alert.Play();
        }

    
    }
}



I am a little confused on where I have to place this in the code and how.
Was This Post Helpful? 0
  • +
  • -

#24 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1912
  • Posts: 5,708
  • Joined: 05-May 12

Re: DB Null error - what is a good way to catch this error?

Posted 10 August 2012 - 01:56 PM

I did say in post #20 that it was pseudo code. You even quoted it on post #21.

You'll have to translate that into real C# code.
Was This Post Helpful? 1
  • +
  • -

#25 Cmore86  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 01-August 12

Re: DB Null error - what is a good way to catch this error?

Posted 13 August 2012 - 09:09 AM

View PostSkydiver, on 10 August 2012 - 01:56 PM, said:

I did say in post #20 that it was pseudo code. You even quoted it on post #21.

You'll have to translate that into real C# code.


I'm a little lost at how I am supposed to translate that.

This DBNull error is random and happens on a DB that I have no control over.

Trying my best to figure out how to implement it, but I am stuck.

I would just like the app to continue running and not stop once it catches that exception.

Any help appreciated.

Thanks for all the other help, really helped me out.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2