4 Replies - 4014 Views - Last Post: 21 July 2011 - 05:52 AM Rate Topic: -----

#1 Ahleki  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 01-June 10

Conversion from string "" to type 'Double' is not vali

Posted 21 July 2011 - 04:59 AM

Am trying the add up the values of a column in a datagridview but "Conversion from string "" to type 'Double' is not valid." error message keeps on appearing: Here is how I have tried to fix the error
           Dim tot As Double

            For i As Integer = 0 To dg.RowCount - 1
                tot += Cdbl(dg.Rows(i).Cells(5).Value)
            Next

            txtTotal.Text = tot



Thanks In Advance

Is This A Good Question/Topic? 0
  • +

Replies To: Conversion from string "" to type 'Double' is not vali

#2 Nightfish  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 74
  • View blog
  • Posts: 158
  • Joined: 24-May 11

Re: Conversion from string "" to type 'Double' is not vali

Posted 21 July 2011 - 05:17 AM

Where have you tried to fix the error? All you do here is loop over all of your rows add try to calculate the sum. None of this is error handling.

The error message already gives you a pretty good hint of what's wrong. You have an empty string in one of your cells and as it says, it cannot convert that to double. How about you try to use an "if" clause to check for the empty string and if you find it, you add 0. That should fix your problem.
Was This Post Helpful? 0
  • +
  • -

#3 fixo  Icon User is offline

  • D.I.C Regular

Reputation: 85
  • View blog
  • Posts: 335
  • Joined: 10-May 09

Re: Conversion from string "" to type 'Double' is not vali

Posted 21 July 2011 - 05:28 AM

Sorry , just from my memory:
Dim tot As Double=0.0

 For i As Integer = 0 To dg.RowCount - 1
Double.TryParse(dg.Rows(i).Cells(5).Value, tot)
     tot += tot
 Next

 txtTotal.Text = tot.ToString

Was This Post Helpful? 0
  • +
  • -

#4 Ahleki  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 01-June 10

Re: Conversion from string "" to type 'Double' is not vali

Posted 21 July 2011 - 05:30 AM

Am trying to fix the error by converting the value of the cell to a double
    tot += CDbl(dg.Rows(i).Cells(5).Value)



And also there is no empty cell because am getting rid of the last row
This is how the values appear on the datagridview
	1	EASY WEIGH	KEYPAD	   1	100	
	2	EASY WEIGH	COMM PORT  1	100


Am summing the last column in this case
Was This Post Helpful? 0
  • +
  • -

#5 Nightfish  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 74
  • View blog
  • Posts: 158
  • Joined: 24-May 11

Re: Conversion from string "" to type 'Double' is not vali

Posted 21 July 2011 - 05:52 AM

Well, if it says it cannot convert to double, trying to convert to double is unlikely to fix your problem. Try putting a print statement inside the loop where the math is so you can see what you are trying to add here.

By the way: Check your values. If it's like this:

cell(0) = "1"
cell(1) = "easy weigh"
cell(2) = "keypad"
cell(3) = "1"
cell(4) = "100"

I can tell you right there why "cell(5)" cannot be converted to double. Because there is no cell(5).

This post has been edited by Nightfish: 21 July 2011 - 06:10 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1