Jaa


Updating a Subscription

Use the Update method of the Subscription class to modify existing subscriptions to a Notification Services application. You access an individual subscription object either by the subscription Id, which is determined by Notification Services when you create the subscription, or, more likely, by iterating through the subscriber's subscriptions using a SubscriptionEnumeration object.

The examples below show how to update a subscription using managed code and using Microsoft Visual Basic Scripting Edition (VBScript) to illustrate COM interop.

Managed Code Example

The following example shows how to obtain a specific subscription from a subscription enumeration, and then update that subscription.

If you are developing a user interface, you can obtain values from the text and list boxes, not hard-coded strings as shown below. To allow a subscriber to select a subscription, you could populate a list box with the subscription identifiers returned from the subscription enumeration. You would then allow the subscriber to select a subscription, modify field values, and then update the subscription.

// Create the NSInstance object.
NSInstance testInstance = new NSInstance("Tutorial");

// Create the NSApplication object.
NSApplication testApplication =
    new NSApplication(testInstance, "Weather");

// Create the Subscription object.
Subscription testSubscription =
    new Subscription(testApplication, "WeatherCity");


//Create a SubscriptionEnumeration object.
// that contains all of the subscriber's subscriptions
// in a specific subscription class
SubscriptionEnumeration testSubscriptionEnumeration = 
    new SubscriptionEnumeration(testApplication, 
        "WeatherCity", "TestUser1");

//Iterate through the subscriptions, finding the subscription
// to match. In a user interface, you would probably list the 
// subscriptions in a list box and then let the user
// alter the data for the selected subscription
foreach (Subscription subscription in testSubscriptionEnumeration)
{
    String city = subscription.GetFieldValue("City").ToString();
    Console.WriteLine(city);
    Console.WriteLine("Subscription ID: {0}", subscription.SubscriptionId);
        if (city.Equals("Shoreline"))
            testSubscription.SubscriptionId = subscription.SubscriptionId;
}

// Set all of the subscription data fields 
testSubscription["DeviceName"] = "Work e-mail";
testSubscription["SubscriberLocale"] = "en-US";
testSubscription["City"] = "Edmonds";

testSubscription.Update();

COM Interop Example

The following VBScript example shows how to use the SetFieldValue method of the Subscription class to set the value of the application-specific subscription fields:

Dim testInstance, testApplication, testSubscription

const instanceName = "Tutorial"
const applicationName = "Weather"
const subscriptionClassName = "WeatherCity"

' Create the NSInstance object.
set testInstance = WScript.CreateObject( _ 
    "Microsoft.SqlServer.NotificationServices.NSInstance")
testInstance.Initialize instanceName

' Create the NSApplication object.
set testApplication = WScript.CreateObject( _ 
    "Microsoft.SqlServer.NotificationServices.NSApplication")
testApplication.Initialize (testInstance), applicationName

' Create the Subscription object.
set testSubscription = WScript.CreateObject( _ 
    "Microsoft.SqlServer.NotificationServices.Subscription")
testSubscription.Initialize (testApplication), subscriptionClassName

' Set the subscription ID so that 
'the correct record is updated.
testSubscription.SubscriptionId = "2"

' Update the subscription record.
testSubscription.SetFieldValue "DeviceName", "Work e-mail"
testSubscription.SetFieldValue "SubscriberLocale", "en-US"
testSubscription.SetFieldValue "City", "Orlando"
testSubscription.Update

wscript.echo "Subscription updated."

See Also

Concepts

Creating a Subscription Object
Adding a Subscription
Deleting a Subscription
Getting Subscription Field Information
Populating a Subscriber Locale List
Populating a Time Zone List

Other Resources

NS<SubscriptionClassName>View

Help and Information

Getting SQL Server 2005 Assistance