Object cannot be cast from DBNull to other types

i have a method that getruntime() currently there is no records in that columns hence it represents null, so when i am setting that value to a variable i am getting the Object cannot be cast from DBNull to other types.

 public int getruntime(int timeused, string breaks, string setup, string shiftcleaning, string trail, string line, string machine, string plant)
            int value = 0;
            int? pm = 0;
            int? sc = 0;
 SqlConnection sqlcon = new SqlConnection(con);
            string query = "SELECT [Planned Maintenance],[Sanitation Cleaning] FROM General WHERE [Line] = \'" + line + "\' AND [Machine] = \'" + machine + "\' AND [Plant] = \'" + plant + "\'";
            //SqlCommand com = new SqlCommand(query, sqlcon);
            SqlDataAdapter sda = new SqlDataAdapter(query, con);
            DataTable dt = new DataTable();          
            if (dt.Rows.Count > 0)
                var outputParam = Convert.ToInt16(dt.Rows[0]["Planned Maintenance"]); <- error comes on this line of the code. 
                if (!(outputParam is DBNull))
                    pm = Convert.ToInt16(outputParam);
                    pm = 0;
                var outputParamm = Convert.ToInt16(dt.Rows[1]["Sanitation Cleaning"]);
                if (!(outputParamm is DBNull))
                    sc = Convert.ToInt16(outputParamm);
                    sc = 0;
 catch(Exception ex)
                lblErrorMessage.Text = ex.Message.ToString();
            value = (timeused - Convert.ToInt16(breaks) - Convert.ToInt16(setup) - Convert.ToInt16(shiftcleaning) - Convert.ToInt16(trail) - pm.Value - sc.Value);
            return value;


Open in new window

Aravind RanganathanWindows Application DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
Which is perfectly understandable.
You can not perform magic by transforming a 'zero' into some other object.

I suspect you need something like
if(!(dt.Rows[0]["Planned Maintenance"]  is DBNull))
  //Nothing - add your new code here when that is the case
//your current code goes here

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Aravind RanganathanWindows Application DeveloperAuthor Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.