Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

This resource has not currently been approved, and is not currently linked to from our directory of resources. It is being displayed here for preview by the author and moderators only.
Rated
Read 3,938 times

Related Categories

Populating InfoPath fields with SQL data (using managed code) - Page1

S.S. Ahmed

Page1

Target Audience: Advanced

Article will be helpful to those who have worked with InfoPath toolkit and have written basic level managed code using the toolkit.

I know it's easy to populate InfoPath form fields with SQL data using data connections but there are certain scenarios where you may want to populate fields with SQL data using custom code. For example, consider a scenario where you form has different sections, and each section is filled with data from a different database. One way is to create multiple data connections in your form. The other way is to write custom code. This is not the only example, there can be different situations where writing your own code could prove useful. Another situation is when you want to validate data entered by a user. Simply, open a connection to your database and check the field's value against data in your database. You must have InfoPath toolkit installed on your computer.

1. Create an InfoPath form and add a field and a button.
2. Field name is  "field1" which is the default name for a newly added field. You may want to change it to a name of your liking, for example, First Name, Last Name, Address, etc.
3. Double click the button (default name for the button is Ctrl_1) and select "Edit Form Code..." in the dialog box that opens.
4. Add following code in the click event of the button:

SqlConnection MyConnection = new SqlConnection("server=sqlserver;database=yourdatabase;UID=;PWD=;");
MyConnection.Open();
SqlCommand Cmd = new SqlCommand();
Cmd.Connection = MyConnection;
Cmd.CommandType = CommandType.Text;
Cmd.CommandText = "select * from tblUser";
SqlDataAdapter DA = new SqlDataAdapter(Cmd);
DataSet DS = new DataSet();
DA.Fill(DS);

thisXDocument.DOM.selectSingleNode("/my:myFields/my:field1").text = DS.Tables[0].Rows[1][1].ToString();

if(DS.Tables[0].Rows[1][1].ToString() == "John Doe")
{
thisXDocument.UI.Alert("User name is John Doe.");
}

Code explanation:

Open a connection to the database using a connection string. Connection string contains your sql server, database name and userid and password to access the database. Open the connection before making any transaction. Add your sql query in the command object:

Cmd.CommandText = "select * from tblUser";

Following line will add data from SQL DB to your form field:

thisXDocument.DOM.selectSingleNode("/my:myFields/my:field1").text = DS.Tables[0].Rows[1][1].ToString();

"field1" is your field's name. In the line above, we are populating this field with Row 1, Column 1 of the table.

if(DS.Tables[0].Rows[1][1].ToString() == "John Doe")
{
    thisXDocument.UI.Alert("User name is John Doe.");
}

If DB field is equal to "John Doe" then display a message to the user.

You can also do the opposite, instead of populating a field with DB data, get a value from the form field and find a record against this value in the DB. You just need to pass the form field value in the sql query:

Cmd.CommandText = "select * from tblUser where username='" + thisXDocument.DOM.selectSingleNode("/my:myFields/my:field1").text + "'";

Don't forget to add following namespaces in your code page:

using System;
using System.Data;
using System.Data.SqlClient;

Add following code to the project class:

public class InfoPathDBProject
{
    private XDocument thisXDocument;
    private Application thisApplication;

    public void _Startup(Application app, XDocument doc)
    {
        thisXDocument = doc;
        thisApplication = app;
    }

    //Application code

}

 

S.S. Ahmed is a senior IT Professional and works for a web and software development firm. Ahmed is a Microsoft Office SharePoint Server MVP. Ahmed specializes in creating database driven dynamic web sites. He has been working with SharePoint for the last 3-4 years. He develops customized SharePoint solutions. Ahmed likes to hop into other tools as well. Ahmed has used Project Server, InfoPath and BizTalk. Ahmed enjoys travelling and has been to many parts of the world. Web: www.walisystems.com Blog: www.sharepointblogs.com/ssa

Comments