Cast from string "" to type 'Double' is not valid

G

Guest

Hello, ¿why this code generates an error?:

Dim oReg As ADODB.Recordset = oBD.SQL("SELECT * FROM Trabajos")
Dim oDt As New DataTable("Trabajos")
Dim oDA As New OleDB.OleDBDataAdapter()

oDA.Fill(oDt, oReg)

Dim oRow As DataRow = oDt.NewRow()

oRow("Horas") = 3 ' error appears here.
.....

The error is:

Excepción no controlada del tipo 'System.InvalidCastException' en
system.data.dll

Información adicional: Cast from string "" to type 'Double' is not valid

Database is MySQL and column "Horas" is DOUBLE (5, 2). No other column
causes error. Assigning value 0 cuases no error.

Assigning values to that column by using ADO recordset works fine.

Someone could help me?
 
C

Cor Ligthert [MVP]

Dim oRow As DataRow = oDt.NewRow()

oRow("Horas") = 3 ' error appears here.
....
The error is strange, did you already try

oRow("Horas") = Cdbl(3)

To see what is the result of that?

Cor
 
J

Jim Underwood

Santi,
Let me see if I understand...
oRow("Horas") = 3 fails, but oRow("Horas") = 0 succeeds?
 
G

Guest

At first time I thougth that, but it happens only when trying to modify the
field. When row is created it contains 0 by default, so when trying to assign
0 causes no error because row is not altered.

Thanks.

"Jim Underwood" escribió:
 
J

Jim Underwood

If that does not work, try
oRow("Horas") = "3"

However, I think we are looking at the wrong line of code. The error
message references and empty string, which I do not see in the code below.
Are you posting the exact code that you are using, or changing it to make it
simpler for us to review? Specifically, in the following line:

oRow("Horas") = 3

Are you actually using the literal 3, or are you using a variable that has a
value of 3?

Posting a little more code may make this clearer.
 
G

Guest

In my first post I've exposed simplified code. There goes exact code from
begining to line in which error happens:

Private Sub CargaTrabajos()
Dim oDt As New DataTable("Trabajos")
Dim oDA As New OleDb.OleDbDataAdapter
Dim oReg As ADODB.Recordset

' Get records
oReg = goAp.BD.SQL("SELECT SAT_Trabajos.ID, SAT_Trabajos.SAT,
SAT_Trabajos.Fecha, SAT_Trabajos.Tipo, SAT_Trabajos.Plazo, SAT_Trabajos.OT,
SAT_Trabajos.Concepto, SAT_Trabajos.Observaciones, SAT_Trabajos.Producto,
SAT_Trabajos.Serie, SAT_Trabajos.Estado, SAT_Trabajos.Asignado,
SAT_Trabajos.Cerrado, SAT_Tipos.Nombre AS NombreTipo, SAT_Estados.Nombre AS
NombreEstado, SAT_Trabajos.Horas, SAT_Trabajos.TipoHoras,
SAT_Trabajos.ClaseHoras, SAT_Trabajos.Material, SAT_Trabajos.Kilometros,
SAT_Trabajos.TipoKilometros, SAT_Trabajos.Vehiculo, SAT_Trabajos.Revisado
FROM (SAT_Trabajos LEFT JOIN SAT_Tipos ON SAT_Trabajos.Tipo = SAT_Tipos.ID)
LEFT JOIN SAT_Estados ON SAT_Trabajos.Estado = SAT_Estados.ID WHERE
SAT_Trabajos.SAT = " + Format(Me.Registro.Fields("ID").Value) + " ORDER BY
SAT_Trabajos.ID")
If oReg Is Nothing Then
MsgBox(goAp.BD.UltimoMensaje, MsgBoxStyle.Exclamation, "Error")
Exit Sub
End If

' Additional fields
oDt.Columns.Add("NombreOperario", GetType(String))
oDt.Columns.Add("NombreOT", GetType(String))
oDt.Columns.Add("NombreTipoHoras", GetType(String))
oDt.Columns.Add("NombreClaseHoras", GetType(String))
oDt.Columns.Add("NombreTipoKm", GetType(String))
oDt.Columns("NombreOT").DefaultValue = ""
oDt.Columns("NombreOperario").DefaultValue = ""
oDt.Columns("NombreTipoHoras").DefaultValue = ""
oDt.Columns("NombreClaseHoras").DefaultValue = ""
oDt.Columns("NombreTipoKm").DefaultValue = ""

oDA.Fill(oDt, oReg)
moDTOT = oDt
oReg.Close()

' Valores por defecto
oDt.Columns("Fecha").DefaultValue = Today
oDt.Columns("Concepto").DefaultValue = ""
oDt.Columns("Observaciones").DefaultValue = ""
oDt.Columns("OT").DefaultValue = 0
oDt.Columns("Producto").DefaultValue = ""
oDt.Columns("Serie").DefaultValue = ""
oDt.Columns("Asignado").DefaultValue = 0
oDt.Columns("NombreEstado").DefaultValue = ""
oDt.Columns("Horas").DefaultValue = 0
oDt.Columns("TipoHoras").DefaultValue = ""
oDt.Columns("ClaseHoras").DefaultValue = -1
oDt.Columns("Material").DefaultValue = ""
oDt.Columns("Kilometros").DefaultValue = 0
oDt.Columns("TipoKilometros").DefaultValue = ""
oDt.Columns("Vehiculo").DefaultValue = ""
oDt.Columns("Revisado").DefaultValue = 0

oDt.Rows(0)("Horas") = 3 ' Error appears here
...

This last line is only to reproduce error because really it happens when
datagrid that handles oDt updates a row, so this way datagrid is discarded as
error maker. Error happens at this line too.

Additional fields are to show related information in the datagrid.

I'm still making tests but I've confirmed that same code (copied and pasted)
works ok on another blank project. It's very strange.

Any advice about other tests to do to get error source?

Thanks.
 
G

Guest

Problem is resolved. Sorry.

The problem was with a datatable RowChanging() event handler in which row is
checked searching for errors. In a If statement inside the event handler
there was an invalid comparison between a integer column and a string literal
that caused that error. That comparison only runs when "Horas" column has a
non zero value, so this is the reason error only happens when assigning a
value <> 0.

As long as debugger reported error in assignment line instead of event
handler line in wich error really occurs (I don't know why), it was very
difficult to me to find the reason of this issue.

Sorry and thank all you very much.

"Santi" escribió:
 
C

Cor Ligthert [MVP]

Santi,

A simple question, do you have in top of your program

Option strict is On

Cor
 
J

Jim Underwood

I am glad you got it resolved. I have had the same problem with the
debugger not stepping through the event handlers and showing errors on the
line that triggers them rather than in the handler itself.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top