2

I have made a method that can grab all row values from a specific column in a database and cast them into a list as integers. when i ran this code on my personal computer it worked like a charm, but on my company computer this code throws an exception.

This is the code:

static void Main(string[] args)
        {
            int norm = 43;
            List<int> List = new List<int>();

            string connString = ///

            DataTable dt = new DataTable();

            using (OleDbConnection conn = new(connString))
            {
                OleDbCommand cmd = new("SELECT * FROM USERS", conn);

                conn.Open();

                OleDbDataAdapter adapter = new(cmd);

                adapter.Fill(dt);
            }


            try
            {
                if (norm == 33)
                {
                    for (int i = 0; i < 9; i++)
                    {
                        List.Add(Convert.ToInt32(dt.Rows[i].Field<int>(1)));
                    }
                    Console.WriteLine(LeftSegmentIndex(List, 27));
                }
                else if (norm == 43)
                {
                    for (int i = 9; i < dt.Rows.Count; i++)
                    {
                        List.Add(Convert.ToInt32(dt.Rows[i].Field<int>(1)));
                    }
                    Console.WriteLine(LeftSegmentIndex(List, 27));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }


        }

The code throws the following exception:

System.InvalidCastException: Unable to cast object of type 'System.Double' to type 'System.Int32'.
   at System.Data.DataRowExtensions.UnboxT`1.ValueField(Object value)
   at System.Data.DataRowExtensions.Field[T](DataRow row, Int32 columnIndex)

This exception occurs on line 46.

the database has numbers like this :

Soc Art T_c
12  13  11
15  17  13
18  20  16
22  24  19
25  28  23
28  32  26
32  36  30
36  39  35
44  44  44
13  14  12
16  17  14
19  21  17
22  25  21
26  29  24
30  33  28
33  37  32
37  40  36
44  44  44

Can someone please explain why this error gets thrown at my company computer but not on my personal computer?

Thanks.

4
  • What kind of database is used, is it Oracle? Apr 8, 2021 at 10:27
  • sorry i forgot to mention it is a Microsoft Access Db
    – James VH
    Apr 8, 2021 at 10:34
  • 1
    check the column types in the database. They may be integers on yours but numeric(n, 0) on the company computer. Apr 8, 2021 at 10:39
  • thnks i got it!
    – James VH
    Apr 8, 2021 at 10:58

1 Answer 1

4

The error tells you that the underlying value is double and you are trying to read it as int. If the type is different in different DBs - don't assume int, just let Convert.ToInt32 do its work:

List.Add(Convert.ToInt32(dt.Rows[i][1]));

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.