Copy data from secondary to primary data source - InfoPath Dev
in

InfoPath Dev

Copy data from secondary to primary data source

Last post 11-30-2006 08:13 AM by leGeneral. 7 replies.
Page 1 of 1 (8 items)
Sort Posts: Previous Next
  • 11-14-2006 01:20 PM

    Copy data from secondary to primary data source

    Hallo,

    After some days of trying and searching without any result I hope someone could help me to solve my problem.

    First of all what I am trying to do:
    I developed a form based on an access database with a sales table. I created a secondary datasource ("Produkte") which receives data (detailed data of products) from the same database.
    The products are bound to a repeating table. I also added a button to the repeating table, so that there is a button in every row. After clicking the button some of the products data should be copied to the sales table (also a repeating table).
    Afterwards I want to save the rows of the sales table to the database.


    I don't know how I could solve this problem?

    After reading this post I tried it also this way:
    http://infopathdev.com/forums/topic.asp?TOPIC_ID=2781&SearchTerms=oFromItem,length

    var selectedNodes=XDocument.GetDOM("Produkte").selectedNodes("/dfs:myFields/dfs:dataFields/d:Produkte");

    selectedNodes.length is just returning "0".

    I also set the namespace like in the HowTo on this page:

    XDocument.GetDOM( "Produkte" ).setProperty( "SelectionNamespaces" , 'xmlns:dfs="http://schemas.microsoft.com/office/infopath/2003/dataFormSolution " xmlns:d="http://schemas.microsoft.com/office/infopath/2003/ado/dataFields"');

    I also tried a lot of variations to access the secondary datasource like:

    XDocument.GetDOM("Produkte").selectSingleNode("//dfs:myFields/dfs:dataFields/d:Produkte/@Id").text;


    I hope someone could help me cause at the moment I don't know what else to try.

    Christoph
  • 11-15-2006 09:44 AM In reply to

    • Shiva
    • Top 25 Contributor
    • Joined on 04-15-2005
    • India
    • Posts 694

    Re: Copy data from secondary to primary data source

    XDocument secondaryDOM = XDocument.GetDOM( "Produkte" );
    //First you need to check the DOM is getting or not
    secondaryDOM.setProperty( "SelectionNamespaces" , 'xmlns:dfs="http://schemas.microsoft.com/office/infopath/2003/dataFormSolution " xmlns:d="http://schemas.microsoft.com/office/infopath/2003/ado/dataFields"');
    //Try to set the property of the namespaces

    var nodeText = secondaryDOM.selectSingleNode("/dfs:myFields/dfs:dataFields/d:Produkte/@Id").text;
    //Try to get the node text.

    For each step you need to debug and let you know where is the your code is wrong. If you get the any specific error let us know.
  • 11-15-2006 09:44 AM In reply to

    Re: Copy data from secondary to primary data source

    First verify that GetDOM is actually returning an object.

    Then see if you can select the root node. Then then next one down, etc.

    Troubleshoot this way to figure out where the source of the problem is. Find out where the selections are failing. . . is it the data source object itself, or the XPath?
  • 11-16-2006 08:41 AM In reply to

    Re: Copy data from secondary to primary data source

    I tried this:

    XDocument secondaryDOM = XDocument.GetDOM( "Produkte" );

    After opening the form I receive an error.(";" expected)

    So I did it like this:

    var secondaryDOM = XDocument.GetDOM( "Produkte" );

    This returns an object.

    After setting the namspace (like Shiva posted) I tried to select a node:

    var sec = secondaryDOM.selectNodes("/dfs:myFields/dfs:dataFields/d:Produkte");

    -> Returns Type: Object Value: {...}.

    var singleNode = secondaryDOM.selectSingleNode("/dfs:myFields/dfs:dataFields/d:Produkte/@Id")

    -> Returns Type: Object Value: null.

    Any idea?
  • 11-16-2006 09:25 AM In reply to

    Re: Copy data from secondary to primary data source

    Since you are using JScript, all variables use "var". That is why when you tried to define a variable as "XDocument" it failed.

    Since you are getting an object back, that is a good start. But all that means is that you have a valid data connection named "Produkte".

    Now you need to find out where the problem is coming from. Keep in mind that you might be getting a false positive with your "var sec" test because "selectNodes" might always returns an object, even if it doesn't find anything. So first try:

    var test = secondaryDOM.selectSingleNode("/dfs:myFields");

    If that fails then there's one of two problems:
    1. dfs:myFields is not the correct A) namespace and/or B) node name.
    2. The namespace definition you supplied is not correct according to the schema of the data connection.

    If this doesn't fail--that's good. Keep using selectSingleNode at this point, and not selectNodes. Add on dfs:dataFields and see if that works and so on until it fails.

    Maybe @Id should really be @d:Id. Check for namespaces there too.
  • 11-16-2006 02:08 PM In reply to

    Re: Copy data from secondary to primary data source

    var test = secondaryDOM.selectSingleNode("/dfs:myFields");

    -> It failed and returns null.

    I made a Screenshot of my datasource. I hope this helps a little.



    After extracting my form I found a "Produkte.xsd" and a "Produkte1.xsd".

    In "Produkte.xsd" I found this line:

    <xsd:schema targetNamespace="http://schemas.microsoft.com/office/infopath/2003/dataFormSolution" elementFormDefault="qualified" attributeFormDefault="unqualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:dfs="http://schemas.microsoft.com/office/infopath/2003/dataFormSolution" xmlns:d="http://schemas.microsoft.com/office/infopath/2003/ado/dataFields">

    <xsd:import schemaLocation="Produkte1.xsd" namespace="http://schemas.microsoft.com/office/infopath/2003/ado/dataFields"></xsd:import>


    In "Produkte1.xsd":
    <xsd:schema targetNamespace="http://schemas.microsoft.com/office/infopath/2003/ado/dataFields" elementFormDefault="unqualified" attributeFormDefault="unqualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:d="http://schemas.microsoft.com/office/infopath/2003/ado/dataFields">


    I'm not sure if I set the namespace correct??
  • 11-17-2006 09:42 AM In reply to

    Re: Copy data from secondary to primary data source

    As I look at the image of your data source, I see that you have a root node named myFields which you are not accounting for. When an XPath begins with a slash ('/'), that means to start from the root node. Since Produkte is not the root node, this will of course fail.

    I'm not sure what the namespace for myFields is, but let's assume that it is "my:". Then you would need to try something like:

    .selectSingleNode("/my:myFields");

    and then

    .selectSingleNode("/my:myFields/dfs:dataFields");

    and so on.
  • 11-30-2006 08:13 AM In reply to

    Re: Copy data from secondary to primary data source

    First of all thanks Greg for you help so far!!!!!!

    I found a solution to copy a row.

    var id = eventObj.Source.selectSingleNode("@Id");
    var vk = eventObj.Source.selectSingleNode("@VK");
    nNewLocalCacheRow = nSampleProductNode.cloneNode(true);
    nNewLocalCacheRow.selectSingleNode( "@ArtikelID" ).text = parseInt(id.text);
    nNewLocalCacheRow.selectSingleNode( "@VK" ).text = vk.text;
    XDocument.DOM.selectSingleNode( "/dfs:myFields/dfs:dataFields/d:Produkte" ).appendChild( nNewLocalCacheRow );

    nSampleProductNode ist defined in OnLoad like this:
    nSampleProductNode = XDocument.DOM.selectSingleNode( "//d:Umsatz" );

    This works fine!


    After I figured that out I noticed a new problem:

    After choosing some products I would like to update the stock of the chosen products in my sales table. So I wrote this code:

    I wrote a custom sql command so that a querry returns the chosen products. To avoid that after this querry my sales table is empty I copy the rows back after executing the querry.


    var selectedUms = XDocument.DOM.selectNodes("//d:Umsatz");
    var i=0;
    var sql = "SELECT * FROM Produkte WHERE Id= ";
    var sqlWhere;
    while (oSrc = selectedUms.nextNode()){
    var artikelID = oSrc.selectSingleNode("@ArtikelID").text;
    if (i == 0){
    sqlWhere = artikelID + " ";
    sql = sql + sqlWhere;
    }
    else {
    sqlWhere = "OR Id =" + artikelID + " ";
    sql = sql + sqlWhere;
    }
    i++;
    }

    selectedUms = XDocument.DOM.selectNodes("//d:Umsatz");
    XDocument.QueryAdapter.Command = sql;
    XDocument.Query();

    var UmsTab = XDocument.DOM.selectNodes("//d:Umsatz");
    UmsTab.removeAll();

    while (oSrc = selectedUms.nextNode()){
    nNewLocalCacheRow = nSampleProductNode.cloneNode(true);

    nNewLocalCacheRow.selectSingleNode("@ArtikelID").text = parseInt(oSrc.selectSingleNode("@ArtikelID").text);
    nNewLocalCacheRow.selectSingleNode("@VK").text = oSrc.selectSingleNode("@VK").text;
    nNewLocalCacheRow.selectSingleNode("@Nachbestellen").text = oSrc.selectSingleNode("@Nachbestellen").text;

    XDocument.DOM.selectSingleNode("/dfs:myFields/dfs:dataFields/d:Produkte").appendChild(nNewLocalCacheRow);
    }



    I looks like it works fine. But when I am trying to save the data back to my database I receive an error like:

    "structure of group "dataFields" has changed and is not similar to the structure of the data source"

    I can't see any probelm because I use a similar code to querry the data source without any error. After executing this code there is no problem to save data to my database:


    var selectedUms = XDocument.DOM.selectNodes("//d:Umsatz");
    XDocument.Query();

    var UmsTab = XDocument.DOM.selectNodes("//d:Umsatz");
    UmsTab.removeAll();

    while (oSrc = selectedUms.nextNode()){
    nNewLocalCacheRow = nSampleProductNode.cloneNode(true);
    if ((oSrc.selectSingleNode("@ArtikelID").text) != ""){
    nNewLocalCacheRow.selectSingleNode("@ArtikelID").text = oSrc.selectSingleNode("@ArtikelID").text;
    nNewLocalCacheRow.selectSingleNode("@VK").text = oSrc.selectSingleNode("@VK").text; nNewLocalCacheRow.selectSingleNode("@Nachbestellen").text = oSrc.selectSingleNode("@Nachbestellen").text;
    XDocument.DOM.selectSingleNode("/dfs:myFields/dfs:dataFields/d:Produkte").appendChild(nNewLocalCacheRow);
    }
    }
    }



    Christoph
Page 1 of 1 (8 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.