Monday, September 17, 2012

datagrid SelectionChanged causes invalid cross-thread access

datagrid SelectionChanged causes invalid cross-thread access

Hi, i'm newbie for Silverlight and web development, but anyway trying to learn a bit... I have a client with two datagrid, and when a user selects a row in another, this should populate another grid. But all i get is invalid cross-thread access exeption.

Code is quite a copy from an example from a book, and i can't figure out what's wrong. Help anyone?

Code (this terrible lining is not mine, it comes from the house...):

public partial class MainPage : UserControl  {

//Dynamic data context creation 

DataServiceCollection<HenkiloRooli> henkiloroolit = new DataServiceCollection<HenkiloRooli>(); DataServiceCollection<Rooli> roolit = new DataServiceCollection<Rooli>();

//Entity context OsaamisrekisteriEntities context = new OsaamisrekisteriEntities(new Uri("http://localhost:50608/DBService1.svc", UriKind.RelativeOrAbsolute));

//Error handler class ErrorHandler err = new ErrorHandler();

public MainPage()

{           

 InitializeComponent();     

Application.Current.MainWindow.WindowState = WindowState.Maximized;

 //Appropriate merge option set for context

context.MergeOption = MergeOption.PreserveChanges;

getPersons();

 }

private void getPersons()        

{     

try            

 {   //Linq to ado.net dataservice

  var operator_data = (from c in context.Henkilo.Expand("HenkiloRooli") select c);               

  //Async call to load data   henkilot.LoadAsync(operator_data); 

  //Async load completed

  henkilot.LoadCompleted +=new EventHandler<LoadCompletedEventArgs>(henkilot_LoadCompleted);            

 }     

catch (Exception ex)            

 {                

err.ShowMessage(ex.Message);            

 }        

 }

 void henkilot_LoadCompleted(object sender, LoadCompletedEventArgs e)        

 {     

if (e.Error == null)            

 {               

  dgPersonnel.ItemsSource = henkilot;           

  }      else            

 {   string eInnerException = e.Error.InnerException.ToString();                

 err.ShowMessage(eInnerException);            

}        

}

 private void dgPersonnel_SelectionChanged(object sender, SelectionChangedEventArgs e)        

 {   

Henkilo currentHenkilo = dgPersonnel.SelectedItem as Henkilo;  

 var q = from hr in context.HenkiloRooli   where hr.HenkiloId == currentHenkilo.HenkiloId select hr;

  var dsq = (DataServiceQuery<HenkiloRooli>)q;              

dsq.BeginExecute(result =>            

 {                  

dgRole.ItemsSource =                     

 dsq.EndExecute(result).FirstOrDefault().RooliId.ToString();            

 }, null);        

 }

Line dsq.EndExecute(result).FirstOrDefault().RooliId.ToString(); triggers exception.

I guess i do somehow in a wrong way, but i cant find out how else this should be done.

Cheers!

 

Answers & Comments...

Answer: 1

http://forums.silverlight.net/t/143981.aspx/1

http://forums.silverlight.net/t/163643.aspx/1

http://forums.silverlight.net/t/18215.aspx/1





No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog