The name 'data' does not exist in the current context [Resolved]

Posted by Mandlaa under ASP.NET MVC on 3/21/2014 | Points: 10 | Views : 2099 | Status : [Member] | Replies : 1
public ActionResult Newcars(string id)
{
string prod_data = (from p in db.ProductsHondaofConyers2s where p.TagNo == data select p.Quality_ValueAnalysis);
ProductsHondaofConyers2 p1 = db.ProductsHondaofConyers2s.Single(x => x.TagNo==id);
return View(p1);
}


In your controller, store the data from your database in a variable.

I am getting an error like this
ERROR:Cannot implicitly convert type 'System.Linq.IQueryable<string>' to 'string'




Responses

Posted by: A2H on: 3/22/2014 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
Hi,

In the below line of code you have used 'data' and you haven't declared the variable any where. This is the reason for the issue you mentioned in the subject line of this post.
 string prod_data = (from p in db.ProductsHondaofConyers2s where p.TagNo == data select p.Quality_ValueAnalysis);

To resolve this issue you need to change your query to us the id value instead like given below
string prod_data = (from p in db.ProductsHondaofConyers2s where (p.TagNo == id) select p.Quality_ValueAnalysis);


Lets take the second error now. when you select single value from Linq query you need to use either FirstOrDefault or SingleOrDefault

Please change your code like given below
string prod_data = (from p in db.ProductsHondaofConyers2s where (p.TagNo == id) select p.Quality_ValueAnalysis).FirstOrDefault();


Your Modified Code:
 public ActionResult Newcars(string id)

{

string prod_data = (from p in db.ProductsHondaofConyers2s where (p.TagNo == id) select p.Quality_ValueAnalysis).FirstOrDefault();

ProductsHondaofConyers2 p1 = db.ProductsHondaofConyers2s.Single(x => x.TagNo == id);

return View(p1);

}

Please mark my reply as answer if it answer your question


Thanks,
A2H
My Blog

Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response