<?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: Solution: Button Lookup to a customized SQL Server query to return a block of data</title><link>https://www.infopathdev.com:443/forums/thread/67438.aspx</link><pubDate>Fri, 17 Jun 2011 00:35:18 GMT</pubDate><guid isPermaLink="false">033a2e2d-04e2-4a9d-be01-a4634161eefd:67438</guid><dc:creator>Patrick Halstead</dc:creator><slash:comments>0</slash:comments><comments>https://www.infopathdev.com:443/forums/thread/67438.aspx</comments><wfw:commentRss>https://www.infopathdev.com:443/forums/commentrss.aspx?SectionID=47&amp;PostID=67438</wfw:commentRss><description>&lt;p&gt;Thanks for sharing!&lt;/p&gt;</description></item><item><title>Solution: Button Lookup to a customized SQL Server query to return a block of data</title><link>https://www.infopathdev.com:443/forums/thread/67426.aspx</link><pubDate>Thu, 16 Jun 2011 17:35:56 GMT</pubDate><guid isPermaLink="false">033a2e2d-04e2-4a9d-be01-a4634161eefd:67426</guid><dc:creator>rlstrommen</dc:creator><slash:comments>0</slash:comments><comments>https://www.infopathdev.com:443/forums/thread/67426.aspx</comments><wfw:commentRss>https://www.infopathdev.com:443/forums/commentrss.aspx?SectionID=47&amp;PostID=67426</wfw:commentRss><description>&lt;p&gt;&amp;nbsp;I inherited support for an application when the previous develper left the company.&amp;nbsp; I needed to restore the infopath project, because it is stored separately from the .xsn form.&lt;/p&gt;

&lt;p&gt;Based on a lot of feedback from this forum, I was able to put together the solution that works for what our button click event was supposed to do.&amp;nbsp; It performs very well, and minimizes the network traffic to the database.&lt;/p&gt;

&lt;p&gt;Here is the code for the form.&amp;nbsp; I&amp;#39;m not sure how to attach, so I&amp;#39;m sorry it&amp;#39;s so long.&lt;/p&gt;

&lt;p&gt;You&amp;#39;ll also notice a debugging technique that I used to figure out where the code wasn&amp;#39;t working properly.&lt;/p&gt;



&lt;p&gt;&lt;br /&gt;&lt;/p&gt;
&lt;pre&gt;       Public Sub TSN_Lookup_Clicked(ByVal sender As Object, ByVal e As ClickedEventArgs)
            Dim mainNavigator As XPathNavigator
            Dim resultNavigator As XPathNavigator
            Dim searchField As XPathNavigator
            &amp;#39;
            Dim resultSetPath As String = &amp;quot;/dfs:myFields/dfs:dataFields/d:SAGE_SP_COA_TSN&amp;quot;
            Dim rootXMLPath As String = &amp;quot;/my:main/my:Material_Section/my:TSN_Material_Section&amp;quot;
            Dim sageTSNPath As String = &amp;quot;/my:main/my:SAGE/my:SAGE_SP_Columns/my:SAGE_Test_Substance_Number&amp;quot;
            Dim sageTSNFoundPath As String = &amp;quot;/my:main/my:SAGE/my:SAGE_SP_Columns/my:SAGE_TSN_MATERIAL_FOUND&amp;quot;
            Dim sageShowMatlPanelPath As String = &amp;quot;/my:main/my:Show_Hide_Form/my:Show_Material_Panel&amp;quot;
            &amp;#39;
            Dim queryConnection As AdoQueryConnection
            Dim origCommand As String
            Dim whereClause As String
            &amp;#39;
            Dim TSNDataSource As DataSource
            Dim myNamespace As String
            Dim dfsNamespace As String
            &amp;#39;
            Dim DebugLine As Integer
            Dim SAGE_Test_Substance_Number As String
            Dim NewValue As String
            &amp;#39;
            &amp;#39;  Code example taken from
            &amp;#39;    http://www.infopathdev.com/forums/t/8940.aspx?PageIndex=2
            &amp;#39;
            &amp;#39;  Run a query to get the information based on the SAGE_Test_Substance_Number
            &amp;#39;  /my:main/my:SAGE/my:SAGE_SP_Columns/my:SAGE_Test_Substance_Number
            &amp;#39;
            &amp;#39;  SELECT  CABINET             AS TSN_Cabinet
            &amp;#39;         ,DED                 AS TSN_DED
            &amp;#39;         ,F_MAT_GROUP_CLASS   AS TSN_DED_GROUP
            &amp;#39;         ,MAT_NAME            AS TSN_Material
            &amp;#39;         ,MAT_COMP            AS TSN_Material_Comp
            &amp;#39;         ,MAT_FAMILY          AS TSN_Material_Family
            &amp;#39;         ,MAT_ID              AS TSN_Material_ID
            &amp;#39;         ,F_MAT_GROUP         AS TSN_Parent
            &amp;#39;         ,GIFAP_CODE          AS TSN_Material_Type
            &amp;#39;
            &amp;#39; from dbo.SAGE_SP_COA_TSN
            &amp;#39; WHERE TSN_ID = /my:main/my:SAGE/my:SAGE_SP_Columns/my:SAGE_Test_Substance_Number
            &amp;#39;
            Try
                DebugLine = 10
                mainNavigator = Me.CreateNavigator()
                &amp;#39;
                &amp;#39;  get the value of the Test Substance number for the where clause
                &amp;#39;
                DebugLine = 20
                myNamespace = NamespaceManager.LookupNamespace(&amp;quot;my&amp;quot;)
                dfsNamespace = NamespaceManager.LookupNamespace(&amp;quot;dfs&amp;quot;)
                DebugLine = 30
                searchField = mainNavigator.SelectSingleNode(sageTSNPath, NamespaceManager)
                DebugLine = 40
                SAGE_Test_Substance_Number = searchField.Value
                DebugLine = 50
                whereClause = &amp;quot; WHERE (TSN_ID = &amp;#39;&amp;quot; + SAGE_Test_Substance_Number.Replace(&amp;quot;&amp;#39;&amp;quot;, &amp;quot;&amp;#39;&amp;#39;&amp;quot;) + &amp;quot;&amp;#39;)&amp;quot;
                &amp;#39;
                &amp;#39;  get the original command (we&amp;#39;ll need to set it back)
                &amp;#39;
                DebugLine = 60
                TSNDataSource = Me.DataSources(&amp;quot;SAGE_SP_COA_TSN&amp;quot;)
                queryConnection = TSNDataSource.QueryConnection
                DebugLine = 70
                origCommand = queryConnection.Command
                &amp;#39;
                &amp;#39;  modify the query to use the where clause
                &amp;#39;
                DebugLine = 80
                queryConnection.Command = origCommand + whereClause
                &amp;#39;
                &amp;#39;  now we run the query
                &amp;#39;
                DebugLine = 90
                queryConnection.Execute()
                &amp;#39;
                &amp;#39;  Reset the query back to its normal value
                &amp;#39;
                DebugLine = 100
                queryConnection.Command = origCommand
                &amp;#39;
                &amp;#39;  Create the XPathNavigator for the resultset
                &amp;#39;    resultNavigator.ToString should reveal the entire XML structure.
                &amp;#39;
                DebugLine = 110
                resultNavigator = TSNDataSource.CreateNavigator()
                &amp;#39;
                &amp;#39;  Find the row result node
                &amp;#39;
                DebugLine = 120
                &amp;#39;
                &amp;#39;  Fill in the following lookup fields
                &amp;#39;  /my:main/my:Material_Section/my:TSN_Material_Section/my:TSN_Cabinet
                &amp;#39;  /my:main/my:Material_Section/my:TSN_Material_Section/my:TSN_DED
                &amp;#39;  /my:main/my:Material_Section/my:TSN_Material_Section/my:TSN_DED_GROUP
                &amp;#39;  /my:main/my:Material_Section/my:TSN_Material_Section/my:TSN_Material
                &amp;#39;  /my:main/my:Material_Section/my:TSN_Material_Section/my:TSN_Material_Comp
                &amp;#39;  /my:main/my:Material_Section/my:TSN_Material_Section/my:TSN_Material_Family
                &amp;#39;  /my:main/my:Material_Section/my:TSN_Material_Section/my:TSN_Material_ID
                &amp;#39;  /my:main/my:Material_Section/my:TSN_Material_Section/my:TSN_Material_Type (GIFAP Code)
                &amp;#39;  /my:main/my:Material_Section/my:TSN_Material_Section/my:TSN_Parent
                &amp;#39;
                mainNavigator.SelectSingleNode(sageShowMatlPanelPath, NamespaceManager).SetValue(&amp;quot;true&amp;quot;)
                &amp;#39;
                &amp;#39;  Did we get any query results?
                &amp;#39;
                If resultNavigator Is Nothing Then
                    &amp;#39;
                    DebugLine = 1201
                    mainNavigator.SelectSingleNode(sageTSNFoundPath, NamespaceManager).SetValue(&amp;quot;false&amp;quot;)
                    &amp;#39;
                    &amp;#39;  Did we find the record we wanted?
                    &amp;#39;
                ElseIf resultNavigator.SelectSingleNode(resultSetPath &amp;amp; &amp;quot;/@MAT_ID&amp;quot;, NamespaceManager) Is Nothing Then
                    &amp;#39;
                    DebugLine = 1201
                    mainNavigator.SelectSingleNode(sageTSNFoundPath, NamespaceManager).SetValue(&amp;quot;false&amp;quot;)
                    &amp;#39;
                    &amp;#39;  Looks like we found it, so lets return the data
                    &amp;#39;
                Else
                    &amp;#39;
                    DebugLine = 1202
                    mainNavigator.SelectSingleNode(sageTSNFoundPath, NamespaceManager).SetValue(&amp;quot;true&amp;quot;)
                    &amp;#39;
                    DebugLine = 130
                    NewValue = resultNavigator.SelectSingleNode(resultSetPath &amp;amp; &amp;quot;/@CABINET&amp;quot;, NamespaceManager).Value
                    mainNavigator.SelectSingleNode(rootXMLPath &amp;amp; &amp;quot;/my:TSN_Cabinet&amp;quot;, NamespaceManager).SetValue(NewValue)
                    &amp;#39;
                    DebugLine = 131
                    NewValue = resultNavigator.SelectSingleNode(resultSetPath &amp;amp; &amp;quot;/@DED&amp;quot;, NamespaceManager).Value
                    mainNavigator.SelectSingleNode(rootXMLPath &amp;amp; &amp;quot;/my:TSN_DED&amp;quot;, NamespaceManager).SetValue(NewValue)
                    &amp;#39;
                    DebugLine = 132
                    NewValue = resultNavigator.SelectSingleNode(resultSetPath &amp;amp; &amp;quot;/@F_MAT_GROUP_CLASS&amp;quot;, NamespaceManager).Value
                    mainNavigator.SelectSingleNode(rootXMLPath &amp;amp; &amp;quot;/my:TSN_DED_GROUP&amp;quot;, NamespaceManager).SetValue(NewValue)
                    &amp;#39;
                    DebugLine = 133
                    NewValue = resultNavigator.SelectSingleNode(resultSetPath &amp;amp; &amp;quot;/@MAT_NAME&amp;quot;, NamespaceManager).Value
                    mainNavigator.SelectSingleNode(rootXMLPath &amp;amp; &amp;quot;/my:TSN_Material&amp;quot;, NamespaceManager).SetValue(NewValue)
                    &amp;#39;
                    DebugLine = 134
                    NewValue = resultNavigator.SelectSingleNode(resultSetPath &amp;amp; &amp;quot;/@MAT_COMP&amp;quot;, NamespaceManager).Value
                    mainNavigator.SelectSingleNode(rootXMLPath &amp;amp; &amp;quot;/my:TSN_Material_Comp&amp;quot;, NamespaceManager).SetValue(NewValue)
                    &amp;#39;
                    DebugLine = 135
                    NewValue = resultNavigator.SelectSingleNode(resultSetPath &amp;amp; &amp;quot;/@MAT_FAMILY&amp;quot;, NamespaceManager).Value
                    mainNavigator.SelectSingleNode(rootXMLPath &amp;amp; &amp;quot;/my:TSN_Material_Family&amp;quot;, NamespaceManager).SetValue(NewValue)
                    &amp;#39;
                    DebugLine = 136
                    NewValue = resultNavigator.SelectSingleNode(resultSetPath &amp;amp; &amp;quot;/@MAT_ID&amp;quot;, NamespaceManager).Value
                    mainNavigator.SelectSingleNode(rootXMLPath &amp;amp; &amp;quot;/my:TSN_Material_ID&amp;quot;, NamespaceManager).SetValue(NewValue)
                    &amp;#39;
                    DebugLine = 137
                    NewValue = resultNavigator.SelectSingleNode(resultSetPath &amp;amp; &amp;quot;/@F_MAT_GROUP&amp;quot;, NamespaceManager).Value
                    mainNavigator.SelectSingleNode(rootXMLPath &amp;amp; &amp;quot;/my:TSN_Parent&amp;quot;, NamespaceManager).SetValue(NewValue)
                    &amp;#39;
                    DebugLine = 138
                    NewValue = resultNavigator.SelectSingleNode(resultSetPath &amp;amp; &amp;quot;/@GIFAP_CODE&amp;quot;, NamespaceManager).Value
                    DebugLine = 1381
                    mainNavigator.SelectSingleNode(rootXMLPath &amp;amp; &amp;quot;/my:TSN_Material_Type&amp;quot;, NamespaceManager).SetValue(NewValue)
                    &amp;#39;
                End If
                &amp;#39;
            Catch ex As Exception
                If Not resultNavigator Is Nothing Then
                    Throw New Exception(&amp;quot;Line: &amp;quot; &amp;amp; DebugLine &amp;amp; resultNavigator.InnerXml &amp;amp; &amp;quot;, Error = &amp;quot; &amp;amp; ex.ToString)
                Else
                    Throw New Exception(&amp;quot;Line: &amp;quot; &amp;amp; DebugLine &amp;amp; &amp;quot;, Error = &amp;quot; &amp;amp; ex.ToString)
                End If
            End Try
        End Sub
    End Class 
&lt;/pre&gt;</description></item></channel></rss>