0

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

Dale K
  • 25,246
  • 15
  • 42
  • 71

2 Answers2

0

BigInt equivalent in C# is Int64.

string name = saReader.GetInt64(0).ToString();

Reference - https://stackoverflow.com/a/968734/1182982

Yasser Shaikh
  • 46,934
  • 46
  • 204
  • 281
  • 1
    If `OrgUnitId` is `nullable` then you have to put condition for `DBNull`. You can put condition as `if (!saReader.IsDBNull(0)) { // your code }` – Karan Jun 01 '20 at 05:46
  • @Karan This throw exception. –  Jun 01 '20 at 06:45
0

If you install Dapper, all that code you wrote can probably be reduced to:

using(conn = new SqlConnection(...))
   ddlStatus.DataSource = conn.Query("SELECT DISTINCT OrgUnitID FROM tblZaposleni_AD ORDER BY OrgUnitID ASC").ToList();

(And it wouldn't have thrown this error)

Life is too short to spend it writing endless GetString(1), GetString(2), GetInt(3) ... :)

Dapper's pretty neat- it can even take your queries and turn them into your classes for you so it'll just return you a Person when you "select * from person", it does all the setting of the properties for you. You can take a look at what it does/some example usage without installing it at dapper-tutorial.net (no affiliation)

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
  • Thank you for reccomendation but so far I just create test application and try to resolve some another issue. –  Jun 01 '20 at 06:45
  • No, I mean I just start learning some new thinks in `C#` and I try to test something. But however, thank you for recommendation and suggestions. :) –  Jun 01 '20 at 07:12
  • Dapper, Entity Franework or something else similar to make your database life easier is definitely one of the things you should learn if you're in a learning mood - if you think about it, you use the Forms visual designer to create your UI, and it does all the code for you (look in the Form1.Designer.cs file to find the code it writes) so you should of course try to do the same with your data access code; life's too short to write long, repetitive, boring code (like all the code the forms designer sites for you, and like all the data access code you write in your question) – Caius Jard Jun 01 '20 at 07:16
  • Is there any tutorial how to use `Dapper` ? Can you post like or some Documentation ? As well, I alredy learned EntityFramework and it's look good, but I have never heard about `Dapper` –  Jun 01 '20 at 07:18
  • I mentioned http://dapper-tutorial.net at the bottom of the answer, but there will certainly be other sites that are similar – Caius Jard Jun 01 '20 at 07:24