I am trying to fill ComboBox in button click, but however I get the following error:
System.InvalidCastException: 'Specified cast is not valid.'
public void FillOrgUnit()
{
try
{
using (SqlConnection conn = new SqlConnection(@"Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=DesignSaoOsig1;Integrated Security=True"))
{
conn.Open();
string query = "SELECT DISTINCT OrgUnitID FROM tblZaposleni_AD ORDER BY OrgUnitID ASC";
SqlCommand cmd = new SqlCommand(query, conn);
using (SqlDataReader saReader = cmd.ExecuteReader())
{
while (saReader.Read())
{
string name = saReader.GetInt32(0).ToString();
ddlStatus.Items.Add(name);
}
}
}
}
catch (Exception)
{
throw;
}
}
I tried to convert string name
to int
but this doesn't work. I have no idea where the mistake comes from.
BTW : OrgUnitId
in database is type bigint