Converting text to double

  • Thread starter Frederik Vanderhaeghe
  • Start date
F

Frederik Vanderhaeghe

Hi,

I have a problem converting text to a double. Why doesn't the code work:
If Not (txtdocbedrag.Text = "") Then

Select Case ddlBedrag.SelectedIndex

Case 0

Case 1

whereSQL += "cast(bedrag as numeric(10,2)) > '" &
Double.Parse(txtdocbedrag.Text) & "' and "

Case 2

whereSQL += "cast(bedrag as numeric(10,2)) >= '" &
Double.Parse(txtdocbedrag.Text) & "' and "

Case 3

whereSQL += "cast(bedrag as numeric(10,2)) < '" &
Double.Parse(txtdocbedrag.Text) & "' and "

Case 4

whereSQL += "cast(bedrag as numeric(10,2)) <= '" &
Double.Parse(txtdocbedrag.Text) & "' and "

Case 5

whereSQL += "cast(bedrag as numeric(10,2)) = '" &
Double.Parse(txtdocbedrag.Text) & "' and "

Case 6

whereSQL += "cast(bedrag as numeric(10,2)) >= '" &
Double.Parse(txtdocbedrag.Text) & "' cast(bedrag as numeric(10,2)) < '" &
Double.Parse(txtBedragTot.Text) & "' and "

End Select

End If
 
P

pvdg42

Frederik Vanderhaeghe said:
Hi,

I have a problem converting text to a double. Why doesn't the code work:
If Not (txtdocbedrag.Text = "") Then

Select Case ddlBedrag.SelectedIndex

Case 0

Case 1

whereSQL += "cast(bedrag as numeric(10,2)) > '" &
Double.Parse(txtdocbedrag.Text) & "' and "

Case 2

whereSQL += "cast(bedrag as numeric(10,2)) >= '" &
Double.Parse(txtdocbedrag.Text) & "' and "

Case 3

whereSQL += "cast(bedrag as numeric(10,2)) < '" &
Double.Parse(txtdocbedrag.Text) & "' and "

Case 4

whereSQL += "cast(bedrag as numeric(10,2)) <= '" &
Double.Parse(txtdocbedrag.Text) & "' and "

Case 5

whereSQL += "cast(bedrag as numeric(10,2)) = '" &
Double.Parse(txtdocbedrag.Text) & "' and "

Case 6

whereSQL += "cast(bedrag as numeric(10,2)) >= '" &
Double.Parse(txtdocbedrag.Text) & "' cast(bedrag as numeric(10,2)) < '" &
Double.Parse(txtBedragTot.Text) & "' and "

End Select

End If
One thing I spotted is the use of single quote punctuation around a numeric
value (the result of Double.Parse(...).
In your SQL query (that is what you're building, correct?), numeric values
should have no punctuation characters surrounding them. String values need
single quotes, as to dates (in SQL Server. Access date punctuation is the
#.).
 
P

Pipo

I dont know but converting a string to a double does work:
Dim st As String = "2.0"
Dim d As Double
d = Double.Parse(st)
 
F

Frederik Vanderhaeghe

but the problem is the "cast(bedrag as numeric(10,2)"
bedrag=databasefield
 
J

Jeff Dillon

Define "doesn't work". Are you getting an error?

And what kind of syntax is "bedrag=databasefield".

apples=oranges

May I suggest you use some sort of meaningful naming convention?

Jeff
 
F

Frederik Vanderhaeghe

bedrag = databasefield is not a syntax it was just saying that it is a field
from the database, it's not in the code.
The eroor i get is: Error converting data type varchar to numeric
 
P

Pipo

What is the value of bedrag then?
Are you trying to convert text to a double?
Double.Parse("SomeText") wont work ofcourse.
 
F

Frederik Vanderhaeghe

bedrag is varchar in the database. I have to compare the varchar out of the
database with a value in a textbox, so i convert the varchar to double and i
parse the textbox value to double.
 
J

Jeff Dillon

Post the exact line, and the value of all variables, that is generating the
error. You can use CDbl too

Jeff
 
J

Jeff Dillon

then don't do that (?)

Obviously double.parse is failing. Catch the exception, and debug the
results.

Response.write, instead of issuing the sql (duh)

txtdocbedrag.text contains text you don't expect. Find out why

I find it amazing, and almost sad, the programmers these days can't figure
out reproducible problems.

I'm 50. I had issues like this 25 years ago, but before newgroups, granted.

Just my opinion
 
J

Jeff Dillon

Please show the resultant SQL

response.write strSQL

but don't actually execute it.

Jeff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top