none
Operand type clash: int is incompatible with text

    Question

  • I'm want to insert values from a list object into a table.  Most of the values are text, a couple of int's.  I'm getting this exception

    Operand type clash: int is incompatible with text

    at the  line with the star

    toDBR = sqlCommand.ExecuteReader(); *

    I'm setting the object int values from the datagridviewusing this format

    int Value
    if (int.TryParse(datagridview.Rows[x].Cells[5].Value.ToString(), out Value)))
    {
    List.SetValue(Value);
    }

    I'm guessing this is where the problem is as this is the where I'm doing type conversion string to int. 

    Saturday, April 22, 2017 1:08 PM

All replies

  • Found the problem.  Two columns in a datagridview were in the wrong place(order) so when I was assigning values to a object in a certain order, a column type clashed with a object member type.   I switched the columns and checked that the types match which they do, string or null with string, int with int.  Despite this, its still giving me the error.  Annoying.
    Saturday, April 22, 2017 2:22 PM
  • Hello,

    This is why when possible always use the column name rather than ordinal position e.g.

    dataGridView1.Rows[0].Cells["Colum name goes here"].Value.ToString()

    If needed the index is obtained as follows

    dataGridView1.Columns["Column1"].DisplayIndex


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Saturday, April 22, 2017 2:49 PM
    Moderator
  • I tried using the column name instead of the ordinal number but kept getting a column name not found error, even though the datagridview column name is the same as the table column name.  Whilst I got it working with ordinal numbers after switching columns around, I later found what was causing the column name not found.  In the output window in VS, if you expand the list, it shows all the columns.  Instead of this

    Column: ColumnName

    it displays this

    Column: columnColumnName

    for some inexplicable reason VS append the word column before the column name, which makes no sense.   

    Saturday, April 22, 2017 7:34 PM
  • Been using code like this since version 1 of Visual Studio/C# and VB.NET, never seen that happen.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Saturday, April 22, 2017 7:45 PM
    Moderator
  • It looked odd to me, I may have interpreted it wrong though.  But still, unless there is another explanation for the error column name not found.  (Name of column in double quotes just for clarity). 
    Saturday, April 22, 2017 7:58 PM
  • If I have

    And use

    dataGridView1
        .Columns
        .Cast<DataGridViewColumn>()
        .ToList()
        .ForEach(col => Console.WriteLine($"'{col.Name}'"));

    I get expected results

    'UserNameColumn'
    'FirstNameColumn'
    'LastNameColumn'

    We can validate

    if (dataGridView1.Columns.Contains("UserNameColumn"))
    {
        // use it
    }
    else
    {
        // does not exist or the colum name has spaces in it
    }


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Saturday, April 22, 2017 8:07 PM
    Moderator