- your KeyDown event handler is triggered when the Text property is still null (i.e. the event is registered before the Text property is set)
int.Parse(...) is throwing an exception if the text is null.
Do the following:
private void textBox1_KeyDown(object sender, KeyEventArgs e)
if (textBoxWagenID != null && int.TryParse(textBoxWagenID.Text ?? string.Empty, out a))
m_EntityContext.Foo.Where( p => p.Bar == a).First();
And again: I would not debug this as a KeyDown/KeyUp event! You would trigger an event storm while debugging...
PS: In Visual Studio, turn on in the debuging --> Exceptions dialog the .Net runtime exception to stop on throwing. You will be promptet the exact object that causes the problem. In this case for sure either textBoxWagenID or its Text property.