Querying secondary data connection - InfoPath Dev
in

InfoPath Dev

Use our Google Custom Search for best site search results.

Querying secondary data connection

Last post 02-28-2009 10:41 PM by Donna Kelly. 45 replies.
Page 2 of 4 (46 items) < Previous 1 2 3 4 Next >
Sort Posts: Previous Next
  • 12-31-2008 09:34 PM In reply to

    Re: Querying secondary data connection

    I was able to work up a JScript version.

    As set up, you need to do the following:

    Find this line at the top of the script (it will be slightly different in your script)

    XDocument.DOM.setProperty("SelectionNamespaces", 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ...[remainder abbreviated]');

    copy out the second parameter (the part with all the namespaces, between the single quote marks), and use it to create these two lines immediately below that line (don't make any changes to the original line), replacing TestTable1 with the name of the secondary data source you will be using.  You can duplicate that second line as much as you want if you will be querying multiple data sources

    var Namespaces = 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ...[remainder abbreviated]';
    XDocument.DataObjects["TestTable1"].DOM.setProperty("SelectionNamespaces", Namespaces);

    Here is the code:

    function CTRL11_5::OnClick(eventObj)
    {
         if(RunQuery())
         {
              CopyResults();
         }
    }


    function RunQuery()

         // select the field to be queried 
         var field1 = XDocument.DOM.selectSingleNode("/my:myFields/my:field1"); 
         // make sure the field is non-empty 
         if(field1.text.length > 0) 
         { 
              var dc = XDocument.DataObjects["TestTable1"].QueryAdapter; 
              var originalCommand = dc.Command;           // construct the SQL Query 
              dc.Command = "SELECT * FROM [TestTable1] WHERE [OrderNum] = '" + field1.text + "' OR [ID] = '" + field1.text + "'";
              dc.Query(); 
              dc.Command = originalCommand;
              // return true if the query was successful 
              return true; 
         } 
         else 
         { 
              XDocument.UI.Alert("You must enter a value before querying!"); 
              // return false because the query was not performed 
              return false; 
         }
    }

    function CopyResults()

         // get a reference to the node where the results will be placed in the main data source 
         var dataFields = XDocument.DOM.selectSingleNode("/my:myFields/my:QueryResults"); 

         // get a reference to the node with the query results in the secondary data source 
         var queryDataFields = XDocument.DataObjects["TestTable1"].DOM.selectSingleNode("/dfs:myFields/dfs:dataFields"); 

         // clear out the children of the QueryResults node in the main DOM 
         while(dataFields.hasChildNodes()) 
         { 
              dataFields.removeChild(dataFields.childNodes[0]); 
         } 

         // select all of the result rows from the query 
         var queryRows = queryDataFields.selectNodes("*"); 

         // iterate through the query result rows and copy them over 
         for(var i = 0; i < queryRows.length; i++) 
         { 
              var queryRow = queryRows[i]; 

              // Add a new row to the repeating table in the main view 
              // The value that appears as "ResultRow_19" here can be obtained 
              // From the Advanced tab of the repeating table's properties 
              XDocument.View.ExecuteAction("xCollection::insert", "ResultRow_19"); 

              // select all the fields in the newly created row. Their names must coincide 
              // precisely to the names of the fields that you want to copy 
              var resultFields = dataFields.selectNodes("my:ResultRow[last()]/*"); 
              for(var j = 0; j < resultFields.length; j++) { 
                   resultFields[j].text = queryRow.attributes.getNamedItem(resultFields[j].baseName).nodeValue; 
              } 
         }
    }

    If you used similar naming conventions to my examples, this should work without modification, except for the underlined parts which would be naturally different depending on your situation.

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 01-29-2009 05:39 AM In reply to

    Re: Querying secondary data connection

              var nodeString = "<my:ResultRow +" Namespaces +">";

    Jimmy,

    I am just learning the coding aspect of this...what needs to go in place of Namespaces above? This is using your jscript code you provided above.  When I run it, I get an error on this line.  Thanks.

  • 01-29-2009 07:31 AM In reply to

    Re: Querying secondary data connection

    It looks like a typo crept in on that line.  What happens if you use this instead:

    var nodeString = "<my:ResultRow "+ Namespaces +">"; 

    Note the different location of the second quotation mark.

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 01-29-2009 07:47 AM In reply to

    Re: Querying secondary data connection

    I now am getting the following error.

     

    A name was started with an invalid character.

    It is pointing to the very first line.

     

    XDocument.DOM.setProperty("SelectionNamespaces", 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ...[remainder abbreviated]');

     

    What's are thoughts?

  • 01-29-2009 07:51 PM In reply to

    Re: Querying secondary data connection

    I wrote "...[remainder abbreviated]" to make that line shorter in my post.  You're not supposed to actually modify it to say ...[remainder abbreviated].

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 01-29-2009 11:45 PM In reply to

    Re: Querying secondary data connection

     I apoligize for the lack of experience with coding...i hope  you had a good chuckle any ways:-)

    my fields have been named the same as yours in your example, yet, I am getting this error now.

    The following error occurred:

    Reference to undeclared namespace prefix: 'my'.

    File:script.js
    Line:30


    Unspecified error

    This is the line. 

    var field1 = XDocument.DOM.selectSingleNode("/my:myFields/my:field1");

     

    I kept it like your example for my testing of your code yet, I got this error.  what do you think?

     

    jimmy, i appreciate it.

  • 01-30-2009 08:36 AM In reply to

    Re: Querying secondary data connection

    Could you check near the top of the file .js file to make sure that the XDocument.DOM.setProperty..... line is still there and it contains a definition for the my namespace (you should see xmlns:my= somewhere if it's there.

    If the line is there and that error still occurs, please paste that line into this thread so I can have a look at it.

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 01-30-2009 08:43 AM In reply to

    Re: Querying secondary data connection

     This is what I have.... 

     

    XDocument.DOM.setProperty("SelectionNamespaces", 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"');

    var Namespaces = 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"';

    XDocument.DataObjects["MatterBasicInfo"].DOM.setProperty("SelectionNamespaces", Namespaces);

    function CTRL1219::OnClick(eventObj)
    {
         if(RunQuery())
         {
              CopyResults();
         }
    }

    function RunQuery()
    {
         // select the field to be queried
         var field1 = XDocument.DOM.selectSingleNode("/my:myFields/my:field1");
         // make sure the field is non-empty
         if(field1.text.length > 0)
         {
              var dc = XDocument.DataObjects["x"].QueryAdapter;
              // construct the SQL Query
              dc.Command = "SELECT * FROM [x] WHERE [x] = '" + field1.text + "'";
              dc.Query();
              // return true if the query was successful
              return true;
         }
         else
         {
              XDocument.UI.Alert("You must enter a value before querying!");
              // return false because the query was not performed
              return false;
         }
    }


    function CopyResults()
    {
         // store the names of the columns in an array
         var columnNames = ["x", "x", "x", "x", "x", "x_x", "x"];

         // get a reference to the node where the results will be placed in the main data source
         var dataFields = XDocument.DOM.selectSingleNode("/my:myFields/my:QueryResults");

         // get a reference to the node with the query results in the secondary data source
         var queryDataFields = XDocument.DataObjects["x"].DOM.selectSingleNode("/dfs:myFields/dfs:dataFields");

         // clear out the children of the QueryResults node in the main DOM
         while(dataFields.hasChildNodes())
         {
              dataFields.removeChild(dataFields.childNodes[0]);
         }

         // select all of the result rows from the query
         var queryRows = queryDataFields.selectNodes("*");

         // iterate through the query result rows and copy them over
         for(var i = 0; i < queryRows.length; i++)
         {
              var queryRow = queryRows[i];
              // create an opening my:ResultRow tag with the requisite namespaces
              var nodeString = "<my:ResultRow "+ Namespaces +">";

              // iterate through the column names and add a node to my:ResultRow for each one
              for(var colNum in columnNames)
              {
                   // get the column name from the array
                   var colName = columnNames[colNum];
                   // retrieve the column's value from the query row node
                   var colValue = queryRow.attributes.getNamedItem(colName).nodeValue;

                   // construct a node string for the column's node and append it to the
                   // my:ResultRow node string we are constructing
                   nodeString += "<my:" + colName+ ">" + colValue + "</my:" + colName + ">";
              }

              // close the my:ResultRow node
              nodeString += "</my:ResultRow>";

              // create a node from the string and append it to my:QueryResults
              dataFields.appendChild(CreateNode(nodeString));
         }
    }

    function CreateNode(nodeString)
    {
         var node = XDocument.CreateDOM();
         node.loadXML(nodeString);
         return node.documentElement;

  • 01-30-2009 08:54 AM In reply to

    Re: Querying secondary data connection

    Yeah, it looks like you've removed part of that setProperty line.  Please back up the contents of that file in a TXT file or something, go to Form Options and remove the code, then re-add the event handler to your button.  That should restore that line to its correct state, and then you can paste in the code you backed up, with the exception of those top two lines (you will need to re-create the var Namespaces = .... line to match that one).

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 01-30-2009 09:15 AM In reply to

    Re: Querying secondary data connection

    Just so that I understand you right...

    I have changed the top 3 lines to this.

    XDocument.DOM.setProperty("SelectionNamespaces", 'xmlns:my="http://www.w3.org/2001/XMLSchema-instance"');

    var Namespaces = 'xmlns:my="http://www.w3.org/2001/XMLSchema-instance"';

    XDocument.DataObjects["MatterBasicInfo"].DOM.setProperty("SelectionNamespaces", Namespaces);

     

    I need to physically type this in once I have created a new button with the below code? I only ask this because, I just basically pasted all of this once I deleted what was there and I still get the error about this line.

     var field1 = XDocument.DOM.selectSingleNode("/my:myFields/my:field1");

     

    Code: 

    function CTRL1219::OnClick(eventObj)
    {
         if(RunQuery())
         {
              CopyResults();
         }
    }

    function RunQuery()
    {
         // select the field to be queried
         var field1 = XDocument.DOM.selectSingleNode("/my:myFields/my:field1");
         // make sure the field is non-empty
         if(field1.text.length > 0)
         {
              var dc = XDocument.DataObjects["x"].QueryAdapter;
              // construct the SQL Query
              dc.Command = "SELECT * FROM [x] WHERE [x] = '" + field1.text + "'";
              dc.Query();
              // return true if the query was successful
              return true;
         }
         else
         {
              XDocument.UI.Alert("You must enter a value before querying!");
              // return false because the query was not performed
              return false;
         }
    }


    function CopyResults()
    {
         // store the names of the columns in an array
         var columnNames = ["x", "x", "x", "x", "x", "x_x", "x"];

         // get a reference to the node where the results will be placed in the main data source
         var dataFields = XDocument.DOM.selectSingleNode("/my:myFields/my:QueryResults");

         // get a reference to the node with the query results in the secondary data source
         var queryDataFields = XDocument.DataObjects["x"].DOM.selectSingleNode("/dfs:myFields/dfs:dataFields");

         // clear out the children of the QueryResults node in the main DOM
         while(dataFields.hasChildNodes())
         {
              dataFields.removeChild(dataFields.childNodes[0]);
         }

         // select all of the result rows from the query
         var queryRows = queryDataFields.selectNodes("*");

         // iterate through the query result rows and copy them over
         for(var i = 0; i < queryRows.length; i++)
         {
              var queryRow = queryRows[i];
              // create an opening my:ResultRow tag with the requisite namespaces
              var nodeString = "<my:ResultRow "+ Namespaces +">";

              // iterate through the column names and add a node to my:ResultRow for each one
              for(var colNum in columnNames)
              {
                   // get the column name from the array
                   var colName = columnNames[colNum];
                   // retrieve the column's value from the query row node
                   var colValue = queryRow.attributes.getNamedItem(colName).nodeValue;

                   // construct a node string for the column's node and append it to the
                   // my:ResultRow node string we are constructing
                   nodeString += "<my:" + colName+ ">" + colValue + "</my:" + colName + ">";
              }

              // close the my:ResultRow node
              nodeString += "</my:ResultRow>";

              // create a node from the string and append it to my:QueryResults
              dataFields.appendChild(CreateNode(nodeString));
         }
    }

    function CreateNode(nodeString)
    {
         var node = XDocument.CreateDOM();
         node.loadXML(nodeString);
         return node.documentElement;
    }


     

     

  • 01-30-2009 09:17 AM In reply to

    Re: Querying secondary data connection

    it tells me that

    object required for this line.

     var field1 = XDocument.DOM.selectSingleNode("/my:myFields/my:field1");

  • 01-30-2009 09:21 AM In reply to

    Re: Querying secondary data connection

    now it is for this line

     if(field1.text.length > 0)

     says that an object is required.

     

    I literally created a new button this time.

     

  • 01-30-2009 09:26 AM In reply to

    Re: Querying secondary data connection

    ZaiZai:
    I have changed the top 3 lines to this.

    XDocument.DOM.setProperty("SelectionNamespaces", 'xmlns:my="http://www.w3.org/2001/XMLSchema-instance"');

    var Namespaces = 'xmlns:my="http://www.w3.org/2001/XMLSchema-instance"';

    XDocument.DataObjects["MatterBasicInfo"].DOM.setProperty("SelectionNamespaces", Namespaces);

    I need to physically type this in once I have created a new button with the below code? I only ask this because, I just basically pasted all of this once I deleted what was there and I still get the error about this line.

    No.  The first line should be generated by InfoPath and you shouldn't touch it. There is information missing from it the way you have it there.  You need to copy part of that line (the part in single quotes starting after the comma and up to the end parenthesis) to create the second line.  The third line is ok the way you have it.

     Please back up your code, and from the InfoPath designer go to Tools->Form Options->Programming, and click the Remove Code button.  Then open up the script editor in whatever manner you have until now, and you should find that the first line is restored to the way it's supposed to be.

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 01-30-2009 09:28 AM In reply to

    Re: Querying secondary data connection

    Yeah, it looks like you've removed part of that setProperty line.  Please back up the contents of that file in a TXT file or something, go to Form Options and remove the code, then re-add the event handler to your button.  That should restore that line to its correct state, and then you can paste in the code you backed up, with the exception of those top two lines (you will need to re-create the var Namespaces = .... line to match that one).

     

    What exactly do I need to make sure matches? If I can understand this, I think that should resolve it...

  • 01-30-2009 09:32 AM In reply to

    Re: Querying secondary data connection

    Just copy the part in single quotes starting after the comma and going up to the end parenthesis.  In my form, the full line looks like this:

    var Namespaces = 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:d="http://schemas.microsoft.com/office/infopath/2003/ado/dataFields" xmlns:dfs="http://schemas.microsoft.com/office/infopath/2003/dataFormSolution" xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2008-10-24T09:48:18" xmlns:xd="http://schemas.microsoft.com/office/infopath/2003"'

    As for the error about the null reference, is there actually a field called field1 in your form?

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
Page 2 of 4 (46 items) < Previous 1 2 3 4 Next >
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.