How do I bind a list box to an existing dataset? - InfoPath Dev
in

InfoPath Dev

How do I bind a list box to an existing dataset?

Last post 06-03-2010 06:59 PM by rbemiller. 17 replies.
Page 1 of 2 (18 items) 1 2 Next >
Sort Posts: Previous Next
  • 06-02-2010 01:27 PM

    How do I bind a list box to an existing dataset?

    Hi all, I'm a newbie to this forum.  Great stuff on here. 

    I'm trying to ad a list box to a web enabled form.  The form currently has a User ID text box that serves as a lookup/search field.  Currently it triggers a sql query which returns all Users that are LIKE the searched User ID...but I need to make it so that when the user enters a User ID, then clicks the lookup button, a list box gets populated with the results from this search.  The query is already in place and is done in code-behind.

    I'm not sure how to ad a list box...that is, what do I bind it to?  I've read that I should create a secondary data source, but the data is already there for me in the code...so creating a new data source for that seems wrong.  And even if it's not, I don't know how to bind the list box to this dataset. 

    Let me know if some code or screenshots would be helpful to answer this.  Thanks for any help!

    Filed under:
  • 06-02-2010 02:13 PM In reply to

    Re: How do I bind a list box to an existing dataset?

    I think you will need a secondary datasource that you will link the list box on. I suggest to create a xml file template with no data in it.

    example :

    <?xml version="1.0" encoding="UTF-8" ?>

    - <options>
    -    <option>
            <value />
            <displayname />
      </option>
    -    <option>
            <value />
            <displayname />
      </option>
    </options>
     
    Then you will need to populate this xml file with the data you will have recovered from your database.
     
    public void YourFunction(){

       RemoveItems("GetApproversLvl1");

       foreach (SPListItem oListItemSrc in collListItemsSrc)

       {

          string approver = oListItemSrc.GetFormattedValue("Approver");

          string username = oListItemSrc.GetFormattedValue("Username");

          AddItem(username, approver, "GetSupervisors");

       }

    }

    private void RemoveItems(string dataConnection)

    {

       XPathNavigator DOM = DataSources[dataConnection].CreateNavigator();

       XPathNavigator group1 = DOM.SelectSingleNode("//options", NamespaceManager);   while (group1.MoveToFirstChild())

       {

          group1.DeleteSelf();

       }

    }

    private void AddItem(string itemId, string itemName, string dataConnection)

    {

       XPathNavigator DOM = DataSources[dataConnection].CreateNavigator();

       XPathNavigator DOM2 = DataSources["template"].CreateNavigator();

       XPathNavigator group1 = DOM.SelectSingleNode("//options", NamespaceManager);

       XPathNavigator field1 = DOM2.SelectSingleNode("//options/option", NamespaceManager);

       XPathNavigator newNode = field1.Clone();

       newNode.SelectSingleNode("value").SetValue(itemId);

       newNode.SelectSingleNode("displayname").SetValue(itemName);

       group1.AppendChild(newNode);

    }

     

    Hope this help

     

  • 06-02-2010 06:00 PM In reply to

    Re: How do I bind a list box to an existing dataset?

    Thanks very much Mgarant.  I think this looks like a good start.  I'll try this and see where I get.

  • 06-03-2010 09:06 AM In reply to

    Re: How do I bind a list box to an existing dataset?

    Okay, I've finally been able to get back to this and I'm having trouble.  I have created the empty xml file (PatientData.xml), now I need to populate it with my dataset.  It looks like in this example this is getting its data from a sharepoint list, but my datasource is a SQL Stored Procedure.  I'm not sure how to make it work for my needs.  Any help is appreciated.  Thanks!

    Here's the code block where I need to do this:

    ...
    //Make call to SP
    adpt = new SqlDataAdapter(cmd);
    DataTable patientTable = new DataTable();
    adpt.Fill(patientTable);

    //Make sure not more than one patient
    if (patientTable.Rows.Count > 1)
    {
    // here's where I need to populate the xml for the secondary datasource, and then
    }

  • 06-03-2010 10:35 AM In reply to

    Re: How do I bind a list box to an existing dataset?

    You just need to go through all the data inside your dataset with a foreach loop and then populate your list with the AddItems function.


    foreach (DataRow dataRow in patientTable.Rows)
    {
         string approver = dataRow.Item[<the ID of the data you want to recover>].toString();

         string username = dataRow.Item[<the ID of the data you want to recover>].toString();

         AddItems(approver, username, <dataconnection>);

    }

  • 06-03-2010 11:17 AM In reply to

    Re: How do I bind a list box to an existing dataset?

    Okay, thanks.  I'm on the right track now...but still getting some errors.  See my comments to the right of the code.

    The xml file is called PatientData.xml and I created a datasource called PatientData that references the xml file. 

    RemoveItems("PatientData");

                            foreach (DataRow dataRow in patientTable.Rows)
                            {
                                 string PatientID = dataRow.Item[patientId].toString(); // here and on the next line I'm getting an error at the .Item part of dataRow.Item[...  the error: System.data.datarow doesn't contain a definion for Item

                                 string PatientLName = dataRow.Item[patientLastName].toString();

                                 AddItem(PatientID, PatientLName, PatientData); // and here I get the error: the name PatientData does not exist in the current context
                            }

    ... then...

    private void RemoveItems(string dataConnection)
            {
                XPathNavigator DOM = DataSources[dataConnection].CreateNavigator();
                XPathNavigator group1 = DOM.SelectSingleNode("//options", NamespaceManager); while (group1.MoveToFirstChild())
                {
                    group1.DeleteSelf();
                }
            }

            private void AddItem(string itemId, string itemName, string dataConnection)
            {

                XPathNavigator DOM = DataSources[dataConnection].CreateNavigator();
                XPathNavigator DOM2 = DataSources["PatientData"].CreateNavigator();

                XPathNavigator group1 = DOM.SelectSingleNode("//options", NamespaceManager);
                XPathNavigator field1 = DOM2.SelectSingleNode("//options/option", NamespaceManager);

                XPathNavigator newNode = field1.Clone();
                newNode.SelectSingleNode("value").SetValue(itemId);

                newNode.SelectSingleNode("displayname").SetValue(itemName);
                group1.AppendChild(newNode);

            } 

     

  • 06-03-2010 11:29 AM In reply to

    Re: How do I bind a list box to an existing dataset?

    For your second error,

    AddItem(PatientID, PatientLName, "PatientData"); // and here I get the error: the name PatientData does not exist in the current context

    You forgot your "".

     For your first error,

    Do you use Visual Studio?? If so try to write the code and see what visual studio suggest you went you type dataRow. and try to find the list of items inside the row. It should be named something like Items or anything refering to data.

  • 06-03-2010 11:54 AM In reply to

    Re: How do I bind a list box to an existing dataset?

    Okay I may have it now using dataRow.Field.  See below...

    Next thing I noticed is that when trying to bind my list box to the PatientData datasource, I get the error that "a list box cannot be bound to a group or repeating field..."  What should I bind it to?

    foreach (DataRow dataRow in patientTable.Rows)
                            {
                                 string PatientID = dataRow.Field<string>("patientId");

                                 string PatientLName = dataRow.Field<string>("patientLastName");

                                 AddItem(PatientID, PatientLName, "PatientData");
                            }

  • 06-03-2010 12:00 PM In reply to

    Re: How do I bind a list box to an existing dataset?

    How did you bind your data to the list box? because i've just tried to bind a list box to an xml dataconnection and it is working perfectly...

  • 06-03-2010 12:36 PM In reply to

    Re: How do I bind a list box to an existing dataset?

    I went to Manage Data Connections, then added a data connection > new connection to receive data > XML Document > selected my PatientData.xml file > selected Access data from the specified location

    Then when I add the list box, I get the automatic List Box Binding pop-up > then select the PatientData (secondary) data source > then no matter which node or field I select I get the error.

    Here's the PatientData.xml file:

    <?xml version="1.0" encoding="utf-8"?>
    <options>
      <option>
        <value />
        <displayname />
      </option>
      <option>
        <value />
        <displayname />
      </option>
    </options>

  • 06-03-2010 12:42 PM In reply to

    Re: How do I bind a list box to an existing dataset?

    Ok, when you create the data connection, check "Include the data as a resource file in the form template or template part" instead of "Access the data from the specified location".

     It should solve the problem

  • 06-03-2010 12:49 PM In reply to

    Re: How do I bind a list box to an existing dataset?

    Tried that, unfortunatel it didn't resolve my issue.  I get the same errors.

     

  • 06-03-2010 01:00 PM In reply to

    Re: How do I bind a list box to an existing dataset?

    Look to what kind of Data Source you list box is bind to. Look in the right panel on infopath -> Data Source then select your list box and see to wich of the data source it is bind. If it is a repeating group try to delete both the list box and data source and recreate the list box and let infopath create the data source for you.

  • 06-03-2010 01:33 PM In reply to

    Re: How do I bind a list box to an existing dataset?

    It was bound to a repeating group...the datasource I created.  I deleted both the datasource and the list box.  It doesn't create a data source for me automatically...when I ad a list box it pops up the window to select the binding...there's no option to have it create a data source for me.

  • 06-03-2010 01:54 PM In reply to

    Re: How do I bind a list box to an existing dataset?

    Ok... what version of infopath do you use? Any way, just create a data source that is not repeating and you should be ok... i guess...

Page 1 of 2 (18 items) 1 2 Next >
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.