0

I am creating a simple inventory system in Asp.net MVC

I have got an error at this line below

int db_product = db.products.First(e => e.id == m.barcode_id);

5
  • One of those values (id or barcode_id) is an int, and one is a string, as stated by the error message. So you cannot compare the two. You will need to cast the string to an int, or the int to a string, to be able to compare them. Commented Oct 23, 2019 at 13:29
  • yes e.id should be interger. barcode_id is string . so how to cast it sir Commented Oct 23, 2019 at 13:30
  • The simplest way is with int.Parse(m.barcode_id) but it depends on what you expect the ID to be. It would always have to be a number for that to work. This might not be compatible with your database context if you're using entity framework for example. Commented Oct 23, 2019 at 13:31
  • int db_product = db.products.First(e => e.id.ToString()== m.barcode_id); Since you said barcode_id is a string, just convert your e.id to string and compare. Commented Oct 23, 2019 at 13:33
  • int db_product = db.products.First(e => e.id.ToString() == m.barcode_id); i did sir but i got the Error Cannot implicitly convert type to Int Commented Oct 23, 2019 at 13:37

1 Answer 1

2

As the comments noted, the error really says it all. One of your variables is an int and one is a string so they cannot be directly compared. You need to convert either the int to a string or the string to an int, I would do the latter in this case but both can work. Something like this could work:

var db_product = db.products.First(e => e.id.ToString() == m.barcode_id)
Sign up to request clarification or add additional context in comments.

4 Comments

if i write it like this way db.products.First(e => e.id.ToString() == m.barcode_id) it is working sir but int db_product = db.products.First(e => e.id.ToString() == m.barcode_id); db_product.qty = db_product.qty - m.qty; if i write it int db_product = like this error i need this variable to do the calucation db_product.qty = db_product.qty - m.qty; like this
It is hard to understand what you need based on that comment but why can't you do db_product.qty = db_product.qty - m.qty; on the next line? The first line find the product you need and the next calculates the new qty value.
yes sir. The first line find the product next calculates the new qty value.
Oh, actually your first problem is that you used int db_product but the thing you are getting is not an int, it is a product from your products table. Change the int db_product to be var db_product as I just did in my edit, then add the second line where you do the subtraction.

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.