Walkthrough: Access Dynamic Data

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

This walkthrough illustrates using Workflow Designer to create a simple speech application demonstrating dynamic data access.

Prerequisites

To complete this walkthrough, you need:

  • Speech Server.
  • Access to the Northwind sample database with access granted to the user NETWORK SERVICE. By default, the Northwind and pubs sample databases are not installed on Microsoft SQL??Server 2005. These databases can be downloaded from the Microsoft Download Center.
  • Sufficient permissions to create and debug ASP.NET Web application projects on the computer where Speech Server is installed.

Connecting to SQL??Server

There are three options for connecting a Speech Server application to SQL??Server:

  • The recommended option is to use Windows Authentication and add NETWORK SERVICE as a user to the database. By default, a Speech Server application connects as NETWORK SERVICE.
  • Alternatively, switch to SQL??Server Authentication and use the connection string to pass in a user name and password. There are security issues with this option. For more information, see Securing Connection Strings.
  • As a third option, impersonate another account, and then carefully undo the impersonation in the finally block.

Accessing Dynamic Data

To create a voice response application

  1. Create a new voice response application.

    For more information, see How to: Create a Voice Response Application.

  2. Add a grammar that recognizes the two semantic values: suppliers and products.

    For more information, see Walkthrough: Use Conversational Grammar Builder to Create a Keyword Grammar.

To add a welcome prompt using a QuestionAnswer

  1. In Solution Explorer, right-click VoiceResponseWorkflow1.cs, and then click View Designer.

  2. On the Microsoft Visual Studio 2005 toolbar, click Toolbox.

  3. In the Toolbox, click Speech Dialog Components to display speech activities.

  4. Drag and drop a QuestionAnswer speech dialog component onto the design surface between answerCallActivity1 and disconnectCallActivity1.

  5. In the Properties window, change the Name property to askFirstQuestion.

  6. On the design surface in askFirstQuestion, click Edit Prompts.

  7. In the QuestionAnswer Property Builder dialog box, type Do you want information on products or suppliers in the Main box, and then click OK.

  8. Click Attach Grammar.

  9. In the QuestionAnswer Property Builder dialog box, select the response node in the tree view pane, and then click OK.

    This grammar should recognize a user selection between two options: products or suppliers.

To add an IfElse activity and a QuestionAnswer

  1. In the Toolbox, click Windows Workflow to display workflow activities.

  2. Drag and drop an IfElse activity onto the design surface between askFirstQuestion and disconnectCallActivity1.

  3. In the Properties window, change the Name property to chooseProductsOrSuppliers.

  4. In the Toolbox, click Speech Dialog Components to display speech activities.

  5. Drag and drop a QuestionAnswer speech dialog component onto the design surface, and then position it on the left branch of chooseProductsOrSuppliers.

  6. In the Properties window, change the Name property to askWhichSupplier.

  7. On the design surface in askWhichSupplier, click Edit Prompts.

  8. In the QuestionAnswer Property Builder dialog box, click Show Event Handler.

  9. In the event handler, add a statement to ask about suppliers.

    this.askWhichSupplier.MainPrompt.SetText("which supplier are you interested in");
    
  10. In Solution Explorer, right-click VoiceResponseWorkflow1.cs, and then click View Designer.

To add a code condition to the IfElse

  1. On the red exclamation at the top of chooseProductsOrSuppliers, click the down arrow, and then click Property 'Condition' is not set.

  2. In the Properties window for IfElseBranchActivity1, set the Condition property to Code Condition in the list box, and then expand the Condition property.

  3. Type ChooseCategory as a name for the condition.

  4. To set the property, click the design surface away from the Properties window.

  5. In the event handler, add a statement to set the Result property to true or false based on the answer to askFirstQuestion.

    e.Result = (this.askFirstQuestion.RecognitionResult.Text == "suppliers");
    

    The left branch is now set to execute if the user answers that they want information about suppliers.

To add a second QuestionAnswer to the IfElse

  1. In Solution Explorer, right-click VoiceResponseWorkflow1.cs, and then click View Designer.

  2. Drag and drop a QuestionAnswer speech dialog component onto the design surface, and then position it on the right branch of chooseProductsOrSuppliers.

  3. In the Properties window, change the Name property to askWhichProduct.

  4. On the design surface in askWhichProduct, click Edit Prompts.

  5. In the QuestionAnswer Property Builder dialog box, click Show Event Handler.

  6. In the event handler, add a statement to clear the prompt, and then add a second statement to ask about products.

    this.askWhichProduct.MainPrompt.ClearContent();
    this.askWhichProduct.MainPrompt.AppendText("which product are you interested in");
    

    askWhichProduct executes if the user answers that they want information about products.

To add code to generate a dynamic grammar

  1. In Solution Explorer, right-click VoiceResponseWorkflow1.cs, and then click View Code.

  2. In the code window, add the following using statements:

    using Microsoft.SpeechServer.Recognition;
    using Microsoft.SpeechServer.Recognition.SrgsGrammar;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Xml;
    
  3. Add the following two procedures at the end of the Workflow1 class.

    private void GetSQLData()
      {
        SqlConnection cnn = new SqlConnection("Initial Catalog=Northwind;Data Source=localhost;Integrated Security=SSPI;");
        SqlCommand cmd = cnn.CreateCommand();
        cmd.CommandType = CommandType.Text;
        //Select the 1st 4 company names in the table
        cmd.CommandText = "SELECT Top 4 CompanyName FROM Suppliers";
        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = cmd;
        DataSet ds = new DataSet();
        da.Fill(ds, "Suppliers");
        DataRow[] adr = ds.Tables["Suppliers"].Select();
    
        foreach (DataRow row in adr)
          {
            _choices.Add(row[0].ToString());
          }
      }
    
    private SrgsDocument CreateGrammar()
      {
        SrgsDocument suppliersGrammar = new SrgsDocument();
        suppliersGrammar.Mode = SrgsGrammarMode.Voice;
    
        SrgsRule rule = new SrgsRule("Items");
        SrgsOneOf oneOf = new SrgsOneOf(_choices.ToArray());
    
        rule.Elements.Add(oneOf);
    
        suppliersGrammar.Rules.Add(rule);
        suppliersGrammar.Root = rule;
    
        //The following 5 statements save the grammar to a .grxml file
        //Change the path to match your project folders.
        string filePath = @"C:\My Documents\Visual Studio 2005\Projects\DynamicDataAccess\DynamicDataAccess\Grammars";
        XmlTextWriter writer = new XmlTextWriter(filePath, System.Text.Encoding.UTF8);
        suppliersGrammar.WriteSrgs(writer);
        writer.Flush();
        writer.Close();
    
        return suppliersGrammar;
      }
    
  4. Add the following three statements to the beginning of the askWhichSupplier_TurnStarting event handler.

      GetSQLData();
      Grammar suppliersGrammar = new Grammar(CreateGrammar());
      this.askWhichSupplier.Grammars.Add(suppliersGrammar);
    
  5. Add the following declaration just above the Workflow1 constructor.

    private List<string> _choices = new List<string>();
    

To add a confirmation statement

  1. In Solution Explorer, right-click VoiceResponseWorkflow1.cs, and then click View Designer.

  2. Drag and drop a Statement speech dialog component onto the design surface between chooseProductsOrSuppliers and disconnectCallActivity1.

  3. In the Properties window, change the Name property to summary.

  4. On the design surface in summary, click Edit Prompts.

  5. In the Statement Property Builder dialog box, click Show Event Handler.

  6. In the event handler, add the following code.

    if (this.askFirstQuestion.RecognitionResult.Text == "suppliers")
      {
        this.summary.MainPrompt.AppendText("you asked for information on " + askWhichSupplier.RecognitionResult.Text);
      }
    else 
      {
        this.summary.MainPrompt.AppendText("you asked for information on " + askWhichProduct.RecognitionResult.Text);
    }
    

To run the application

  1. Answer "suppliers" to the first question.

  2. Answer "Bigfoot Breweries" to the second question.

To extend the application

  • Modify the application so it also responds to a request for product information.

    This might require changes to the workflow, as well as querying the Northwind database for data and building a speech recognition grammar using the query results.

See Also

Other Resources

Speech Application Development Guide