linq to sql - Exception handling in WPF data binding -
let's have following code in wpf application:
try { // northwinddatacontext linq datacontext created sql server northwind database data.northwinddatacontext data = new data.northwinddatacontext(connectionstring); var orders = order in data.orders select order; listview.datacontext = orders; } catch (sqlexception ex) { messagebox.show(ex.message); }
if connectionstring incorrect, code doesn't throw sqlexception immediately. instead of this, exception thrown later, when wpf data binding starts enumerate linq query. application crashes unhandled exception. how can handle exception in such situation?
i know possible global exception handling, want more precise way, allows catch specific exception when specific function executed.
this curse of linq , not data binding.
your query has been compiled not run - you binding query not result. change following code:
var orders = order in data.orders select order; var realorders = orders.tolist(); listview.datacontext = realorders ;
basically repository must return results , not raw queries.
Comments
Post a Comment