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? |
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 |
Quote:
|
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?
|
Quote:
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? |
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 |
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. |