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

Object cannot be cast from DBNull to other types

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

ASP.NETMicrosoft SQL Server 2005C#

Avatar of undefined
Last Comment
TonyReba

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
DBAduck - Ben Miller

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
TonyReba

ASKER
Thanks Bro, I completely missed that one.!
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