开发者

How to catch return value from service method?

I am completely new to silverlight and WCF. Still开发者_开发技巧 reading online stuff and trying to write some code to get going. :)

My question is, I want to insert data into database and my insert method returns a bool. How would I catch the return value in silverlight in the button click event and display confirmation message to the user.

My Service code is:

[OperationContract]
    public bool insertData(string Name, string Address, string cType, string postcode, string city, string phone, string email)
    {
        bussAppDataContext dc = new bussAppDataContext();
        TestTable tt = new TestTable();


        tt.CompanyName = Name;
        tt.Address = Address;
        tt.CompanyType = cType;
        tt.Postcode = postcode;
        tt.City = city;
        tt.Telephone = phone;
        tt.Email = email;

        dc.TestTables.InsertOnSubmit(tt);
        dc.SubmitChanges();
        return true;

    }

And the silverlight client code is:

private void btnSend_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        FirstServiceReference.FirstServiceClient webServc = new FirstServiceReference.FirstServiceClient();

webServc.insertDataAsync(txtCName.Text.Trim(), txtAddress.Text.Trim(), cmbCType.SelectedValue.ToString(), txtPostcode.Text.Trim(), txtCity.Text.Trim(), txtPhone.Text.Trim(), txtEmail.Text.Trim());

}


all webservice calls are async in silverlight so you need to add a handler to the insertDataCompleted event. This event is called when the operation is done. Something like this:

webServc.insertDataCompleted += MyHandler;
webServc.insertDataAsync(txtCName.Text.Trim(), txtAddress.Text.Trim(), cmbCType.SelectedValue.ToString(), txtPostcode.Text.Trim(), txtCity.Text.Trim(), txtPhone.Text.Trim(), txtEmail.Text.Trim());

}

private void MyHandler(object sender, MyEventArgs args) {}

The args have the boolean as result. Have a look here Calling web services with Silverlight Tim Heuer.

Hope this helps.

BR,

TJ

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜