Why do I get error when replacing datagridview values in vb?

Nek

I have a datagridview which holds an imported excel file. I also have a MySQL database which holds the values i want to replace in my datagridview and the values that will replace the previous values.

This is my database:

Database

What I want to accomplish is to loop inside the datagridview cells and find the values inside the Content field and replace it with the corresponding value in the Convert field. For example all 1 will be replaced by 11, all Name 1 will be replaced by Std Name 1, all 18 will be replaced by 18 Years Old.

I use the following code:

MysqlQuery = "SELECT * FROM contents"
    MysqlComm = New MySqlCommand(MysqlQuery, MysqlConn)
    MysqlReader = MysqlComm.ExecuteReader

    While MysqlReader.Read
        Dim content = MysqlReader.GetString("Content")
        Dim convert = MysqlReader.GetString("Convert")
        For Each row As DataGridViewRow In dgvFile.Rows
            For Each cell As DataGridViewCell In row.Cells
                If cell.Value IsNot Nothing Then
                    If cell.Value.ToString = content.ToString Then
                        cell.Value = convert.ToString
                    End If
                End If
            Next
        Next
    End While

        MessageBox.Show("File successfully converted")

This code converts 1 = 11 and Name 1 = Std Name 1 but not 18 = 18 Years old and it shows this error.

Output

QUESTION Why do I get this error and how can I fix this? Answers and suggestions are well appreciated. Thanks. :)

UPDATE For those who can't see the error message, here it is.

The following exception occurred in the DataGridView:

System.Exception: 18 Years Old is not a valid value for Double, --->
System.FormatException: Input string was not in a correct format.
 at System.Number.PraseDouble(String value, NumberStyles options,
NumberFormatInfo numfmt)
 at System.Double.Parse(String s, NumberStyles style, IFormatPRovider     provider)
 at System.ComponentModel.DoubleConverter.FromString(String value,   NumberFormatInfo formatInfo)
 at     System.ComponentModel.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext  context, CultureInfo culture, Object value)
--- End of inner exception stack trace ---
 at    System.ComponentModel.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext   context, CultureInfo culture, Object value)
 at System.ComponentModel.TypeConverter.ConvertFrom(Object value)
 at  System.Windows.Forms.DataGridView.DataGridViewDataConnection.PushValue(Int32  boundColumnIndex, Int32 columnIndex, Int32 rowIndex, Object value)

To replace this default dialog please handle the DataError event
ilans

Seems that's the line causing you the error:

cell.Value = convert.ToString

It is probably because the column type is <> String (probably integer or double).

You can either put the value in its right type, instead of doing ToString when you set the value.

Another option is to make the all column types of the grid String. But than you'd have to take in account that the sort of the column would have to be changed (you will need to find the sort event and convert the value to the right type when you sort).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why do I get an "invalid values" error when I try to access the network settings on Eclipse Mars?

From Dev

Why do i get the following error, when i try to click a asp.net button using vb.net?

From Dev

Why do I get NaN (Not a number) error when grabbing values from an array?

From Dev

Why do I get an error when trying to sum values of a column retrieved in a ResultSet?

From Dev

Why do I get this error when I run 'bower install'?

From Dev

Why do I get an error for "__CrtGetFileInformationByHandleEx " when I try to compile

From Dev

Why do I get this error when I run 'bower install'?

From Dev

Why do I get this error when installing npm now package?

From Dev

Why do I get a routing error when doing a delete action?

From Dev

Why do I get an error when creating an array in Swift?

From Dev

Why do I get the error "The handle is invalid" when invoking GetThreadTimes?

From Dev

Why do I get syntax error when converting with UFormat in Powershell

From Dev

Why do I get this error when trying to map on a list of lists?

From Dev

Why do I get an error when the pointer is not copied?

From Dev

Why do I get a compile error when calling start method?

From Dev

Why do I get an error when installing Pillow 3.0.0 on Ubuntu?

From Dev

Why do i get this error when trying to check if in Corona sdk?

From Dev

Why do I get an error when using list in python?

From Dev

Why do I get an error when passing a string as a filename, but not a char*?

From Dev

Why do I get error 255 when returning -1

From Dev

Why do I get an error statement when using an if comparison of ids

From Dev

Why do I get a Swift compiler error when importing Vapor?

From Dev

SQL, Why do I get this error when creating the reports table?

From Dev

Why do I get an 'Invariant Violation' error when using KeyboardAvoidView?

From Dev

Why do I get this error

From Dev

Why do I get the error saying "No value given for one or more required parameters." Vb.net

From Dev

Why do I get a Runtime exception when I try to find values I just put in the hashmap?

From Dev

error C4996: visual studio: why do I get an error when I use fopen in c?

From Dev

I do not understand why i get this error: [...] got multiple values for argument [...]

Related Related

  1. 1

    Why do I get an "invalid values" error when I try to access the network settings on Eclipse Mars?

  2. 2

    Why do i get the following error, when i try to click a asp.net button using vb.net?

  3. 3

    Why do I get NaN (Not a number) error when grabbing values from an array?

  4. 4

    Why do I get an error when trying to sum values of a column retrieved in a ResultSet?

  5. 5

    Why do I get this error when I run 'bower install'?

  6. 6

    Why do I get an error for "__CrtGetFileInformationByHandleEx " when I try to compile

  7. 7

    Why do I get this error when I run 'bower install'?

  8. 8

    Why do I get this error when installing npm now package?

  9. 9

    Why do I get a routing error when doing a delete action?

  10. 10

    Why do I get an error when creating an array in Swift?

  11. 11

    Why do I get the error "The handle is invalid" when invoking GetThreadTimes?

  12. 12

    Why do I get syntax error when converting with UFormat in Powershell

  13. 13

    Why do I get this error when trying to map on a list of lists?

  14. 14

    Why do I get an error when the pointer is not copied?

  15. 15

    Why do I get a compile error when calling start method?

  16. 16

    Why do I get an error when installing Pillow 3.0.0 on Ubuntu?

  17. 17

    Why do i get this error when trying to check if in Corona sdk?

  18. 18

    Why do I get an error when using list in python?

  19. 19

    Why do I get an error when passing a string as a filename, but not a char*?

  20. 20

    Why do I get error 255 when returning -1

  21. 21

    Why do I get an error statement when using an if comparison of ids

  22. 22

    Why do I get a Swift compiler error when importing Vapor?

  23. 23

    SQL, Why do I get this error when creating the reports table?

  24. 24

    Why do I get an 'Invariant Violation' error when using KeyboardAvoidView?

  25. 25

    Why do I get this error

  26. 26

    Why do I get the error saying "No value given for one or more required parameters." Vb.net

  27. 27

    Why do I get a Runtime exception when I try to find values I just put in the hashmap?

  28. 28

    error C4996: visual studio: why do I get an error when I use fopen in c?

  29. 29

    I do not understand why i get this error: [...] got multiple values for argument [...]

HotTag

Archive