Click here to Skip to main content
13,253,159 members (51,448 online)
Click here to Skip to main content
Add your own
alternative version


8 bookmarked
Posted 21 Jan 2011

Returning a Dataset from Workflow Foundation to ASP.NET

, 21 Jan 2011
Rate this:
Please Sign up or sign in to vote.
Synchronously returning a Dataset from Workflow Foundation to ASP.NET


The code below will help you retrieve a DataSet object from a Workflow Foundation Instance synchronously using properties.


This article is in contrast to all the heavy plumbing work we do in Workflow Foundation to return a Dataset.

Using the Code

Let me begin the code straight away.

  • Create an Empty Solution. Add an ASP.NET project to it.
  • On the Default page, Add a GridView or any other data bound control you like.
  • Add another project, WWF Sequential Library to the solution.
  • Add only a single Code Activity to the designer of WWF.
  • Name the Handler of the CodeActivity "CallMe".
  • Open your Workflow1.designer.cs file.
  • Right after the class name, add a public Property with return type DataSet, name it Result.
  • Open the Workflow.cs file, inside the partial class, find the method we defined CallMe, add the below code to CallMe.
    string conn = ConfigurationManager.ConnectionStrings["Database"].ConnectionString;
    SqlConnection sqlcon = new SqlConnection(conn);
    DataSet ds = new DataSet();
    SqlDataAdapter da = new SqlDataAdapter("select top 10 firstname, 
    				lastname from Employees", sqlcon); 
    Result = ds;

Now, you are done with the Workflow1.cs class. After this, add appropriate ConnectionString Section to the config file to retrieve the connection string.

  • Compile your Workflow Library.
  • Now switch back to your ASP.NET application.
  • Add Global.ascx file to it. Add the below code to the Application_Start:
    // Code that runs on application startup
    System.Workflow.Runtime.WorkflowRuntime workflowRuntime 
        = new System.Workflow.Runtime.WorkflowRuntime();
    System.Workflow.Runtime.Hosting.ManualWorkflowSchedulerService manualService 
        = new System.Workflow.Runtime.Hosting.ManualWorkflowSchedulerService();
    Application["WorkflowRuntime"] = workflowRuntime;
  • Now add the below code to Application_End.
    // Code that runs on application shutdown
    System.Workflow.Runtime.WorkflowRuntime workflowRuntime 
        = Application["WorkflowRuntime"] as System.Workflow.Runtime.WorkflowRuntime;
  • That's it. Open your Default.aspx.cs file.
  • Reference the Workflow sequential Library you created before in the web application along with System.Workflow.Runtime, System.Workflow.Runtime.Hosting,System.Workflow.Activities; System.Threading, System.Data namespaces to your file.
  • Copy and paste the following code to your code behind:
    public partial class _Default : System.Web.UI.Page 
        WorkflowRuntime workflowRuntime;
        AutoResetEvent autoWaitHandler = new AutoResetEvent(false); 
        protected void Page_Load(object sender, EventArgs e)
                workflowRuntime = Application["WorkflowRuntime"] as WorkflowRuntime;
                ManualWorkflowSchedulerService scheduler 
                   = workflowRuntime.GetService(typeof(ManualWorkflowSchedulerService)) 
                      as ManualWorkflowSchedulerService;
                    += new EventHandler<WorkflowCompletedEventArgs>
                WorkflowInstance instance 
                    = workflowRuntime.CreateWorkflow(typeof(ASPNetWorkflow.Workflow1));
                //execute the workflow instance synchronously on our thread.
            catch (Exception ex)
                Response.Write("Error :" + ex.Message);
        void workflowRuntime_WorkflowCompleted
    		(object sender, WorkflowCompletedEventArgs e)
            //Get the result from the workflow.
            if (e.OutputParameters.ContainsKey("Result"))
                DataSet ds = (DataSet)e.OutputParameters["Result"];
                GridView1.DataSource = ds.Tables[0];

That is all. Set your web application as the Start up project and run it.


  • 21st January, 2011: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Parvez B
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Rajesh Kumar Chekuri6-Mar-11 21:03
memberRajesh Kumar Chekuri6-Mar-11 21:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171114.1 | Last Updated 21 Jan 2011
Article Copyright 2011 by Parvez B
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid