Byte Cast [] to String and from String to Bytes

1

I explain, I have a query in SQL that returns me sometimes Int's and sometimes Doubles

+--------+-----------+
| Algo   | Resultado |
+--------+-----------+
| Algo1  |   521     |
+--------+-----------+
| Algo2  |   5.12    |
+--------+-----------+
| Algo3  |   0       |
+--------+-----------+

I clarify since I can not change the query.

What I used to do was.

Byte[] val;
while(dr.read()){
  val = System.Text.Encoding.UTF8.GetBytes(dr.GetValue(2).ToString());
   objeto.setValor(System.Text.Encoding.Default.GetString(val));

 }

The other class:

 class Objeto
 {
     private double valor;
     //otros atributos

    public void setValor(String valor)
    {
        this.valor = Double.Parse(valor, System.Globalization.CultureInfo.InvariantCulture);
    }

And it used to work for me, but now I do not know why he says it to me

  

System.FormatException : 'The input string does not have the correct format.'

The query returns two types of data because it's something like this:

SELECT Count(*)
FROM Tabla
UNION
SELECT AVG(valores)
FROM Tabla

I clarify again that I can not modify it

    
asked by Aritzbn 09.03.2018 в 13:58
source

1 answer

1

It seems that the exception

  

System.FormatException : 'The input string does not have the correct format.'

was caused by dr.getValue(2) when passing it to String I returned a String "System.Byte[]" when I collected the information from the BBDD .

I have solved it by doing dr.getString(2) directly and it seems to work fine.

    
answered by 12.03.2018 / 09:29
source