none
Cannot implicitly convert type 'double' to 'string'' RRS feed

  • Question

  •  for (iRow = 10; iRow <= xlWorkSheet.Rows.Count; iRow++)           

    {               

    if (xlWorkSheet.Cells[iRow, 10].value == null)               

    {                   

    break;              

    }               
    else               

    {                                 

    string[] row = new string[] { xlWorkSheet.Cells[iRow, 10].value,

    xlWorkSheet.Cells[iRow, 11].value.ToString(),

    xlWorkSheet.Cells[iRow, 12].value };               

    dataGridView1.Rows.Add(row);               

    }       

    }

    Any idea how to fix?
    Monday, May 13, 2019 3:40 AM

Answers

  • Build is has no problems/ errors. only when i try to read an excel file to datagrid view then this happens

    Since i cant upload an image i will just type it here.  

    Exception Unhandled

    Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'Cannot perform runtime binding on a null reference'

    That is an entirely different error than the one in the Subject of this thread.

    Since the error from trying to implicitly convert a double to a string is now 
    fixed apparently, you should close this thread and start a new one for the
    next problem. It is the preferred practice in these forums to address only
    one issue per thread. Additional problems even from the same code usually
    should be put in their own thread.

    >Since i cant upload an image i will just type it here.  

    You will be able to upload images once your account in these forums has been
    verified. That may take a day or more but will happen automatically eventually.
    It is just a procedure to try and block spamming of the forums.

    In the meantime you should be able to post images on a file sharing service
    and then post links in replies here so that we can retrieve the images.

    Microsoft's Onedrive is such a service, is free, and you can usually log into
    it using the same account and password used for these forums.

    - Wayne

    • Marked as answer by Mendel Lim Monday, May 13, 2019 6:40 AM
    • Unmarked as answer by Mendel Lim Monday, May 13, 2019 6:40 AM
    • Marked as answer by Mendel Lim Monday, May 13, 2019 6:44 AM
    Monday, May 13, 2019 6:35 AM

All replies


  • string[] row = new string[] { xlWorkSheet.Cells[iRow, 10].value,

    xlWorkSheet.Cells[iRow, 11].value.ToString(),

    xlWorkSheet.Cells[iRow, 12].value };               


    If the second initializer works

    xlWorkSheet.Cells[iRow, 11].value.ToString(),

    have you tried invoking ToString() with the other two as well?

     string[] row = new string[] { xlWorkSheet.Cells[iRow, 10].value.ToString(),
         xlWorkSheet.Cells[iRow, 11].value.ToString(),
         xlWorkSheet.Cells[iRow, 12].value.ToString() };

    - Wayne


    • Edited by WayneAKing Monday, May 13, 2019 5:26 AM
    Monday, May 13, 2019 4:23 AM
  • hello!

    tried but it still doesn't work, still gives the same error.


    • Edited by Mendel Lim Monday, May 13, 2019 5:14 AM
    Monday, May 13, 2019 5:13 AM

  • tried but it still doesn't work, still gives the same error.


    That doen't make sense. Assuming that each of the "value"s is a double it
    shouldn't show that error. For example:

    double x = 1, y = 2, z = 3;
    string[] row = new string[] { x.ToString(), y.ToString(), z.ToString() };
    

    will build and execute as expected.

    Are you sure you're looking at the right line?

    - Wayne

    Monday, May 13, 2019 5:33 AM
  • Assuming that each of the "value"s is a double it

    shouldn't show that error. For example:

    double x = 1, y = 2, z = 3;
    string[] row = new string[] { x.ToString(), y.ToString(), z.ToString() };

    will build and execute as expected.

    Are you sure you're looking at the right line?

    - Wayne

         {
                        
                    string[] row = new string[] { xlWorkSheet.Cells[iRow, 10].value.ToString(),
                    xlWorkSheet.Cells[iRow, 11].value.ToString(),
                    xlWorkSheet.Cells[iRow, 12].value.ToString() };  
                    dataGridView1.Rows.Add(row);
          }
    the code is as above. sorry for my poor knowledge on programming.  

    Monday, May 13, 2019 5:52 AM

  •      {
                        
                    string[] row = new string[] { xlWorkSheet.Cells[iRow, 10].value.ToString(),
                    xlWorkSheet.Cells[iRow, 11].value.ToString(),
                    xlWorkSheet.Cells[iRow, 12].value.ToString() };  
                    dataGridView1.Rows.Add(row);
          }
    the code is as above. 

    Are you seeing the error from Intellisense (a red squiggly line) or from a 
    compile error, or both? Intellisense should show which part of the code is
    causing the error. In the Error List window after a Build if you double 
    click on an error message it will take you to the line with the error in it.

    If you made the changes above - using ToString in three places - and are still
    seeing the same error message, make sure that it isn't pointing to another line
    that has the same issue. Fixing an error in one place does't fix it everywhere 
    in the program. Make sure you don't need an explicit ToString() conversion in
    other places as well as the original lines you posted.

    - Wayne

    Monday, May 13, 2019 6:05 AM
  • Build is has no problems/ errors. only when i try to read an excel file to datagrid view then this happens

    Since i cant upload an image i will just type it here.  

    Exception Unhandled

    Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'Cannot perform runtime binding on a null reference'

    Monday, May 13, 2019 6:20 AM
  • Build is has no problems/ errors. only when i try to read an excel file to datagrid view then this happens

    Since i cant upload an image i will just type it here.  

    Exception Unhandled

    Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'Cannot perform runtime binding on a null reference'

    That is an entirely different error than the one in the Subject of this thread.

    Since the error from trying to implicitly convert a double to a string is now 
    fixed apparently, you should close this thread and start a new one for the
    next problem. It is the preferred practice in these forums to address only
    one issue per thread. Additional problems even from the same code usually
    should be put in their own thread.

    >Since i cant upload an image i will just type it here.  

    You will be able to upload images once your account in these forums has been
    verified. That may take a day or more but will happen automatically eventually.
    It is just a procedure to try and block spamming of the forums.

    In the meantime you should be able to post images on a file sharing service
    and then post links in replies here so that we can retrieve the images.

    Microsoft's Onedrive is such a service, is free, and you can usually log into
    it using the same account and password used for these forums.

    - Wayne

    • Marked as answer by Mendel Lim Monday, May 13, 2019 6:40 AM
    • Unmarked as answer by Mendel Lim Monday, May 13, 2019 6:40 AM
    • Marked as answer by Mendel Lim Monday, May 13, 2019 6:44 AM
    Monday, May 13, 2019 6:35 AM