Retrieve an InfoPath form from SQL Server and display it in an InfoPath FormControl

Applies to: InfoPath 2007

Use code to retrieve the XML of an InfoPath form from SQL Server 2005, convert the InfoPath form into a MemoryStream object, and load this object into an InfoPath FormControl on a Windows Forms application.

ADVERTISEMENTS

Problem

You saved InfoPath forms in an XML field in SQL Server 2005 and would now like to retrieve one of those forms and display it within an InfoPath FormControl in a Windows Forms application.

Solution

Use C# code to retrieve the XML of the InfoPath form from SQL Server 2005, convert the XML of the InfoPath form into a MemoryStream object, and load this object into an InfoPath FormControl on a Windows Forms application.

Discussion

You can accomplish this functionality as follows:

  1. Open Microsoft Visual Studio 2005 and create a new Windows Application project.
  2. Add an InfoPath FormControl to the main Windows Form of the application.
  3. Double-click on the surface of the main Windows Form to add a Load event handler.
  4. Add using statements for System.Data.SqlClient, System.Text, System.IO, and put the following code in the Load event handler of the Windows Form:
    string data = string.Empty;

    using (SqlConnection conn = new SqlConnection("Data Source=Your_Server_Name;Initial Catalog=Your_Database_Name;Integrated Security=True"))
    {
    // Open a connection to the database
    conn.Open();

    // Retrieve the XML data for an InfoPath form that has an ID equal to 1
    SqlCommand cmd = new SqlCommand(@"SELECT Form FROM IPForms WHERE ID = 1", conn);
    SqlDataReader reader = cmd.ExecuteReader();
    while (reader.Read())
    {
    data = reader.GetString(0);
    }

    // Close the connection to the database
    conn.Close();
    }

    // Convert the XML data for the InfoPath form into a byte array
    byte[] bytes = Encoding.UTF8.GetBytes(data);

    using (MemoryStream memStream = new MemoryStream())
    {
    // Load the byte array into the memory stream
    memStream.Write(bytes, 0, bytes.Length);

    // Load the memory stream into the FormControl
    formControl1.Open(memStream);

    // Close the memory stream
    memStream.Close();
    }
  5. Build your project and test the Windows application.

Now whenever you open the Windows Forms application, the InfoPath form with an ID equal to 1 in the SQL Server database will be retrieved from the SQL Server database and loaded into the InfoPath FormControl and displayed on the Windows Forms application.

 


Related InfoPath Articles:

 

Copyright: This article may not be used on web sites (whether personal or otherwise), copied, disseminated, altered, printed, published, broadcasted, or reproduced in any way without an expressed written consent. The techniques demonstrated in this article may be used within any Microsoft InfoPath project. This article is provided without any warranties. Copyright for this article is non-transferrable and remains with the author.

InfoPath 2013 Cookbook: 121 Codeless Recipes for Beginners

InfoPath 2013 Cookbook 2: 121 Codeless Recipes for SharePoint 2013

InfoPath 2010 Cookbook: 101 Codeless Recipes for Beginners

InfoPath 2010 Cookbook 2: 101 Codeless Recipes for SharePoint 2010

InfoPath 2010 Cookbook 3: 101 Code Recipes for C# Developers

InfoPath 2010 Cookbook 4: 101 Code Recipes for VB Developers

InfoPath 2010 Cookbook 5: Integrating InfoPath with Excel and Excel Services