Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear Please, I ask for your support to solve a problem that I have in the following code:

ERROR: "The input string is not in the correct format"

CLASS:

Public Class Customer

Public Property SUBDEPT As String
Public Property EXISTENCIA As Single

End Class


FILL IN THE DATAGRIDVIEW AND THEN IT WILL BE SENT TO A SQL TABLE:


Dim dt As DataTable = Tables(cboSheet.SelectedItem.ToString())
DataGridView1.DataSource = dt
If dt IsNot Nothing Then
Dim list As List (Of Customer = New List (Of Customer ()
For i As Integer = 0 To dt.Rows.Count - 1
Dim Customer As Customer = New Customer ()
Customer.SUBDEPT = dt.Rows (i) ("SUBDEPT"). ToString ()
Customer.EXISTENCIA = dt.Rows (i) ("EXISTENCIA"). ToString () 'here generates error
list.Add (Customer)
Next
CustomerBindingSource.DataSource = list
End If

Thank You !!!!

What I have tried:

Customer.EXISTENCIA = Convert.ToSingle(dt.Rows (i) ("EXISTENCIA"). ToString ()) 'here generates error
Posted
Updated 18-Apr-21 19:57pm
Comments
[no name] 19-Apr-21 0:52am    
I'm guessing EXISTENCIA is a bool, and you're trying to assign a string.

1 solution

Never use Convert methods to parse "unverified" data (such as anything generated by a user, or stored in a text field in a database) - they will throw an exception when they meet a problem and that is what you are getting: the content of the column is not a valid single precision number.

We can't fix that - it's a data error and we have no access to your data - so you you need to code your app to be tolerant of such problems or to report such problems so they can be corrected before the data gets anywhere near your DB and starts doing real damage to your data integrity.
Always convert such input with TryParse methods: Single.TryParse Method (System) | Microsoft Docs[^] and report problems instead of continuing blindly!
 
Share this answer
 
Comments
Member 15158443 19-Apr-21 19:07pm    
Gerry, thank you very much for your support.

Best regards,

Mynor G.
Guatemala

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900