0

I get this error here is my code

var query1 = (from cust1 in context2.Clients
              where cust1.ID == int.Parse(textBox1.Text)
              select cust1).FirstOrDefault();`

even if my ID is an Int not a string

Dirk
  • 10,358
  • 2
  • 31
  • 46

3 Answers3

1

Call Parse method outside of your query:

var id=int.Parse(textBox1.Text);
var result=context2.Clients.FirstOrDefault(cust=> cust.ID ==id );
octavioccl
  • 37,218
  • 8
  • 83
  • 98
0

I imagine the simplest solution would be to convert to an int before the query:

var id = int.Parse(textBox1.Text);
var query1 = (from cust1 in context2.Clients
              where cust1.ID == id
              select cust1).FirstOrDefault();

Or, for the sake of error handling:

var id = 0;
if (!int.TryParse(textBox1.Text, out id)) {
    // raise an error condition
}
var query1 = (from cust1 in context2.Clients
              where cust1.ID == id
              select cust1).FirstOrDefault();
David
  • 188,958
  • 33
  • 188
  • 262
0

You have to set the int.Parse to a variable first, like this:

var intValue = int.Parse(textBox1.Text);   
 var query1 = (from cust1 in context2.Clients
                          where cust1.ID == intValue
                          select cust1).FirstOrDefault();
Daniel Stackenland
  • 2,944
  • 1
  • 18
  • 22