0

System.NullReferenceException when trying to set ItemsSource (ADO.NET EF)

dataGrid.ItemsSource = OdbConnectHelper.entObj.работники.ToList();

I checked App.Config and everything seems to be connected correctly

<add name="qualificationEntities" connectionString="metadata=res://*/db.qualification.csdl|res://*/db.qualification.ssdl|res://*/db.qualification.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.\sqlexpress01;initial catalog=qualification;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /><add name="DefaultConnection" connectionString="Data Source=.\sqlexpress01;Initial Catalog=qualification;Integrated Security=True" providerName="System.Data.SqlClient" />

What could be the problem?

greybeard
  • 2,249
  • 8
  • 30
  • 66
reseeman
  • 3
  • 1
  • I can't fix the error in any way. I have another program with exactly the same pieces of code and it works perfectly, but this one doesn't. – reseeman Jun 28 '21 at 00:26
  • Put a break point on the line of code that throws the exception. When it gets hit, look at each property and see which one is `null`. `entObj` and `работники` are good candidates. – JohnG Jun 28 '21 at 00:32
  • dataGrid.ItemSource is null [image link](https://imgur.com/a/kqMOVF9) (workers = работники) – reseeman Jun 28 '21 at 00:37
  • That is NOT going to throw the null Reference Exception. `dataGrid.ItemsSource` is getting assigned a value as it is on the left-hand side of the “=”. In other words, the code is “assigning a value” to a currently `null` value … which is OK. The exception means you are trying to “use” something that is `null` and my previous comment points to two likely candidates. – JohnG Jun 28 '21 at 00:41

1 Answers1

0

I found a solution. I forgot to write OdbConnectHelper.entObj = new qualificationEntities(); in MainWindow after initialization.

reseeman
  • 3
  • 1