且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

实体框架错误:输入字符串格式不正确

更新时间:2023-11-08 12:31:46

使用 Int32.TryParse Method(System) [ ^ ]



 int id; 
if(int.TryParse(IdRowtextBox.Text.Trim(),out id))
{
var get = dc.table.Where(b => b.IdRow == id ).SingleOrDefault();
nametextBox.Text = get.name.ToString();
}


这是模型实体中的问题,我添加了错误的关联。


I'm trying to set row name in textbox from database table using Entity Model But I'm getting this message error "

"Input string was not in a correct format"

" in the query code :

var get = dc.table.Where(b => b.IdRow ==  id).SingleOrDefault();




I'm using "IdRow = key research"


it's not the first time I use this code !
Thank you for you help.

What I have tried:

private void IdRowtextBox_KeyUp(object sender, KeyEventArgs e)
       {
           if (e.KeyCode == Keys.Enter)
           {

                Entities dc = new Entities();
                int id = Convert.ToInt32(IdRowtextBox.Text) ;

                var get = dc.table.Where(b => b.IdRow ==  id).SingleOrDefault();
                nametextBox.Text = get.name.ToString ();

use Int32.TryParse Method (System)[^]

int id;
           if (int.TryParse(IdRowtextBox.Text.Trim(), out id))
           {
               var get = dc.table.Where(b => b.IdRow == id).SingleOrDefault();
               nametextBox.Text = get.name.ToString();
           }


It was a problem in the model entity  , I added a wrong association .