Sqldatareader invalid cast exception getDouble

0

I'm trying to get Double type values from an sql datareader, so all right, it happens that when I do a dr.getDouble (2) it gives me invalid cast exception.

  

System.InvalidCastException: 'Unable to convert an object of type' System.Byte [] 'to type' System.IConvertible '.'

I make a query mysql would be something of the style:

Select str1, 'un alias cualquiera', count(*)
from tabla
where valor > 30 and str1 like('Algo')
group by str1
union
Select str1, 'un alias cualquiera', count(*)
from tabla
where valor > 40 and str1 like('Algo')
group by str1
union
...

Table of values

+-----------+--------------+---------+
| String    |  Alias       |  1.92   |
+-----------+--------------+---------+
| String    |  Alias       |  98     |
+-----------+--------------+---------+
| String    |  Alias       | 43.23   |
+-----------+--------------+---------+

This is what I try in my code.

MySqlConnection conn = null;
MySqlCommand consulta = null;
MySqlDataReader dr;
Double val;

inicializarconexion(conn, consulta); //hasta aquí ningún problema.

val = dr.getDouble(2);  //Aquí salta la excepción. 

Capture of the count (*)

    
asked by Aritzbn 15.01.2018 в 10:54
source

1 answer

0

I respond to myself.

The problem was that in the count queries (*) it was like aliases, inside this there were a few avg to get media, hence the decimals.

Solution:

Byte[] val;            
val = (Byte[])dr.GetValue(2);
stat.setValor(System.Text.Encoding.Default.GetString(val));

To pass after String to double. (Although it sounds convoluted)

Double.Parse(val,System.Globalization.CultureInfo.InvariantCulture);
    
answered by 15.01.2018 / 12:06
source