2017-08-18 23 views
0

我想嘗試在我的代碼和我的Visual Studio中使用EDM編碼的程序。我編碼的一切(甚至Save按鈕等),但有一個問題,無論何時我想從我的數據庫(這是在我的計算機在SQL Server 2014年)刪除記錄,我得到一個錯誤,我的程序突然崩潰,我不不知道爲什麼。你能告訴我怎麼解決這個問題?預先感謝您的有用答案。我想在我的Windows窗體中使用EDM代碼時出現錯誤

這裏是我的代碼:

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(); 
    } 

這裏是我的錯誤:

LINQ到實體無法識別方法 '的Int32 ToInt32(System.String)' 的方法,這方法不能轉換成商店表達式

回答

2

這不能轉換爲SQL,所以它不能用於你的LINQ提供者(可能是LINQ to Entities):

Convert.ToInt32(txtCode.Text) 

代替在表達直接使用它,則LINQ表達式之前執行此操作,並使用所得到的值(其可以被傳遞到SQL):

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