troubleshooting Question

Object cannot be cast from DBNull to other types

Avatar of TonyReba
TonyRebaFlag for United States of America asked on
C#ASP.NETMicrosoft SQL Server 2005
2 Comments1 Solution4562 ViewsLast Modified:
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");
                }
            }
        } 
ASKER CERTIFIED SOLUTION
DBAduck - Ben Miller
SQL Server Architect

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros