0

I am getting an error that makes complete sense, but makes no sense to me as to why I am getting it..

My error is Conversion from String to type Double is not valid. I have debugged this and every value is in " " and I could not see anything that would possibly try to be converted to a Double, other than the two cost fields I have, so I place a ToString() on them and it still isn't liking it. I am building a table to attach to the body of an email is the end product of this code below. Can someone please explain to me why I am getting the above error?

Dim body as String  
Dim param As Object() = New Object() { _
                            drpDownLstEnt.SelectedValue(), _
                            txtBoxEligibility.Text, _
                            txtBoxCondition.Text, _
                            approverType, _
                            txtBoxNameOfApprover.Text, _
                            txtBoxOriginatorResults.Text, _
                            txtBoxDate.Text, _
                            drpDownLstNameNumber.SelectedValue(), _
                            lblInternalNameList.Text, _
                            lblExternalNameList.Text, _
                            txtBoxTotalAttendees.Text, _
                            txtBoxCustBeingEnt.Text, _
                            txtBoxEstCost.Text, _
                            txtBoxCostPerPerson.Text, _
                            txtBoxLocation.Text, _
                            txtBoxNameOfEst.Text, _
                            txtBoxComments.Text}

body= String.Format("<table><tr><td>Field 0</td><td>{0}</td></tr>" _
                             + "<tr><td>Field 1</td><td>{1}</td></tr>" _
                             + "<tr><td>Field 2</td><td>{2}</td></tr>" _
                             + "<tr><td>Field 3</td><td>{3}</td></tr>" _
                             + "<tr><td>Field 4</td><td>{4}</td></tr>" _
                             + "<tr><td>Field 5</td><td>{5}</td></tr>" _
                             + "<tr><td>Field 6</td><td>{6}</td></tr>" _
                             + "<tr><td>Field 7</td><td>{7}</td></tr>" _
                             + "<tr><td>Field 8</td><td>{8}</td></tr>" _
                             + "<tr><td>Field 9</td><td>{9}</td></tr>" _
                             + "<tr><td>Field 10</td><td>{10}</td></tr>" _
                             + "<tr><td>Field 11</td><td>{11}</td></tr>" _
                             + "<tr><td>Field 12</td><td>{12}</td></tr>" _
                             + "<tr><td>Field 13</td><td>{13}</td></tr>" _
                             + "<tr><td>Field 14</td><td>{14}</td></tr>" _
                             + "<tr><td>Field 15</td><td>{15}</td></tr>" _
                             + "<tr><td>Field 16</td><td>{16}</td></tr></table>", _
                             param)

message.Body = (currentUser + vbCrLf + vbCrLf + _
                    "Here is your Form ID for the form that was submitted on " _
                    + emailTimeStamp _
                    + ". You will also see a copy of your completed form below." _
                    + vbCrLf + vbCrLf + "Form ID: " + formID + vbCrLf + vbCrLf _
                    + "Completed Form:" + vbCrLf + vbCrLf + emailBody)
17
  • Canyou give the exact error message you are receiving? Commented Oct 31, 2014 at 21:45
  • drpDownLstEnt.SelectedValue is a property not a method. Remove the (). Also for drpDownLstNameNumber Commented Oct 31, 2014 at 21:46
  • You might want to break up those into multiple lines. Its really hard to read the code as is. Commented Oct 31, 2014 at 21:46
  • 1
    what line number does the compiler give you? You also appear to have an error here: </table>, param), I'm thinking you intended </table>", param) Commented Oct 31, 2014 at 21:46
  • @Steve: That's just VB weirdness - VB allows an empty parameter list on a property call and allows omitting it on a method call. Not related to the OP's problem. Commented Oct 31, 2014 at 21:47

3 Answers 3

1

One of the problems in the code you've posted is that you need a final double-quote after .

So not:

+ "<tr><td>Field 16</td><td>{16}</td></tr></table>, param)

But:

+ "<tr><td>Field 16</td><td>{16}</td></tr></table>", param)

But the code wouldn't compile in that case, unless you had some other weirdness. It doesn't really make sense that you're getting that error in the definition of param, given that it's an Object(). Unless you're using some sort of custom control or DependencyProperty that's converting text or a selection from String into a Double in its getter - in which case you'd get the above error if the control had no selection - it doesn't actually seem possible for the code you provided to be causing the error you provided.

Sign up to request clarification or add additional context in comments.

Comments

0

You could get an error like that when accessing the SelectedValue property of a combo box if the ValueMember refers to a property that is not performing a valid conversion. For example:

Public Class Form1

   Protected Overrides Sub OnLoad(e As EventArgs)
      MyBase.OnLoad(e)
      Dim i As New SampleClass() With {.MyStringValue = "Not a number"}
      ComboBox1.ValueMember = "MyValue"
      ComboBox1.DisplayMember = "MyStringValue"
      ComboBox1.DataSource = {i}
   End Sub

   Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
      System.Diagnostics.Debug.WriteLine(ComboBox1.SelectedValue)
   End Sub

   Public Class SampleClass
      Private _myValue As Object

      Public Property MyValue As Double
         Get
            Return DirectCast(_myValue, Double)
         End Get
         Set(value As Double)
            _myValue = CStr(value)
         End Set
      End Property

      Public Property MyStringValue As String
         Set(value As String)
            _myValue = value
         End Set
         Get
            Return _myValue
         End Get
      End Property
   End Class
End Class

I suggest you take a closer look at what's happening when you access the SelectedValue property.

7 Comments

So what are you saying that I look at pertaining to SelectedValue property?
Check if merely accessing SelectedValue causes the error.
I have and accessing the SelectedValue is the only thing causing conversion error... But have tried ToString() method already and not sure what to try next
What is the type of .SelectedValue ?
@programmerGuy : Made an edit to my (not) answer : you can retrieve the Type of drpDownLstEnt.SelectedValue and drpDownLstNameNumber.SelectedValue if they are not Nothing.
|
0

Not An answer, but one way to find exactly what's wrong..

Replace this piece of code..

body = String.Format("<table><tr><td>Field 0</td><td>{0}</td></tr>" _
                         + "<tr><td>Field 1</td><td>{1}</td></tr>" _
                         + "<tr><td>Field 2</td><td>{2}</td></tr>" _
                         + "<tr><td>Field 3</td><td>{3}</td></tr>" _
                         + "<tr><td>Field 4</td><td>{4}</td></tr>" _
                         + "<tr><td>Field 5</td><td>{5}</td></tr>" _
                         + "<tr><td>Field 6</td><td>{6}</td></tr>" _
                         + "<tr><td>Field 7</td><td>{7}</td></tr>" _
                         + "<tr><td>Field 8</td><td>{8}</td></tr>" _
                         + "<tr><td>Field 9</td><td>{9}</td></tr>" _
                         + "<tr><td>Field 10</td><td>{10}</td></tr>" _
                         + "<tr><td>Field 11</td><td>{11}</td></tr>" _
                         + "<tr><td>Field 12</td><td>{12}</td></tr>" _
                         + "<tr><td>Field 13</td><td>{13}</td></tr>" _
                         + "<tr><td>Field 14</td><td>{14}</td></tr>" _
                         + "<tr><td>Field 15</td><td>{15}</td></tr>" _
                         + "<tr><td>Field 16</td><td>{16}</td></tr></table>", _
                         param)

...by this one :

body = "<table>"
For i As Integer = 0 To param.Length - 1
    System.Diagnostics.Debug.WriteLine( _
            "Conversion [" + i.ToString() + "] : " _
            + param(i).GetType().ToString() + " -> String")
    body = body _
           + "<tr><td>Field " _
           + i.ToString() + "</td><td>" _
           + param(i) + "</td></tr>"

           ' Option Strict forbid implicit casting param(i) -> String
           ' I assume you're on Option Strict Off
Loop
body = body + "</table>"

This will tell you exactly which element of param gives the error.
What's the Type of approverType ?
Answered : approverType is of Type String - Thanks.

2 Comments

approverType is a String
@programmerGuy : Thanks. It's not approverType.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.