LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Subtracting numbers in a string using C# (https://www.linuxquestions.org/questions/programming-9/subtracting-numbers-in-a-string-using-c-402029/)

mrobertson 01-11-2006 08:49 AM

Subtracting numbers in a string using C#
 
I am tring to use the following querystring in C#:

string Querystring = "SELECT * FROM tcm_reported_coil@L3 where grade_code ='" + txtgradecode.Text + "' and (width between " + CStr(txtwidth.Text - 0.5) + " and " + CStr(txtwidth.Text + 0.5) + ") and (hot_band_gauge between " + CStr(txthbgauge.Text - 0.005) + " and " + CStr(txthbgauge.Text + 0.005) + ") and (target_gauge between " + CStr(txttargetgauge.Text - 0.005) + " and " + CStr(txttargetgauge.Text + 0.005) + ") order by schedule_number, sequence_number";

As a result I am getting the following error:

Operator '-' cannot be applied to operands of type 'string' and 'double'

What needs to be done so that I can subtract two numbers in my string?

graemef 01-11-2006 08:53 AM

I don't know C# but I would expect that you need to convert the string to a number perform the arithmetic operation and then convert the result to a string.

To make the code clearer you may want to do that in their own statements.

graeme

PTrenholme 01-11-2006 11:19 AM

Quote:

Originally Posted by mrobertson
[snip]
CStr(txtwidth.Text - 0.5)
[snip]

As a result I am getting the following error:

Operator '-' cannot be applied to operands of type 'string' and 'double'

What needs to be done so that I can subtract two numbers in my string?

What is the type of value returned by txtwidth.Text? I, too, don't do C#, but I suspect that your (first) type mismatch is there. From the error message, it seems likely that txtwidth is returning a string, since 0.5 is clearly a double. Perhaps you want length.Text? (Just guessing here.)

mrobertson 01-11-2006 12:00 PM

txtwidth.Text is a number such as 42.5678. I just want to select a range that is .5 higher and lower than that. What hould I do to get this result?

PTrenholme 01-11-2006 12:51 PM

Quote:

Originally Posted by mrobertson
txtwidth.Text is a number such as 42.5678. I just want to select a range that is .5 higher and lower than that. What hould I do to get this result?

O.K. What about txthbgauge.Text and txttargetgauge.Text? I think it's got to be one of them, since those are the only places the "-" occurs.

Also (again, I'm not familiar with C#.) have you tried "(txtwidth.Text) - 0.5", etc.?

A possible "work around:" Let SQL do it for you with constructs like "width between (" + CStr(txtwidth.Text)) +" - 0.5) and " . . .

As an aside, a decade ago, when I retired, the between SQL construct was not well implemented in most data base systems, and a query using it did not scale very well. For small tables -- a few Mb -- it could take several minutes to get a result, and, for a Tb size table, waiting for days was not uncommon. So, have you consulted with your DBA about table indices, and optimal query construction?

graemef 01-11-2006 01:32 PM

I suspect that Text is a string representation of a number. You should be able to typecast it to a double:

Code:

(double)(txtwidth.Text) - 0.5
graeme.

tekkieman 01-11-2006 02:30 PM

In C#, to cast the text representation of the number, you could use Double value = Double.Parse(string)


All times are GMT -5. The time now is 04:17 AM.