I am trying to pull data from database using postgre in C# and putting the values returned in label controls. I keep getting System.InvalidCasaeExeception. The database field is an integer so I a using a data reader to get the value.
here is my code
private void Get_Defects()
{
NpgsqlConnection conn = Connection.getConnection();
try
{
conn.Open();
NpgsqlCommand cmd = new NpgsqlCommand("select * from defect where defect_id >= :MinID and defect_id <= :MaxID and location_id = 102 and top_or_bottom = :TopBottom;", conn);
cmd.Parameters.Add(new NpgsqlParameter("MinID", MinID));
cmd.Parameters.Add(new NpgsqlParameter("MaxID", MaxID));
cmd.Parameters.Add(new NpgsqlParameter("TopBottom", TopBottom));
NpgsqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
lblCrookedPart.Text = dr.GetInt32(12).ToString();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
conn.Close();
}
}
}
Not sure why it wont work. I pulled the first element and it displays correctly. some of the data is integers but have null values in the DB. I tried an element with data but I get the cast exception error. Please help.