VorTex VorTex - 1 month ago 9
C# Question

I got error when i wanna use EDM Codes in my Windows Form

I wanted to try code a program which i wanted to use EDM in my codes and in my Visual studio. I coded everything (Even Save button and etc) but there is a problem that whenever i want to delete a record from my DataBase (which is on my Computer and Sql server 2014) i face with an error and my program suddenly crash and i don't know why. Can you tell me what can i do to solve this harras problem ? thanks in advance for your helpful answers.
Here is my Codes :

private void btnUpdate_Click(object sender, EventArgs e)
{
if (txtCode.Text == "")
{
MessageBox.Show("code morede nazar ra vared konid");
txtCode.Focus();
return;
}
tblfastfood1 w1 = FA.tblfastfood1.FirstOrDefault(x => x.Code == Convert.ToInt32(txtCode.Text));
if (w1 == null)
{
MessageBox.Show("chenin codi mojod nist");
txtCode.Focus();
return;
}
w1.Esm = txtEsmMoshtari.Text;
w1.Address = txtAddress.Text;
w1.GhazaAsli = comboBox1.Text;
w1.Noshidani = comboBox2.Text;
w1.Salad = comboBox3.Text;
FA.SaveChanges();
dataGridView1.DataSource = FA.tblfastfood1.ToList();
}


here is my error which i face :
LINQ to Entities does not recognize the method 'Int32 ToInt32(System.String)' method, and this method cannot be translated into a store expression.

Answer Source

This can't be translated to SQL, so it can't be used in your LINQ provider (probably LINQ to Entities):

Convert.ToInt32(txtCode.Text)

Instead of using it in the expression directly, perform this operation before the LINQ expression and use the resulting value (which can be passed to SQL):

int code = Convert.ToInt32(txtCode.Text);
tblfastfood1 w1 = FA.tblfastfood1.FirstOrDefault(x => x.Code == code);