0

This is a continution of my previous question: Could not find an implementation of the query pattern

Now I managed to query my database I can't seem to get the content on my webpage.

I try 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 press the button I get the following errors:

Object reference not set to an instance of an object.

at SilverlightApplication1.MainPage.client_GetPersoonByIDCompleted(Object sender, GetPersoonByIDCompletedEventArgs e) at SilverlightApplication1.ServiceReference1.Service1Client.OnGetPersoonByIDCompleted(Object state)

The weird thing is it works when I do not use LINQ, but normal 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

Thank you for helping me, I grealy appreciate it! Thomas

Community
  • 1
  • 1
Schoof
  • 2,715
  • 5
  • 29
  • 41

1 Answers1

2

You get that particular exception by trying to reference the "e.Result" property when it's not valid, i.e., when the method call returned an exception instead of a value. Before you reference e.Result, confirm that e.Error == null, and if it doesn't, indulge yourself in some error handling or messaging code, e.g.:

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 of that sort.

Ken Smith
  • 20,305
  • 15
  • 100
  • 147
  • Thanks, I updated my original post with the new error message. – Schoof Nov 21 '11 at 18:53
  • The first thing I'd check is to see whether there's a mismatch between the web service and your client-side proxies. Update your service references. – Ken Smith Nov 21 '11 at 19:04
  • I don't think there is a mismatch, because it works when I switch from LINQ to usual SQL. (I updated my start post) – Schoof Nov 21 '11 at 19:18
  • I'd want confirm, then, that the same data is actually being returned. Focus your troubleshooting steps on the differences between those two server-side methods. You'll presumably find that there's a difference of some sort between the two. – Ken Smith Nov 21 '11 at 19:25
  • There is a difference, but I don't think it should matter? http://localhostr.com/file/BsIRH0B/1.jpg and http://localhostr.com/file/7p9jFLj/2.jpg – Schoof Nov 21 '11 at 19:58
  • No, it absolutely matters. The two different methods are returning an instance of two completely different classes; and the two different classes are not interchangeable. SilverlightApplication1.Web.tblPersoon is *not* the same thing as SilverlightApplication1.Web.Person. They don't even have the same property names, but even if they did, they're still two different classes. – Ken Smith Nov 21 '11 at 22:00
  • If you still want to use LINQ, you'll need to create your own instance of the Person class, and manually set all the properties from the instance of the tblPerson class returned by the LINQ query. – Ken Smith Nov 21 '11 at 22:01
  • The Person class is the exact same class, just not auto generated. (I created the Person class myself) Why would I not be able to display data of the tblPersoon class? It doesn't matter what content I return does it? – Schoof Nov 21 '11 at 22:05
  • That's the whole point. They're two different classes. You can certainly use tblPerson as your data transfer object (DTO), but you'll need to change the return type of the method, which means that you'll need to update your service references on the client. – Ken Smith Nov 22 '11 at 01:14
  • OMG, you are correct. I completely forgot to update my serverice reference on the client, I can't believe I have been so stuppid. I apologise greatly and thank you for helping me! – Schoof Nov 22 '11 at 06:46
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/5243/discussion-between-ken-smith-and-thomas-schoof) – Ken Smith Nov 22 '11 at 16:56