LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 01-11-2006, 08:49 AM   #1
mrobertson
Member
 
Registered: May 2005
Posts: 275

Rep: Reputation: 30
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?
 
Old 01-11-2006, 08:53 AM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,376

Rep: Reputation: 147Reputation: 147
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
 
Old 01-11-2006, 11:19 AM   #3
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,141

Rep: Reputation: 329Reputation: 329Reputation: 329Reputation: 329
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.)
 
Old 01-11-2006, 12:00 PM   #4
mrobertson
Member
 
Registered: May 2005
Posts: 275

Original Poster
Rep: Reputation: 30
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?
 
Old 01-11-2006, 12:51 PM   #5
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,141

Rep: Reputation: 329Reputation: 329Reputation: 329Reputation: 329
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?
 
Old 01-11-2006, 01:32 PM   #6
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,376

Rep: Reputation: 147Reputation: 147
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.
 
Old 01-11-2006, 02:30 PM   #7
tekkieman
Member
 
Registered: Dec 2004
Location: Northern CA
Distribution: Mepis 6.5
Posts: 123

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Subtracting the day of two dates and recieving an integer answer(VB.NET) mrobertson Programming 1 08-15-2005 01:46 PM
Subtracting dates chrisk5527 Linux - General 6 06-10-2004 02:57 PM
Adding numbers, break on non-numbers... Cruger Programming 1 03-22-2004 09:18 AM
C making an int from a string of numbers ocularbob Programming 11 02-25-2004 02:24 AM
java test if string in string array is null. exodist Programming 3 02-21-2004 01:39 PM


All times are GMT -5. The time now is 11:44 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration