Click here to Skip to main content
Click here to Skip to main content
Go to top

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

Introduction

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

Background

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); 
    da.Fill(ds);
    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();
    
    workflowRuntime.AddService(manualService);
    workflowRuntime.StartRuntime();
    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;
    
    workflowRuntime.StopRuntime();
  • 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)
        {
            try
            {
                workflowRuntime = Application["WorkflowRuntime"] as WorkflowRuntime;
                ManualWorkflowSchedulerService scheduler 
                   = workflowRuntime.GetService(typeof(ManualWorkflowSchedulerService)) 
                      as ManualWorkflowSchedulerService;
                workflowRuntime.WorkflowCompleted 
                    += new EventHandler<WorkflowCompletedEventArgs>
    				(workflowRuntime_WorkflowCompleted);
    
                WorkflowInstance instance 
                    = workflowRuntime.CreateWorkflow(typeof(ASPNetWorkflow.Workflow1));
                instance.Start();
    
                //execute the workflow instance synchronously on our thread.
                scheduler.RunWorkflow(instance.InstanceId);
                autoWaitHandler.WaitOne();
            }
            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];
                GridView1.DataBind();
            }
                
            autoWaitHandler.Set();
        }
    }

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

History

  • 21st January, 2011: Initial post

License

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

Share

About the Author

Parvez B

United States United States
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 PinmemberRajesh Kumar Chekuri6-Mar-11 20:03 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 21 Jan 2011
Article Copyright 2011 by Parvez B
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid