LINQ to SQL in Silverlight Exception

This is a continuation of my previous question: Could not find the implementation of the query template

Now I managed to query my database, I can not get the content on my web page.

I am trying to return the code using the following code:

private void button1_Click(object sender, RoutedEventArgs e) { Service1Client client = new Service1Client(); client.GetPersoonByIDCompleted += new EventHandler<GetPersoonByIDCompletedEventArgs>(client_GetPersoonByIDCompleted); client.GetPersoonByIDAsync("1"); } void client_GetPersoonByIDCompleted(object sender, GetPersoonByIDCompletedEventArgs e) { if (e.Error != null) textBox1.Text = e.Error.ToString(); else label1.Content = e.Result.Voornaam.ToString(); } 

However, when I click the button, I get the following errors:

The reference to the object is not installed in the object instance.

in SilverlightApplication1.MainPage.client_GetPersoonByIDCompleted (Sender Object, GetPersoonByIDCompletedEventArgs e) on SilverlightApplication1.ServiceReference1.Service1Client.OnGetPersoonByIDCompleted (State Object)

It is strange that this works when I do not use LINQ, but plain SQL.

  string sql = "SELECT ID, naam, voornaam, leeftijd FROM tblPersoon WHERE id=@pmID"; Persoon pers = null; string connstr = ConfigurationManager.ConnectionStrings["connDB"].ConnectionString; using (SqlConnection cn = new SqlConnection(connstr)) { SqlCommand com = new SqlCommand(sql, cn); com.Parameters.AddWithValue("pmID", id); cn.Open(); SqlDataReader reader = com.ExecuteReader(); if (reader.Read()) { pers = new Persoon(); pers.ID = reader.GetString(0); pers.Naam = reader.GetString(1); pers.Voornaam = reader.GetString(2); pers.Leeftijd = reader.GetInt32(3); } } return pers; } 

LINQ result:

LINQ

SQL result:

SQL

Thanks for helping me, I appreciate it! Thomas

0
sql linq silverlight wcf
Nov 21 '11 at 18:30
source share
1 answer

You get this particular exception by trying to refer to the "e.Result" property when it is not valid, i.e. when a method call returns an exception instead of a value. Before referencing e.Result, confirm that e.Error == null, and if not, treat yourself to error handling or a messaging code, for example:

 void client_GetPersoonByIDCompleted(object sender, GetPersoonByIDCompletedEventArgs e) { if (e.Error != null) { MessageBox.Show("An error occurred: " + e.Error.ToString()); } else { label1.Content = e.Result; } } 

Or something like that.

+2
Nov 21 '11 at 18:49
source share



All Articles