Using a Receive data connection in InfoPath to execute an SQL Server stored procedure
Learn how to temporarily change the Command property of a Receive data connection in InfoPath to execute a SQL Server stored procedure and insert data into a database table.
You want to execute an SQL Server stored procedure from code within an InfoPath form to be able to insert data into a database table.
Programmatically change the Command property of a Receive data connection to execute a SQL Server stored procedure and insert data into a database table.
You can achieve this functionality as follows:
- In a SQL Server 2005 database, create a table using the following statement:
CREATE TABLE OfficeApplications
[id] INT IDENTITY PRIMARY KEY NOT NULL,
[name] VARCHAR(50) NOT NULL
and a stored procedure using the following statement:
CREATE PROCEDURE AddOfficeApplication
INSERT INTO OfficeApplications VALUES (@name)
- In InfoPath, create a new Blank form template.
- On the Tools menu, click Data Connections, and add a Receive data connection to the OfficeApplications table in the SQL Server 2005 database. Note: The data connection will be named OfficeApplications by default; you will use this name later on in code.
- Add a Text Box control to the form template and name it OfficeApplicationName.
- Add a Button control to the form template and add the following C# code to the Clicked event handler of the Button control:
// Retrieve the name of the Office application to add to the database table
XPathNavigator dom = MainDataSource.CreateNavigator();
string officeApplicationName = dom.SelectSingleNode(
// Retrieve the connection to the database table
AdoQueryConnection connection =
// Save the original statement that is stored in the Command property
string originalCommand = connection.Command;
// Execute the stored procedure
connection.Command = "EXEC AddOfficeApplication '" + officeApplicationName + "'";
// Restore the statement of the Command property to its original value
connection.Command = originalCommand;
or add the following Visual Basic code to the Clicked event handler of the Button control:
' Retrieve the name of the Office application to add to the database table
Dim dom As XPathNavigator = MainDataSource.CreateNavigator()
Dim officeApplicationName As String = dom.SelectSingleNode( _
' Retrieve the connection to the database table
Dim connection As AdoQueryConnection = DataConnections("OfficeApplications")
' Save the original statement that is stored in the Command property
Dim originalCommand As String = connection.Command
' Execute the stored procedure
connection.Command = "EXEC AddOfficeApplication '" & officeApplicationName & "'"
' Restore the statement of the Command property to its original value
connection.Command = originalCommand
- Build the code and test the form.
Now whenever you enter an application name in the text box and then click the button, the stored procedure is executed, and a record is added through the Receive data connection to the table in the SQL Server database.
Related InfoPath Articles:
- Retrieve an InfoPath form from SQL Server and display it in an InfoPath FormControl
- InfoPath does not support the specified database. The database must be a Microsoft SQL Server
- Submit/retrieve an entire InfoPath form to/from SQL Server
- Connect InfoPath 2010 to a SQL Server 2008 table
- 3 Ways to integrate InfoPath with SQL Server
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.