<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://www.infopathdev.com:443/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>SQL Database Integration</title><link>https://www.infopathdev.com:443/forums/47.aspx</link><description>For questions concerning issues in the integration of SQL and Microsoft InfoPath.</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP3 (Build: 31118.962)</generator><item><title>Re: Query DB By Stored Procedure OnLoad and Set Field Value</title><link>https://www.infopathdev.com:443/forums/thread/87602.aspx</link><pubDate>Fri, 01 Mar 2013 16:59:59 GMT</pubDate><guid isPermaLink="false">033a2e2d-04e2-4a9d-be01-a4634161eefd:87602</guid><dc:creator>RobSil1976</dc:creator><slash:comments>0</slash:comments><comments>https://www.infopathdev.com:443/forums/thread/87602.aspx</comments><wfw:commentRss>https://www.infopathdev.com:443/forums/commentrss.aspx?SectionID=47&amp;PostID=87602</wfw:commentRss><description>&lt;p&gt;Hilary,&lt;/p&gt;

&lt;p&gt;That was what I needed! :)&amp;nbsp; My code is below in case anyone else tries something similar.&amp;nbsp; Basically I use the DBXL AD Service (as a Data Control in my form) to find the department of the current user, then I query my DB Stored Procedure to get a New Form ID. I &amp;quot;only&amp;quot; do this if the Form is &amp;quot;new&amp;quot;.&lt;/p&gt;


&lt;pre&gt;   public void FormEvents_Loading(object sender, LoadingEventArgs e)
        {
            if (this.New)
            {
                string currentUser = System.Environment.UserName.ToString();

                string department = GetDepartmentOfCurrentUser(currentUser);
                string formNumber = GetFormNumberByUserAndDepartment(department, currentUser);


                XPathNavigator xn = this.MainDataSource.CreateNavigator();
                XPathNavigator xnFormNumber = xn.SelectSingleNode(&amp;quot;/my:myFields/my:Form_Number&amp;quot;, this.NamespaceManager);

                xnFormNumber.SetValue(formNumber);
            }
        }

        private string GetFormNumberByUserAndDepartment(string department, string currentUser)
        {
            //Set the Command for the Query Adapter of the Data Source. Incorporate the 
            //parameter values that you want to use.
            string query = &amp;quot;DECLARE	@return_value int, @FormNumber varchar(50) &amp;quot; +
                &amp;quot;EXECUTE [dbo].[FetchNextFormNumber] @UserName = N&amp;#39;&amp;quot; + currentUser + &amp;quot;&amp;#39;, @Department = N&amp;#39;&amp;quot; + department + &amp;quot;&amp;#39;, @FormNumber = @FormNumber OUTPUT SELECT @FormNumber AS formNumber&amp;quot;;
                        
            AdoQueryConnection myAdoQueryConnection =
                    (AdoQueryConnection)(this.DataConnections[&amp;quot;FormNumberLookup&amp;quot;]);
           
            string oldCmd = myAdoQueryConnection.Command;
            myAdoQueryConnection.Command = query;
            myAdoQueryConnection.Execute();
            myAdoQueryConnection.Command = oldCmd;

            XPathNavigator myNavigator =
              this.DataSources[&amp;quot;FormNumberLookup&amp;quot;].CreateNavigator().SelectSingleNode(&amp;quot;//dfs:myFields/dfs:dataFields/d:row/@formNumber&amp;quot;, this.NamespaceManager);
            
            return myNavigator.Value;
        }

        private string GetDepartmentOfCurrentUser(string currentUser)
        {
            //Set the Command for the Query Adapter of the Data Source. Incorporate the 
            //parameter values that you want to use.
                        
            WebServiceConnection myWSConnection =
                    (WebServiceConnection)(this.DataConnections[&amp;quot;GetMyInfo&amp;quot;]);

            myWSConnection.Execute();
            XPathNavigator myNavigator =
              this.DataSources[&amp;quot;GetMyInfo&amp;quot;].CreateNavigator().SelectSingleNode(&amp;quot;/dfs:myFields/dfs:dataFields/tns:GetMyInfoResponse/tns:GetMyInfoResult/tns:ADProp[tns:Key[text()=&amp;#39;department&amp;#39;]]/tns:Value&amp;quot;, this.NamespaceManager);
              return myNavigator.Value;
        }
    }

&lt;/pre&gt;</description></item><item><title>Re: Query DB By Stored Procedure OnLoad and Set Field Value</title><link>https://www.infopathdev.com:443/forums/thread/87570.aspx</link><pubDate>Thu, 28 Feb 2013 19:51:26 GMT</pubDate><guid isPermaLink="false">033a2e2d-04e2-4a9d-be01-a4634161eefd:87570</guid><dc:creator>Hilary Stoupa</dc:creator><slash:comments>0</slash:comments><comments>https://www.infopathdev.com:443/forums/thread/87570.aspx</comments><wfw:commentRss>https://www.infopathdev.com:443/forums/commentrss.aspx?SectionID=47&amp;PostID=87570</wfw:commentRss><description>Hi - the returned data should be in the MyNumberLookup data source. Try &lt;a href="http://msdn.microsoft.com/en-us/library/microsoft.office.infopath.datasource.createnavigator(v=office.14).aspx"&gt;this&lt;/a&gt; link, see if gets you going in the right direction. :)</description></item><item><title>Re: Query DB By Stored Procedure OnLoad and Set Field Value</title><link>https://www.infopathdev.com:443/forums/thread/87568.aspx</link><pubDate>Thu, 28 Feb 2013 19:19:18 GMT</pubDate><guid isPermaLink="false">033a2e2d-04e2-4a9d-be01-a4634161eefd:87568</guid><dc:creator>RobSil1976</dc:creator><slash:comments>0</slash:comments><comments>https://www.infopathdev.com:443/forums/thread/87568.aspx</comments><wfw:commentRss>https://www.infopathdev.com:443/forums/commentrss.aspx?SectionID=47&amp;PostID=87568</wfw:commentRss><description>&lt;p&gt;As a follow-up... I read this description of the classes:&lt;br /&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/microsoft.office.infopath.dataconnection.execute(v=office.14).aspx"&gt;http://msdn.microsoft.com/en-us/library/microsoft.office.infopath.dataconnection.execute(v=office.14).aspx&lt;/a&gt;&lt;/p&gt;&lt;p&gt;When you &amp;quot;Execute&amp;quot; are the results only bound to a field automatically, in that you can never iterate over them programatically?&amp;nbsp; The complete and total lack of any code after an &amp;quot;Execute&amp;quot; seems to give me that impression... but not sure.&lt;/p&gt;&lt;p&gt;Thx,&lt;/p&gt;&lt;p&gt;Rob&lt;/p&gt;</description></item><item><title>Query DB By Stored Procedure OnLoad and Set Field Value</title><link>https://www.infopathdev.com:443/forums/thread/87538.aspx</link><pubDate>Thu, 28 Feb 2013 15:42:27 GMT</pubDate><guid isPermaLink="false">033a2e2d-04e2-4a9d-be01-a4634161eefd:87538</guid><dc:creator>RobSil1976</dc:creator><slash:comments>0</slash:comments><comments>https://www.infopathdev.com:443/forums/thread/87538.aspx</comments><wfw:commentRss>https://www.infopathdev.com:443/forums/commentrss.aspx?SectionID=47&amp;PostID=87538</wfw:commentRss><description>&lt;p&gt;Greetings,&lt;/p&gt;
&lt;p&gt;I have a stored procedure on my SQL Server 2008 that I have tested as working fine by SQL Queries etc..&amp;nbsp; It does some business logic but end result is it returns a varchar(8) as an OUTPUT parameter.&amp;nbsp; I am attempting to call that SP from Infopath via the OnLoad method of the form (C# format) and then show the result in a simple test field.&amp;nbsp; Just am stuck trying to find out how to read the result from the query and use it?&amp;nbsp; I am familiar with ASP.NET coding so thinking maybe I have a conceptual issue as I am new to Infopath.&amp;nbsp; &lt;/p&gt;

&lt;p&gt;My OnLoad code is:&lt;/p&gt;

&lt;pre&gt;public void FormEvents_Loading(object sender, LoadingEventArgs e)
        {

            XPathNavigator xn = this.MainDataSource.CreateNavigator();
            XPathNavigator xnPONumber = xn.SelectSingleNode(&amp;quot;/my:myFields/my:MyID_Number&amp;quot;, this.NamespaceManager);
            string currentUser = System.Environment.UserName.ToString();

            //Load the appropriate Number from the DB based upon username of current user AND their department.
            //SP has business logic to find right PO Number.
            string department = &amp;quot;TestDept&amp;quot;;

            
            //Set the Command for the Query Adapter of the Data Source. Incorporate the 
            //parameter values that you want to use. 
            string theNumber = &amp;quot;not set&amp;quot;;
            string query = &amp;quot;DECLARE	@return_value int, @RetNumber varchar(50) &amp;quot; +
            &amp;quot;EXECUTE [dbo].[FetchNextNumber] @UserName = N&amp;#39;JohnDoe&amp;#39;, @Department = N&amp;#39;TestDeptHardcoded&amp;#39;, @TheNumber = @RetNumber OUTPUT SELECT @RetNumber AS theNumber&amp;quot;;


            AdoQueryConnection myAdoQueryConnection = (AdoQueryConnection)(this.DataConnections[&amp;quot;MyNumberLookup&amp;quot;]);
            string oldCmd = myAdoQueryConnection.Command;
            myAdoQueryConnection.Command = query;
            myAdoQueryConnection.Execute();
        }
&lt;/pre&gt;&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;After the &amp;quot;Execute();&amp;quot;  how do I read the results out so I can put them into my document?&lt;/pre&gt;</description></item></channel></rss>