Aravind Ranganathan
asked on
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;
try
{
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);
sqlcon.Open();
DataTable dt = new DataTable();
sda.Fill(dt);
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);
}
else
{
pm = 0;
}
var outputParamm = Convert.ToInt16(dt.Rows[1]["Sanitation Cleaning"]);
if (!(outputParamm is DBNull))
{
sc = Convert.ToInt16(outputParamm);
}
else
{
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;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER