Tuesday, August 4, 2009

Inserting Data from Info path to database through object model

Let me tell how to start the Infopath from the scratch so that beginners will can also cope up with us

Go to Start->All Programs->Microsoft Office->Click Microsoft Office Infopath 2007.Now you will be able to see the form as below

Now click the ‘Design a Form Template’ under ‘Design a form’ section.Now you will get the another form as below


Before clicking OK confirm whether ‘Enable browser-compatible features only’ option has checked or not.After clicking OK now you will be able to see a blank form as shown below

Now click the ‘Layout’ option under Tasks section.i have dragged and droped ‘Table with Title’ as per my requirement

Now to get the controls click ‘Design Tasks’ and from ‘Controls’ section utlilize the controls which ever you want.Finally I designed my Info path form as below

Now if we insert any data from the infopath it should get updated in the back end database.Now we need to check some settings before writing the code in VS2005.In your info path form go to Tools Menu->Form Options.In the Form options wizard go to ‘Security and Trust’ option and uncheck that ‘Automatically determine security level(recommended)’.Now check the ‘Full Trust’ option as shown below

Click OK

Now I have gone to the ‘button properties’ by right click of that button.In the Label you can enter the button text that you wish .In my case I have entered ‘Submit’ and I have clicked ‘Edit from code’ and it has displayed the following message

Click OK to save your form. After saving again go to ‘Edit from code’ of the Submit button control. Now it opens in Visual Studio mode first thing is we need to add the using System.Data.SqlClient;

Now in the button click event write the code as

XmlNamespaceManager ns = this.NamespaceManager;

XPathNavigator xnDoc = this.MainDataSource.CreateNavigator();

XPathNavigator xnEmpId = xnDoc.SelectSingleNode("/my:myFields/my:field1", ns);

XPathNavigator xnEmpName = xnDoc.SelectSingleNode("/my:myFields/my:field2", ns);

XPathNavigator xnDeptName = xnDoc.SelectSingleNode("/my:myFields/my:field3", ns);

//connection string

string strConnection = "Data Source=Servername\\SQLEXPRESS;Initial Catalog=Sudheer;Integrated Security=SSPI";

SqlConnection conn = new SqlConnection(strConnection);

conn.Open();

SqlCommand cmd = new SqlCommand("insert into Test values('" + xnEmpId + "','" + xnEmpName + "','" + xnDeptName + "')", conn);

cmd.ExecuteNonQuery();

conn.Close();

I hope no need to explain regarding XmlNamespaceManager,XPathNavigator as you would be aware about XML concepts.Now as we need to capture the data of the fields through SelectSingleNode and there we need give the exact XPath of the field.For that what we have to do is

Go to your Info Path in the Design Tasks of right pane click Data Source there you will be able to see the fields.Right click on the field and click Copy XPath and paste in the SelectSingleNode as follows

XPathNavigator xnEmpId = xnDoc.SelectSingleNode("/my:myFields/my:field1", ns);

Finally we need to write the code to connect to the database in the Data Source.We need to write the sql quey to insert the data and finally close the sql connection.Now Run debug your application and check it out in the database.Now you can check it in your database which ever you have inserted in the Info path.


No comments:

Post a Comment