1

I want to create a new variable using two string variables. But I keep getting the error message

"operator '-' cannot be applied to operands of type string and string c#".

string query3 = "SELECT Quantity FROM Supplier WHERE [Supplier ID]='"+supplierid+"'";
string query4 = "SELECT Quantity FROM Supplier WHERE [Book ID] = '" + bookid + "'";
SqlCommand cmd3 = new SqlCommand(query3, con);
SqlCommand cmd4 = new SqlCommand(query4, con);
con.Open();
string temporaryquantity = cmd3.ExecuteScalar().ToString();
string temporaryquantitystocks = cmd4.ExecuteScalar().ToString();
string totalcostforstocks = (temporaryquantitystocks-temporaryquantity + quantity) * buyingpriceperbook;

"quantity" is in int type
"buyingpriceperbook" is in double type

Can anyone help me with this?

6
  • 5
    Seems pretty clear - you can't "subtract" two strings ,even iof those strings represent numeric values. You have to convert them to numeric types first. Commented Feb 27, 2019 at 18:58
  • 1
    You can't add two strings together... You get a scalar out of your execute operation then immediately ToString it... You are trying to sum strings. Commented Feb 27, 2019 at 18:59
  • 3
    @Charleh You can "add" two strings (meaning you can use the + operator), but that concatenates them. Commented Feb 27, 2019 at 18:59
  • Yeah true, you knew what I meant though (mathematically)! Using operator+ here is not going to give you the results you want :( Commented Feb 27, 2019 at 19:01
  • "quantity" is an int type Commented Feb 27, 2019 at 19:03

2 Answers 2

0

You can not apply arithmetic operators to strings. Change to double or integer:

string query3 = "SELECT Quantity FROM Supplier WHERE [Supplier ID]='" + supplierid + "'";
string query4 = "SELECT Quantity FROM Supplier WHERE [Book ID] = '" + bookid + "'";

SqlCommand cmd3 = new SqlCommand(query3, con);
SqlCommand cmd4 = new SqlCommand(query4, con);
con.Open();
double temporaryquantity = Convert.ToDouble( cmd3.ExecuteScalar());
double temporaryquantitystocks = Convert.ToDouble(cmd4.ExecuteScalar());
double totalcostforstocks = (temporaryquantitystocks - temporaryquantity + quantity) * buyingpriceperbook;
Sign up to request clarification or add additional context in comments.

Comments

0

Convert to a numeric type rather than strings:

int temporaryquantity = (int)cmd3.ExecuteScalar();

Other things to note:

  • ExecuteScalar will give you only the first value from your query - are your queries guaranteed to only return one value? Or are you expecting something to sum up multiple values?
  • ExecuteScalar returns an object, meaning it can be a string, a double, an integer, null, etc. If you cast to int you are assuming that the query is returning a non-null integer value, otherwise it will fail at runtime.

2 Comments

Also, he may need to check for null.
Thanks a lot. Now it works perfectly fine. Thanks for explaining this to me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.