Object cannot be cast from DBNull to other types

TonyReba
TonyReba used Ask the Experts™
on
I am getting tthe above error,

I have a GridView control in an asp.net 3.5 webpage, the following code executes in the RowDataBound Event and it changes the background color and font color, once a value in the columns: RegWaitTime, and TotalWegTime is bigger than 30,

The value comes from two computated columns in sql server which returns the result from the substraction from other two columns, the problem here is that if the values are NULL in those column, I will get and error on the code that changes the color,, sorry my english , please let me know if you need me to clarify my requirement,

Thanks in advance

  // Change Wait Time Cell Rows  CHECK THE LOGIC HERE

        if (e.Row.RowType == DataControlRowType.DataRow )
         {
             // This line will get the reference to the underlying row
             DataRowView _row = (DataRowView)e.Row.DataItem;
             if (_row != null)
             {
                 // get the field value which you want to compare and
                 // convert to the corresponding data type
                 // i assume the fieldName is of int type


                 int _field = Convert.ToInt32(_row.Row["RegWaitTime"]);

                 {


                     if (!DBNull.Value.Equals(_row.Row["RegWaitTime"]))


                         if (_field > 30)
                         {
                             e.Row.Cells[9].BackColor = System.Drawing.Color.Red;
                             e.Row.Cells[9].Style.Add("color", "white");

                         }
                         else
                             e.Row.Cells[9].BackColor = System.Drawing.Color.Green;
                     e.Row.Cells[9].Style.Add("color", "white");
                 }

             }
        
         }


        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            // This line will get the reference to the underlying row
            DataRowView _row2 = (DataRowView)e.Row.DataItem;


            if (_row2 != null)
            {
                // get the field value which you want to compare and
                // convert to the corresponding data type
                // i assume the fieldName is of int type
                int _field = Convert.ToInt32(_row2.Row["TotalRegTime"]);

                if (!DBNull.Value.Equals(_row2.Row["TotalRegTime"]))
                {
                    if (_field > 30)
                    {
                        e.Row.Cells[10].BackColor = System.Drawing.Color.Red;
                        e.Row.Cells[10].Style.Add("color", "white");

                    }
                    else
                        e.Row.Cells[10].BackColor = System.Drawing.Color.Green;
                    e.Row.Cells[10].Style.Add("color", "white");
                }
            }
        } 

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
The key to your problem is you need to check the DBNull.Value before you attempt to convert it with

int _field = Convert.ToInt32(_row.Row["RegWaitTime"]);

So
int _field = Convert.ToInt32(_row2.Row["TotalRegTime"]);

                if (!DBNull.Value.Equals(_row2.Row["TotalRegTime"]))
                {

Open in new window

becomes
if(!DBNull.Value.Equals(_row2.Row["TotalRegTime"])) {

                int _field = Convert.ToInt32(_row2.Row["TotalRegTime"]);

Open in new window


Then you will not get that error.

Author

Commented:
Thanks Bro, I completely missed that one.!

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