<?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: Querying Secondary Data Connection into a Repeating Table using SQL Server Stored Proc.</title><link>https://www.infopathdev.com:443/forums/thread/68297.aspx</link><pubDate>Mon, 11 Jul 2011 14:01:39 GMT</pubDate><guid isPermaLink="false">033a2e2d-04e2-4a9d-be01-a4634161eefd:68297</guid><dc:creator>rlstrommen</dc:creator><slash:comments>0</slash:comments><comments>https://www.infopathdev.com:443/forums/thread/68297.aspx</comments><wfw:commentRss>https://www.infopathdev.com:443/forums/commentrss.aspx?SectionID=47&amp;PostID=68297</wfw:commentRss><description>&lt;p&gt;OK, here is how we solved this problem.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;Stored Procedure&lt;/p&gt;
&lt;p&gt;The stored procedure takes in a search filter parameter, along with the first and last page.&amp;nbsp; The result searches the synonym table and returns only those rows that are requested.&lt;/p&gt;&lt;pre&gt;CREATE FUNCTION [dbo].[SP_COA_LkpMaterialSynonym]
(	
	@MsynFilter VARCHAR(2000)
   ,@FirstRow INTEGER
   ,@LastRow INTEGER
)
RETURNS TABLE 
AS
RETURN 
(
	WITH SearchResult AS
	(
		 SELECT TOP 100 PERCENT 
				ROW_NUMBER() OVER (ORDER BY results.SR_PREFERRED_NAME ASC) AS RowNumber
			   ,results.SR_MAT_ID          AS MAT_ID
			   ,results.SR_PREFERRED_NAME  AS PREFERRED_NAME
			   ,results.SR_IUPAC_NAME      AS IUPAC_NAME
		 FROM (
			  SELECT DISTINCT
					 MAT_ID          AS SR_MAT_ID
					,PREFERRED_NAME  AS SR_PREFERRED_NAME
					,IUPAC_NAME      AS SR_IUPAC_NAME
			  FROM   dbo.SAGE_SP_COA_MATERIAL_SEARCH_RESULT
			  WHERE  UPPER(SYNONYM_NAME) LIKE UPPER(@MsynFilter)
			  ) results
	)
	SELECT	 TOP 100 PERCENT
	         MAT_ID         AS MAT_ID
			,PREFERRED_NAME AS PREFERRED_NAME
			,IUPAC_NAME     AS IUPAC_NAME
		    ,(SELECT COUNT(*)
		  			 FROM SearchResult 
			 )              AS TotalCount
	FROM     SearchResult
	WHERE	 RowNumber BETWEEN @FirstRow AND @LastRow
	ORDER BY RowNumber
)
&lt;/pre&gt;
&lt;p&gt;Data Connector Query&lt;/p&gt;
&lt;p&gt;The data connector is built using a base command.&amp;nbsp; I&amp;#39;m going to change the whole thing anyway, so the only thing I need to make sure of if that I have the same columns in the query that I&amp;#39;m going to have when I tailor it.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;SELECT MAT_ID,PREFERRED_NAME,IUPAC_NAME,&amp;quot;TotalCount&amp;quot; FROM dbo.SP_COA_LkpMaterialSynonym(&amp;#39;%&amp;#39;, 1, 20) as SAGE_SP_COA_MATERIAL_SEARCH_RESULT ORDER BY PREFERRED_NAME&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;VB Code&lt;/p&gt;
&lt;p&gt;The VB Code is fixed as indicated below:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;pre&gt;        Public Sub Do_Material_Search(ByVal FirstRow As Integer, ByVal LastRow As Integer)
            &amp;#39;
            &amp;#39; Update /my:main/my:Material_Section/my:Material_Search_Group/my:SearchResultCount
            &amp;#39; with the number of hits on the field
            &amp;#39;
            &amp;#39;  Repeating Table declarations
            &amp;#39;
            Dim rootXMLPath As String = &amp;quot;/my:main/my:Material_Section/my:Material_Search_Group&amp;quot;
            Dim mainNavigator As XPathNavigator
            Dim msynRows As XPathNodeIterator
            Dim msynRow As XPathNavigator
            Dim errorMessageXMLPath As String = &amp;quot;/my:main/my:Material_Section/my:Material_Search_Group/my:UseSelectedMaterialErrorMsg&amp;quot;
            &amp;#39;
            &amp;#39;  Search Result declarations
            &amp;#39;
            Dim resultNavigator As XPathNavigator
            Dim msynResults As XPathNodeIterator
            Dim I As Integer
            Dim writer As XmlWriter
            Dim MatID As String
            Dim PreferredName As String
            Dim IUPACName As String
            Dim recordCountSet As Boolean
            Dim hitCount As Boolean
            &amp;#39;
            Dim resultSetPath As String = &amp;quot;/dfs:myFields/dfs:dataFields/d:MATERIAL_SYNONYM&amp;quot;
            Dim sageSearchMaterialPath As String = &amp;quot;/my:main/my:Material_Section/my:Material_Search_Group/my:Search_Material&amp;quot;
            &amp;#39;
            Dim queryConnection As AdoQueryConnection
            Dim origCommand As String
            Dim sqlClause As String
            &amp;#39;
            Dim MsynDataSource As DataSource
            Dim myNamespace As String
            Dim dfsNamespace As String
            &amp;#39;
            Dim SearchMatlSynFilter As String
            Dim NewValue As String
            &amp;#39;
            &amp;#39;  Run a query to get the information based on the Search_Material
            &amp;#39;  /my:main/my:Material_Section/my:Material_Search_Group/my:Search_Material
            &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;)
                &amp;#39;
                &amp;#39;  This is a new search, reset the error message.
                &amp;#39;
                mainNavigator.SelectSingleNode(errorMessageXMLPath, NamespaceManager).SetValue(&amp;quot;&amp;quot;)
                &amp;#39;
                DebugLine = 30
                SearchMatlSynFilter = mainNavigator.SelectSingleNode(sageSearchMaterialPath, NamespaceManager).Value
                &amp;#39;
                &amp;#39;  Save this search in the Prev filter for the results display
                &amp;#39;
                mainNavigator.SelectSingleNode(rootXMLPath &amp;amp; &amp;quot;/my:PrevSearchFilter&amp;quot;, NamespaceManager).SetValue(SearchMatlSynFilter)
                DebugLine = 40
                &amp;#39;
                &amp;#39;      SELECT MAT_ID          AS MAT_ID
                &amp;#39;            ,PREFERRED_NAME  AS PREFERRED_NAME
                &amp;#39;            ,IUPAC_NAME      AS IUPAC_NAME
                &amp;#39;            ,TotalCount      AS TotalCount
                &amp;#39;      FROM dbo.SP_COA_LkpMaterialSynonym(&amp;#39;ch%&amp;#39;
                &amp;#39;            ,1
                &amp;#39;            ,20)
                &amp;#39;
                sqlClause = &amp;quot;&amp;quot;
                sqlClause = sqlClause &amp;amp; &amp;quot;SELECT MAT_ID          AS MAT_ID&amp;quot; &amp;amp; vbCrLf
                sqlClause = sqlClause &amp;amp; &amp;quot;      ,PREFERRED_NAME  AS PREFERRED_NAME&amp;quot; &amp;amp; vbCrLf
                sqlClause = sqlClause &amp;amp; &amp;quot;      ,IUPAC_NAME      AS IUPAC_NAME&amp;quot; &amp;amp; vbCrLf
                sqlClause = sqlClause &amp;amp; &amp;quot;      ,TotalCount      AS TotalCount&amp;quot; &amp;amp; vbCrLf
                sqlClause = sqlClause &amp;amp; &amp;quot;FROM dbo.SP_COA_LkpMaterialSynonym(&amp;quot; &amp;amp; vbCrLf
                sqlClause = sqlClause &amp;amp; &amp;quot;       &amp;#39;&amp;quot; &amp;amp; SearchMatlSynFilter.Replace(&amp;quot;&amp;#39;&amp;quot;, &amp;quot;&amp;#39;&amp;#39;&amp;quot;) &amp;amp; &amp;quot;&amp;#39;&amp;quot; &amp;amp; vbCrLf
                sqlClause = sqlClause &amp;amp; &amp;quot;      ,&amp;quot; &amp;amp; FirstRow.ToString &amp;amp; vbCrLf
                sqlClause = sqlClause &amp;amp; &amp;quot;      ,&amp;quot; &amp;amp; LastRow.ToString &amp;amp; &amp;quot;)&amp;quot;
                &amp;#39;
                DebugLine = 60
                MsynDataSource = Me.DataSources(&amp;quot;SAGE_SP_COA_MATERIAL_SEARCH_RESULT&amp;quot;)
                queryConnection = MsynDataSource.QueryConnection
                DebugLine = 70
                origCommand = queryConnection.Command
                &amp;#39;
                &amp;#39;  modify the query to use the custom clause
                &amp;#39;
                DebugLine = 80
                queryConnection.Command = sqlClause
                &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 = MsynDataSource.CreateNavigator()
                &amp;#39;
                hitCount = 0
                recordCountSet = False
                &amp;#39;
                If resultNavigator IsNot Nothing Then
                    &amp;#39;
                    DebugLine = 1300
                    &amp;#39;
                    &amp;#39;  Get the resultset rows
                    &amp;#39;
                    msynResults = resultNavigator.Select(resultSetPath, NamespaceManager)
                    &amp;#39;  
                    &amp;#39; Here is the example code to move data from a secondary data source into a repeating table.
                    &amp;#39; Listing 17.5. Fully Changed Method 
                    &amp;#39;
                    &amp;#39;  Repeating Group: /my:main/my:Material_Section/my:Material_Search_Group/my:SynonymSearchResults/my:MsynRow
                    &amp;#39;  First task is to remove results from any previous search.
                    &amp;#39;  For whatever reason, the example says to delete from the bottom up
                    &amp;#39;  Note: Be sure to check if the xml array is zero relative or not
                    &amp;#39;  
                    DebugLine = 1400
                    msynRows = mainNavigator.Select(rootXMLPath &amp;amp; &amp;quot;/my:SynonymSearchResults/my:MsynRow&amp;quot;, NamespaceManager)
                    If (msynRows.Count &amp;gt; 0) Then
                        For I = msynRows.Count To 1 Step -1
                            DebugLine = 1415 + I
                            msynRow = mainNavigator.SelectSingleNode(rootXMLPath &amp;amp; &amp;quot;/my:SynonymSearchResults/my:MsynRow[&amp;quot; &amp;amp; I &amp;amp; &amp;quot;]&amp;quot;, NamespaceManager)
                            msynRow.DeleteSelf()
                        Next
                    End If
                    &amp;#39;
                    &amp;#39;  Populate return data
                    &amp;#39;
                    DebugLine = 1500
                    For I = 1 To msynResults.Count
                        DebugLine = 1510

                        If Not recordCountSet Then
                            DebugLine = 1520
                            NewValue = resultNavigator.SelectSingleNode(resultSetPath &amp;amp; &amp;quot;[&amp;quot; &amp;amp; I &amp;amp; &amp;quot;]/@TotalCount&amp;quot;, NamespaceManager).Value
                            &amp;#39;
                            &amp;#39;  If the LastRow is greater than the total number of rows, reset it
                            &amp;#39;  (displaying 1 to 20 of 15 records becomes displaying 1 to 15 of 15.
                            &amp;#39;
                            If LastRow &amp;gt; Integer.Parse(NewValue) Then
                                LastRow = Integer.Parse(NewValue)
                            End If
                            DebugLine = 1530
                            mainNavigator.SelectSingleNode(rootXMLPath &amp;amp; &amp;quot;/my:SearchResultCount&amp;quot;, NamespaceManager).SetValue(NewValue)
                            recordCountSet = True
                        End If
                        &amp;#39;
                        DebugLine = 1540
                        MatID = resultNavigator.SelectSingleNode(resultSetPath &amp;amp; &amp;quot;[&amp;quot; &amp;amp; I &amp;amp; &amp;quot;]/@MAT_ID&amp;quot;, NamespaceManager).Value
                        DebugLine = 1550
                        PreferredName = resultNavigator.SelectSingleNode(resultSetPath &amp;amp; &amp;quot;[&amp;quot; &amp;amp; I &amp;amp; &amp;quot;]/@PREFERRED_NAME&amp;quot;, NamespaceManager).Value
                        DebugLine = 1560
                        IUPACName = resultNavigator.SelectSingleNode(resultSetPath &amp;amp; &amp;quot;[&amp;quot; &amp;amp; I &amp;amp; &amp;quot;]/@IUPAC_NAME&amp;quot;, NamespaceManager).Value
                        &amp;#39;
                        &amp;#39;  Populate the repeating table 
                        &amp;#39;
                        DebugLine = 157000 + I
                        writer = mainNavigator.SelectSingleNode(rootXMLPath &amp;amp; &amp;quot;/my:SynonymSearchResults&amp;quot;, NamespaceManager).AppendChild()
                        DebugLine = 157100 + I
                        writer.WriteStartElement(&amp;quot;MsynRow&amp;quot;, myNamespace)
                        DebugLine = 157200 + I
                        writer.WriteElementString(&amp;quot;SelectMe&amp;quot;, myNamespace, &amp;quot;false&amp;quot;)
                        DebugLine = 157300 + I
                        writer.WriteElementString(&amp;quot;PreferredName&amp;quot;, myNamespace, PreferredName)
                        DebugLine = 157400 + I
                        writer.WriteElementString(&amp;quot;IUPACName&amp;quot;, myNamespace, IUPACName)
                        DebugLine = 157500 + I
                        writer.WriteElementString(&amp;quot;MatId&amp;quot;, myNamespace, MatID)
                        DebugLine = 157600 + I
                        writer.WriteEndElement()
                        DebugLine = 157700 + I
                        writer.Close()
                        &amp;#39;
                    Next
                    &amp;#39;
                    &amp;#39;  Now all the results are listed
                    &amp;#39;
                End If
                &amp;#39;
                DebugLine = 1600
                If Not recordCountSet Then
                    NewValue = &amp;quot;0&amp;quot;
                    mainNavigator.SelectSingleNode(rootXMLPath &amp;amp; &amp;quot;/my:SearchResultCount&amp;quot;, NamespaceManager).SetValue(NewValue)
                    FirstRow = 0
                    LastRow = 0
                End If
                &amp;#39;
                mainNavigator.SelectSingleNode(rootXMLPath &amp;amp; &amp;quot;/my:FirstRow&amp;quot;, NamespaceManager).SetValue(FirstRow.ToString)
                mainNavigator.SelectSingleNode(rootXMLPath &amp;amp; &amp;quot;/my:LastRow&amp;quot;, NamespaceManager).SetValue(LastRow.ToString)
                &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;, MatId = &amp;quot; &amp;amp; MatID &amp;amp; &amp;quot;, PrefName = &amp;quot; &amp;amp; PreferredName &amp;amp; &amp;quot;, Error = &amp;quot; &amp;amp; ex.ToString)
                End If
            End Try
        End Sub
&lt;/pre&gt;</description></item><item><title>Re: Querying Secondary Data Connection into a Repeating Table using SQL Server Stored Proc.</title><link>https://www.infopathdev.com:443/forums/thread/68159.aspx</link><pubDate>Thu, 07 Jul 2011 00:17:54 GMT</pubDate><guid isPermaLink="false">033a2e2d-04e2-4a9d-be01-a4634161eefd:68159</guid><dc:creator>Hilary Stoupa</dc:creator><slash:comments>0</slash:comments><comments>https://www.infopathdev.com:443/forums/thread/68159.aspx</comments><wfw:commentRss>https://www.infopathdev.com:443/forums/commentrss.aspx?SectionID=47&amp;PostID=68159</wfw:commentRss><description>&lt;p&gt;It has been a while since I&amp;#39;ve used code to return a stored procedure - but you should be able to just return a results table.&lt;/p&gt;
&lt;p&gt;In fact, you can modify your data connection to execute a stored procedure - maybe pass in a dummy value for your parameter? And then you will be assured that the schema IP creates will be correct for the returned results of your procedure?&lt;/p&gt;</description></item><item><title>Querying Secondary Data Connection into a Repeating Table using SQL Server Stored Proc.</title><link>https://www.infopathdev.com:443/forums/thread/67879.aspx</link><pubDate>Wed, 29 Jun 2011 19:03:18 GMT</pubDate><guid isPermaLink="false">033a2e2d-04e2-4a9d-be01-a4634161eefd:67879</guid><dc:creator>rlstrommen</dc:creator><slash:comments>0</slash:comments><comments>https://www.infopathdev.com:443/forums/thread/67879.aspx</comments><wfw:commentRss>https://www.infopathdev.com:443/forums/commentrss.aspx?SectionID=47&amp;PostID=67879</wfw:commentRss><description>&lt;p&gt;OK, from my previous post, I&amp;#39;ve now implemented another button that does a search on our material database.&amp;nbsp; When I went to implement paging, however, I found that the ADO query processor doesn&amp;#39;t understand the SQL Server WITH verb.&lt;/p&gt;
&lt;p&gt;Functional Requirement&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;div&gt;User enters a search criteria, and clicks search&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;
&lt;div&gt;1-20 rows are returned&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;
&lt;div&gt;User clicks on Next and Prev to get to the page of results that he wants to see.&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;
&lt;div&gt;User selects a material, and then proceeds with the rest of the form.&lt;/div&gt;&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;Constraints&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;div&gt;We don&amp;#39;t have a website yet,&amp;nbsp;so writing this as a webservice isn&amp;#39;t available to us yet.&amp;nbsp;&lt;/div&gt;&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;What I want to do&amp;nbsp;is to write a stored procedure that takes as parameters the SearchFilter, the first row and the last row of the result query.&amp;nbsp; I&amp;#39;m a little confused, though, as to what the stored procedure should return.&lt;/p&gt;
&lt;p&gt;Question: Do I simply return the results of my query in the stored procedure?&amp;nbsp; I&amp;#39;m kind of thinking yes, but I&amp;#39;d like confirmation on that.&lt;/p&gt;
&lt;p&gt;Here is my current code.&amp;nbsp; The SQL Command will ultimately be replaced with EXEC dbo.SearchMaterialTables inputSearchFilter, FirstRow, LastRow&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;pre&gt;        Public Sub Do_Material_Search(ByVal FirstRow As Integer, ByVal LastRow As Integer)
            &amp;#39; Write your code here.
            &amp;#39; Write your code here.
            &amp;#39;
            &amp;#39; Write your code here to change the main data source.
            &amp;#39; Update /my:main/my:Material_Section/my:Material_Search_Group/my:SearchResultCount
            &amp;#39; with the number of hits on the field
            &amp;#39;
            &amp;#39;  Repeating Table declarations
            &amp;#39;
            Dim rootXMLPath As String = &amp;quot;/my:main/my:Material_Section/my:Material_Search_Group&amp;quot;
            Dim mainNavigator As XPathNavigator
            Dim msynRows As XPathNodeIterator
            Dim msynRow As XPathNavigator
            &amp;#39;
            &amp;#39;  Search Result declarations
            &amp;#39;
            Dim resultNavigator As XPathNavigator
            Dim msynResults As XPathNodeIterator
            Dim I As Integer
            Dim writer As XmlWriter
            Dim MatID As String
            Dim PreferredName As String
            Dim IUPACName As String
            Dim recordCountSet As Boolean
            Dim hitCount As Boolean
            &amp;#39;
            Dim resultSetPath As String = &amp;quot;/dfs:myFields/dfs:dataFields/d:SAGE_SP_COA_MATERIAL_SEARCH_BASE&amp;quot;
            Dim sageSearchMaterialPath As String = &amp;quot;/my:main/my:Material_Section/my:Material_Search_Group/my:Search_Material&amp;quot;
            &amp;#39;
            Dim queryConnection As AdoQueryConnection
            Dim origCommand As String
            Dim sqlClause As String
            &amp;#39;
            Dim MsynDataSource As DataSource
            Dim myNamespace As String
            Dim dfsNamespace As String
            &amp;#39;
            Dim DebugLine As Integer
            Dim SearchMatlSynFilter 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 &amp;quot;MAT_ID&amp;quot;,&amp;quot;SYNONYM_NAME&amp;quot; 
            &amp;#39;  FROM   &amp;quot;dbo&amp;quot;.&amp;quot;SAGE_SP_COA_MATERIAL_SEARCH_BASE&amp;quot; as &amp;quot;SAGE_SP_COA_MATERIAL_SEARCH_BASE&amp;quot; 
            &amp;#39;  WHERE SYNONYM_NAME = /my:main/my:Material_Section/my:Material_Search_Group/my:Search_Material
            &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
                SearchMatlSynFilter = mainNavigator.SelectSingleNode(sageSearchMaterialPath, NamespaceManager).Value
                &amp;#39;
                &amp;#39;  Save this search in the Prev filter for the results display
                &amp;#39;
                mainNavigator.SelectSingleNode(rootXMLPath &amp;amp; &amp;quot;/my:PrevSearchFilter&amp;quot;, NamespaceManager).SetValue(SearchMatlSynFilter)
                DebugLine = 40
                sqlClause = &amp;quot;&amp;quot;
                sqlClause = sqlClause &amp;amp; &amp;quot;WITH SearchResult AS&amp;quot; &amp;amp; vbCrLf
                sqlClause = sqlClause &amp;amp; &amp;quot;(&amp;quot; &amp;amp; vbCrLf
                sqlClause = sqlClause &amp;amp; &amp;quot;     SELECT TOP 100 PERCENT &amp;quot; &amp;amp; vbCrLf
                sqlClause = sqlClause &amp;amp; &amp;quot;            ROW_NUMBER() OVER (ORDER BY results.SR_PREFERRED_NAME ASC) AS RowNumber&amp;quot; &amp;amp; vbCrLf
                sqlClause = sqlClause &amp;amp; &amp;quot;           ,results.SR_MAT_ID          AS MAT_ID&amp;quot; &amp;amp; vbCrLf
                sqlClause = sqlClause &amp;amp; &amp;quot;           ,results.SR_PREFERRED_NAME  AS PREFERRED_NAME&amp;quot; &amp;amp; vbCrLf
                sqlClause = sqlClause &amp;amp; &amp;quot;           ,results.SR_IUPAC_NAME      AS IUPAC_NAME&amp;quot; &amp;amp; vbCrLf
                sqlClause = sqlClause &amp;amp; &amp;quot;     FROM (&amp;quot; &amp;amp; vbCrLf
                sqlClause = sqlClause &amp;amp; &amp;quot;          SELECT DISTINCT&amp;quot; &amp;amp; vbCrLf
                sqlClause = sqlClause &amp;amp; &amp;quot;                 MAT_ID          AS SR_MAT_ID&amp;quot; &amp;amp; vbCrLf
                sqlClause = sqlClause &amp;amp; &amp;quot;                  ,PREFERRED_NAME  AS SR_PREFERRED_NAME&amp;quot; &amp;amp; vbCrLf
                sqlClause = sqlClause &amp;amp; &amp;quot;                  ,IUPAC_NAME      AS SR_IUPAC_NAME&amp;quot; &amp;amp; vbCrLf
                sqlClause = sqlClause &amp;amp; &amp;quot;          FROM(dbo.SAGE_SP_COA_MATERIAL_SEARCH_RESULT)&amp;quot; &amp;amp; vbCrLf
                sqlClause = sqlClause &amp;amp; &amp;quot;          WHERE UPPER(SYNONYM_NAME) LIKE UPPER&amp;#39;&amp;quot; + SearchMatlSynFilter.Replace(&amp;quot;&amp;#39;&amp;quot;, &amp;quot;&amp;#39;&amp;#39;&amp;quot;) + &amp;quot;&amp;#39;)&amp;quot; &amp;amp; vbCrLf
                sqlClause = sqlClause &amp;amp; &amp;quot;          ) results&amp;quot; &amp;amp; vbCrLf
                sqlClause = sqlClause &amp;amp; &amp;quot;)&amp;quot; &amp;amp; vbCrLf
                sqlClause = sqlClause &amp;amp; &amp;quot;SELECT	MAT_ID         AS MAT_ID&amp;quot; &amp;amp; vbCrLf
                sqlClause = sqlClause &amp;amp; &amp;quot;        ,PREFERRED_NAME AS PREFERRED_NAME&amp;quot; &amp;amp; vbCrLf
                sqlClause = sqlClause &amp;amp; &amp;quot;        ,IUPAC_NAME     AS IUPAC_NAME&amp;quot; &amp;amp; vbCrLf
                sqlClause = sqlClause &amp;amp; &amp;quot;       ,(SELECT COUNT(*)&amp;quot; &amp;amp; vbCrLf
                sqlClause = sqlClause &amp;amp; &amp;quot;                FROM(SearchResult)&amp;quot; &amp;amp; vbCrLf
                sqlClause = sqlClause &amp;amp; &amp;quot;        ) AS TotalCount&amp;quot; &amp;amp; vbCrLf
                sqlClause = sqlClause &amp;amp; &amp;quot;FROM (SearchResult)&amp;quot; &amp;amp; vbCrLf
                sqlClause = sqlClause &amp;amp; &amp;quot;WHERE	 RowNumber BETWEEN &amp;quot; &amp;amp; FirstRow.ToString &amp;amp; &amp;quot; AND &amp;quot; &amp;amp; LastRow.ToString &amp;amp; vbCrLf
                sqlClause = sqlClause &amp;amp; &amp;quot;ORDER BY RowNumber&amp;quot;
                &amp;#39;
                &amp;#39;  Paging query - Need first and last row number, and the search value
                &amp;#39;  WITH SearchResult AS
                &amp;#39;  (
                &amp;#39;       SELECT TOP 100 PERCENT 
                &amp;#39;              ROW_NUMBER() OVER (ORDER BY results.SR_PREFERRED_NAME ASC) AS RowNumber
                &amp;#39;             ,results.SR_MAT_ID          AS MAT_ID
                &amp;#39;             ,results.SR_PREFERRED_NAME  AS PREFERRED_NAME
                &amp;#39;             ,results.SR_IUPAC_NAME      AS IUPAC_NAME
                &amp;#39;       FROM (
                &amp;#39;            SELECT DISTINCT
                &amp;#39;                   MAT_ID          AS SR_MAT_ID
                &amp;#39;                    ,PREFERRED_NAME  AS SR_PREFERRED_NAME
                &amp;#39;                    ,IUPAC_NAME      AS SR_IUPAC_NAME
                &amp;#39;            FROM(dbo.SAGE_SP_COA_MATERIAL_SEARCH_RESULT)
                &amp;#39;            WHERE UPPER(SYNONYM_NAME) LIKE UPPER(&amp;#39;%clop%&amp;#39;)
                &amp;#39;            ) results
                &amp;#39;  )
                &amp;#39;  SELECT	MAT_ID         AS MAT_ID
                &amp;#39;          ,PREFERRED_NAME AS PREFERRED_NAME
                &amp;#39;          ,IUPAC_NAME     AS IUPAC_NAME
                &amp;#39;  FROM (SearchResult)
                &amp;#39;  WHERE	 RowNumber BETWEEN 1 AND 20
                &amp;#39;  ORDER BY RowNumber
                &amp;#39;  
                DebugLine = 60
                MsynDataSource = Me.DataSources(&amp;quot;SAGE_SP_COA_MATERIAL_SEARCH_BASE&amp;quot;)
                queryConnection = MsynDataSource.QueryConnection
                DebugLine = 70
                origCommand = queryConnection.Command
                &amp;#39;
                &amp;#39;  modify the query to use the where clause
                &amp;#39;
                DebugLine = 80
                queryConnection.Command = sqlClause
                &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 = MsynDataSource.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;
                &amp;#39;  Did we get any query results?
                &amp;#39;
                &amp;#39;
                DebugLine = 1201
                &amp;#39;
                &amp;#39;  Return the record count in this iteration.
                &amp;#39;
                hitCount = 0
                recordCountSet = False
                &amp;#39;
                If resultNavigator IsNot Nothing Then
                    &amp;#39;
                    DebugLine = 1300
                    msynResults = resultNavigator.Select(resultSetPath, NamespaceManager)
                    &amp;#39;  
                    &amp;#39; Here is the example code to move data from a secondary data source into a repeating table.
                    &amp;#39; Listing 17.5. Fully Changed Method 
                    &amp;#39;
                    &amp;#39;  Repeating Group: /my:main/my:Material_Section/my:Material_Search_Group/my:SynonymSearchResults/my:MsynRow
                    &amp;#39;  First task is to remove results from any previous search.
                    &amp;#39;  For whatever reason, the example says to delete from the bottom up
                    &amp;#39;  Note: Be sure to check if the xml array is zero relative or not
                    &amp;#39;  
                    DebugLine = 1400
                    msynRows = mainNavigator.Select(rootXMLPath &amp;amp; &amp;quot;/my:SynonymSearchResults/my:MsynRow&amp;quot;, NamespaceManager)
                    If (msynRows.Count &amp;gt; 0) Then
                        For I = msynRows.Count To 1 Step -1
                            msynRow = mainNavigator.SelectSingleNode(rootXMLPath &amp;amp; &amp;quot;/my:SynonymSearchResults/my:MsynRow[&amp;quot; + I + &amp;quot;]&amp;quot;, NamespaceManager)
                            msynRow.DeleteSelf()
                        Next
                    End If
                    &amp;#39;
                    &amp;#39;  Populate return data
                    &amp;#39;
                    DebugLine = 1500
                    While (msynResults.MoveNext())

                        If Not recordCountSet Then
                            NewValue = msynResults.Current.SelectSingleNode(&amp;quot;d:TotalCount&amp;quot;, NamespaceManager).Value.ToString()
                            mainNavigator.SelectSingleNode(rootXMLPath &amp;amp; &amp;quot;/my:SearchResultCount&amp;quot;, NamespaceManager).SetValue(NewValue)
                            recordCountSet = True
                        End If
                        MatID = msynResults.Current.SelectSingleNode(&amp;quot;d:MAT_ID&amp;quot;, NamespaceManager).Value.ToString()
                        PreferredName = msynResults.Current.SelectSingleNode(&amp;quot;d:PREFERRED_NAME&amp;quot;, NamespaceManager).Value.ToString()
                        IUPACName = msynResults.Current.SelectSingleNode(&amp;quot;d:IUPAC_NAME&amp;quot;, NamespaceManager).Value.ToString()
                        &amp;#39;
                        &amp;#39;  Populate the repeating table 
                        &amp;#39;
                        writer = mainNavigator.SelectSingleNode(rootXMLPath &amp;amp; &amp;quot;/my:SynonymSearchResults&amp;quot;, NamespaceManager).AppendChild()
                        writer.WriteStartElement(&amp;quot;MsynRow&amp;quot;, myNamespace)
                        writer.WriteElementString(&amp;quot;SelectMe&amp;quot;, myNamespace, MatID)
                        writer.WriteElementString(&amp;quot;PreferredName&amp;quot;, myNamespace, PreferredName)
                        writer.WriteElementString(&amp;quot;IUPACName&amp;quot;, myNamespace, IUPACName)
                        writer.WriteEndElement()
                        writer.Close()
                    End While
                    &amp;#39;
                    &amp;#39;  Now all the results are listed
                    &amp;#39;
                End If
                &amp;#39;
                DebugLine = 1600
                If Not recordCountSet Then
                    NewValue = &amp;quot;0&amp;quot;
                    mainNavigator.SelectSingleNode(rootXMLPath &amp;amp; &amp;quot;/my:SearchResultCount&amp;quot;, NamespaceManager).SetValue(NewValue)
                End If
                &amp;#39;
                mainNavigator.SelectSingleNode(rootXMLPath &amp;amp; &amp;quot;/my:FirstRow&amp;quot;, NamespaceManager).SetValue(FirstRow.ToString)
                mainNavigator.SelectSingleNode(rootXMLPath &amp;amp; &amp;quot;/my:LastRow&amp;quot;, NamespaceManager).SetValue(LastRow.ToString)
                &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

        Public Sub SearchMaterialButton_Clicked(ByVal sender As Object, ByVal e As ClickedEventArgs)
            Do_Material_Search(1, 20)
        End Sub

        Public Sub NextSearchButton_Clicked(ByVal sender As Object, ByVal e As ClickedEventArgs)
            Dim rootXMLPath As String = &amp;quot;/my:main/my:Material_Section/my:Material_Search_Group&amp;quot;
            Dim mainNavigator As XPathNavigator
            Dim FirstRow, LastRow, TotalRows As Integer
            Dim thisSearch, prevSearch As String
            &amp;#39;
            &amp;#39;  Get the current search state
            &amp;#39;
            mainNavigator = Me.CreateNavigator()
            &amp;#39;
            &amp;#39;  Get the current search state
            &amp;#39;
            mainNavigator = Me.CreateNavigator()
            thisSearch = mainNavigator.SelectSingleNode(rootXMLPath &amp;amp; &amp;quot;/my:Search_Material&amp;quot;, NamespaceManager).Value
            prevSearch = mainNavigator.SelectSingleNode(rootXMLPath &amp;amp; &amp;quot;/my:PrevSearchFilter&amp;quot;, NamespaceManager).Value
            If thisSearch &amp;lt;&amp;gt; prevSearch Then

                &amp;#39;
                &amp;#39;  If they changed the search, then reset the page counters
                &amp;#39;
                FirstRow = 1
                LastRow = 20
            Else
                TotalRows = Integer.Parse(mainNavigator.SelectSingleNode(rootXMLPath &amp;amp; &amp;quot;/my:SearchResultCount&amp;quot;, NamespaceManager).Value)
                FirstRow = Integer.Parse(mainNavigator.SelectSingleNode(rootXMLPath &amp;amp; &amp;quot;/my:FirstRow&amp;quot;, NamespaceManager).Value)
                LastRow = Integer.Parse(mainNavigator.SelectSingleNode(rootXMLPath &amp;amp; &amp;quot;/my:LastRow&amp;quot;, NamespaceManager).Value)
                &amp;#39;
                &amp;#39;  Adjust the page window
                &amp;#39;
                FirstRow = FirstRow + 20
                If FirstRow &amp;lt; 1 Then
                    FirstRow = 1
                End If
                &amp;#39;
                &amp;#39;  If the FirstRow goes past the total number of hits,
                &amp;#39;  then set it to the last multiple of 20 less than the total
                &amp;#39;  If we have 0 rows, then display 0 to 0 instead of 1 to 0.
                &amp;#39;
                If FirstRow &amp;gt; TotalRows Then
                    FirstRow = Math.Floor(TotalRows / 20) * 20
                    If TotalRows &amp;gt; 0 Then
                        FirstRow = FirstRow + 1
                    End If
                End If
                &amp;#39;
                LastRow = FirstRow + 20
                If LastRow &amp;gt; TotalRows Then
                    LastRow = TotalRows
                End If
            End If
            &amp;#39;
            &amp;#39;  Execute the search
            &amp;#39;
            Do_Material_Search(FirstRow, LastRow)
        End Sub

        Public Sub PrevSearchButton_Clicked(ByVal sender As Object, ByVal e As ClickedEventArgs)
            Dim rootXMLPath As String = &amp;quot;/my:main/my:Material_Section/my:Material_Search_Group&amp;quot;
            Dim mainNavigator As XPathNavigator
            Dim FirstRow, LastRow, TotalRows As Integer
            Dim thisSearch, prevSearch As String
            &amp;#39;
            &amp;#39;  Get the current search state
            &amp;#39;
            mainNavigator = Me.CreateNavigator()
            thisSearch = mainNavigator.SelectSingleNode(rootXMLPath &amp;amp; &amp;quot;/my:Search_Material&amp;quot;, NamespaceManager).Value
            prevSearch = mainNavigator.SelectSingleNode(rootXMLPath &amp;amp; &amp;quot;/my:PrevSearchFilter&amp;quot;, NamespaceManager).Value
            If thisSearch &amp;lt;&amp;gt; prevSearch Then

                &amp;#39;
                &amp;#39;  If they changed the search, then reset the page counters
                &amp;#39;
                FirstRow = 1
                LastRow = 20
            Else
                TotalRows = Integer.Parse(mainNavigator.SelectSingleNode(rootXMLPath &amp;amp; &amp;quot;/my:SearchResultCount&amp;quot;, NamespaceManager).Value)
                FirstRow = Integer.Parse(mainNavigator.SelectSingleNode(rootXMLPath &amp;amp; &amp;quot;/my:FirstRow&amp;quot;, NamespaceManager).Value)
                LastRow = Integer.Parse(mainNavigator.SelectSingleNode(rootXMLPath &amp;amp; &amp;quot;/my:LastRow&amp;quot;, NamespaceManager).Value)
                &amp;#39;
                &amp;#39;  Adjust the page window
                &amp;#39;
                FirstRow = FirstRow - 20
                If FirstRow &amp;lt; 1 Then
                    FirstRow = 1
                End If
                If FirstRow &amp;gt; TotalRows Then
                    FirstRow = TotalRows
                End If
                &amp;#39;
                &amp;#39;  If the FirstRow goes past the total number of hits,
                &amp;#39;  then set it to the last multiple of 20 less than the total
                &amp;#39;  If we have 0 rows, then display 0 to 0 instead of 1 to 0.
                &amp;#39;
                If FirstRow &amp;gt; TotalRows Then
                    FirstRow = Math.Floor(TotalRows / 20) * 20
                    If TotalRows &amp;gt; 0 Then
                        FirstRow = FirstRow + 1
                    End If
                End If
                &amp;#39;
                LastRow = FirstRow + 20
                If LastRow &amp;gt; TotalRows Then
                    LastRow = TotalRows
                End If
            End If
            &amp;#39;
            &amp;#39;  Execute the search
            &amp;#39;
            Do_Material_Search(FirstRow, LastRow)
        End Sub
&lt;/pre&gt;</description></item></channel></rss>