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,668
  • 2
  • 35
  • 49

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 );
ocuenca
  • 38,548
  • 11
  • 89
  • 102
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
  • 208,112
  • 36
  • 198
  • 279
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
  • 3,149
  • 1
  • 19
  • 22