0

Wasn't sure how to phrase this question.. Let me explain my problem, I am querying a table that has a book value and a cancel value, so I do the following:

select book_value, cancel_value, (book_value - cancel_value) as total_value from mytable

in mysql workbench, this does exactly what I want. Now, when I query from vb.net, I get the following error:

System.InvalidCastException: Conversion from string "total_value: " to type 'Double' is not valid. ---> 
System.FormatException: Input string was not in a correct format.   
at Microsoft.VisualBasic.CompilerServices.Conversions.ParseDouble(String Value, NumberFormatInfo NumberFormat)   
at Microsoft.VisualBasic.CompilerServices.Conversions.ToDouble(String Value, NumberFormatInfo NumberFormat) 
  --- End of inner exception stack trace ---   at  Microsoft.VisualBasic.CompilerServices.Conversions.ToDouble(String Value, NumberFormatInfo NumberFormat)
  at Microsoft.VisualBasic.CompilerServices.Operators.AddObject(Object Left, Object Right)   
at cust_info_Default2.Page_Load(Object sender, EventArgs e) 
in line 32

Okay, so I can't convert a double to a string, that's fine. So how do I write a query that returns the same thing, but instead of making that column a "double," it comes back as a string?

Any other ideas would be greatly appreciated!

EDIT: code where error is raised is

        cmd.CommandText = "select book_value, cancel_value, (book_value - cancel_value) as total_value from mytable"
        reader = cmd.ExecuteReader()

EDIT 2: didn't take the right part where the exception is raised... Sorry for the confusion!

listy.Items.Add(col_list(u) + ": " + reader(col_list(u)))

col_list contains the column names returned by the reader. u is the variable I use in the for loop I am going through. and listy is a listbox that is created like

Dim listy As New ListBox

inside of the for loop.

Feign
  • 270
  • 10
  • 28
  • 5
    Perhaps you should show the code where the exception is raised. – Steve Jul 17 '13 at 20:22
  • 2
    According to the exception, you're not trying to convert a numeric value to a `double`. You're trying to convert the *literal string* `"total_value: "` to a `double`, which obviously won't work. As for *why* the error occurs, we can't possibly know without seeing the code. – David Jul 17 '13 at 20:23
  • Post your code where you are mapping your data from the database resultset into .NET objects. – Karl Anderson Jul 17 '13 at 20:26
  • Sorry about that... I put the code in there where the exception is raised. – Feign Jul 17 '13 at 20:27
  • @user2548215: Is the exception actually *raised* on the `ExecuteReader()` step, or on a later step? I only ask because nowhere in the code posted is there a literal string which matches what's in the error, nor is there a conversion to a `double`. – David Jul 17 '13 at 20:28
  • Sorry about the confusion... I didn't take the right part of the code before. – Feign Jul 17 '13 at 20:33
  • Change the plus signs to ampersands and see if you still get the error. See [this thread](http://stackoverflow.com/questions/734600/the-difference-between-and-for-joining-strings-in-vb-net) for more. I suspect reader(col_list(u)) is a Double, and it's trying to add that to the string returned by col_list(u) + ": ", which it obviously can't convert to a Double to add. – kwwallpe Jul 17 '13 at 20:40
  • Didn't work, maybe I could convert the double to a string? – Feign Jul 17 '13 at 20:44
  • @user2548215: You certainly should be able to convert any double to a string, so it's worth a try. Usually conversions like that are implicitly typed by the compiler where needed, but perhaps VB has ways of turning that off and that's in place here? – David Jul 17 '13 at 20:45
  • 1
    Does `reader(col_list(u)).ToString()` help? – shahkalpesh Jul 17 '13 at 20:46
  • Well, problem is solved by using .ToString() Wouldn't have found this without everyone's help. Turned out to be a simple problem, but I appreciate all the help :) – Feign Jul 17 '13 at 20:47
  • @shahkalpesh, yes it does :) I've never had to convert anything that comes back from a datareader, so I didn't expect that to be the problem. Thanks a lot for the help everyone! – Feign Jul 17 '13 at 20:50

1 Answers1

0

change this line

listy.Items.Add(col_list(u) + ": " + reader(col_list(u)))

to

listy.Items.Add(col_list(u) & ": " & reader(col_list(u)))
Mandeep Singh
  • 2,016
  • 7
  • 19
  • 32
  • Problem was solved. But no one posted an answer... reader(col_list(u)).ToString() was used to fix this. – Feign Jul 18 '13 at 21:13