Anyway to Publish a InfoPath Form with C# Code and SQL Server Connections without Full Trust? - InfoPath Dev
in

InfoPath Dev

Use our Google Custom Search for best site search results.

Anyway to Publish a InfoPath Form with C# Code and SQL Server Connections without Full Trust?

Last post 07-06-2016 11:50 AM by ISUGraber. 8 replies.
Page 1 of 1 (9 items)
Sort Posts: Previous Next
  • 06-30-2016 12:37 PM

    Anyway to Publish a InfoPath Form with C# Code and SQL Server Connections without Full Trust?

    I have finished (almost) Building my first ever InfoPath form and have discovered that I can't get it to run some of my C# Code without having it set to Full Trust. For instance it won't run my OleDBConnection.Open() command unless it is in Full Trust. Although it will run Connection.Execute(). Is there a way I can put this together without Full Trust or do I need to go down the Full Trust road? I can throw my code below but every time I post something here it all comes out in 1 paragraph. So It's not easily read. But my code essentially uses a Data Connection from InfoPath modifies the SQL Behind that connection using the user information of the user that opened the form (With ADO) then runs that new query with an OLE DB SQLAdapter. Then it takes the data that is returned (This time User Name, Title, Location) and publishes them to my form. I do other queries like this in the form depending on what the user selects. And at the end I call a Stored Procedure to write the code to SQL Server. All of which works under Full Trust.... Thanks and any thoughts would help.
  • 06-30-2016 01:47 PM In reply to

    Re: Anyway to Publish a InfoPath Form with C# Code and SQL Server Connections without Full Trust?

     If you use Internet Explorer, the forum will leave your post formatting alone. Can you just use a SQL data connection in the form and modify the command for the data connection in your code? I'd guess if you did that instead of using an OleDBConnection, you wouldn't have to go full trust.

    Hilary Stoupa

  • 06-30-2016 02:01 PM In reply to

    Re: Anyway to Publish a InfoPath Form with C# Code and SQL Server Connections without Full Trust?

     This is what I attempted... I thought I was doing that but apparently not. I believe I got the start of this from a post from you a few years ago too.  I varied from it since I wanted to capture the data into a recordset and use that to loop through and send back to my form.  It fails on the OleConnection.Open(); portion.

     

    AdoQueryConnection connection =


    (AdoQueryConnection)DataConnections["AD_Mirror"];


    string customQuery = " where SamAccountName = '" + userName + "'";

     

    string queryString = origCommand + customQuery;

     

     

    DataSet oSet = new DataSet();

    // connection.Execute(); //Is this Needed?

    string conn = connection.Connection;

    // SqlDataAdapter adapter = new SqlDataAdapter(queryString, conn);

    using (OleDbConnection OleConnection = new OleDbConnection(conn))

     

     

     

    {

     

     

    OleDbCommand command = new OleDbCommand(queryString, OleConnection);

    command.CommandType = CommandType.Text;

    try

     

     

    {

     

    OleConnection.Open();

     

     

     

     

     

  • 06-30-2016 02:09 PM In reply to

    Re: Anyway to Publish a InfoPath Form with C# Code and SQL Server Connections without Full Trust?

     Interesting. So you are getting the form data connection, and it appears setting the connection string, then commented out the connection.Execute() line (which would have run the query for the data connection you already accessed) and create an OleDbConnection instead? I confess, I don't understand why.... You should be able to uncomment your connection.Execute() line, then create an XPath navigator to that data source to access the returned data....

    Hilary Stoupa

  • 07-01-2016 06:07 AM In reply to

    Re: Anyway to Publish a InfoPath Form with C# Code and SQL Server Connections without Full Trust?

     That's one thing I couldn't figure out. The connection.Execute() does work. But how do I get data from it and pass it to my forms fields?  The only way I could get this to work is what you see in my code. 

     If I could get the connection.Execute() to work then that would be nice.

  • 07-01-2016 08:01 AM In reply to

    Re: Anyway to Publish a InfoPath Form with C# Code and SQL Server Connections without Full Trust?

    You'll need to create an XPath navigator for the data source - just roughing this, so it may need some clean up - but XPathNavigator adData = this.DataSources["AD_Mirror"].CreateNavigator() should get you the root of the returned data - then you can use SelectSingleNode or Select to get node(s) back....
    Hilary Stoupa

  • 07-01-2016 09:23 AM In reply to

    Re: Anyway to Publish a InfoPath Form with C# Code and SQL Server Connections without Full Trust?

      Thanks for the tips and I believe this is working.  But since I am a newbie to InfoPath and XPathNavigators I have run into a stumbling block.  The execute works.  And it looks like I'm grabbing the data I want right now.  Although this is a basic example of what I'm doing and will need loops in the future.  But I digress.  The below "testing" string I get pulls in a long list of data in XML it looks like.  How can I filter through that and only grab 3 fields?  Right now I just want Last_Name, First_Name and Title. 

     

    XPathNavigator QVData = this.DataSources["AD_Mirror"].CreateNavigator();



    string Testing = QVData.OuterXml.ToString();

     

    "<dfs:myFields xmlns:dfs=\"\r\n\thttp://schemas.microsoft.com/office/infopath/2003/dataFormSolution\"><dfs:dataFields>\r\n\t<d:AD_Mirror xmlns:d=\"http://schemas.microsoft.com/office/infopath/2003/ado/dataFields\" Employee_ID=\"0\" TheDomain=\"WORK\" AccountName=\"MYACCOUNT\" WorkType=\"ZZZ\" Work=\"55555\" Unit=\"55555\" Area=\"ZZZ\" AreaNbr=\"55555\" Last_Name=\"LASTNAME                                                      \" First_Name=\"FIRSTNAME                                                    \" Title=\"TITLE BI\"/>\r\n</dfs:dataFields></dfs:myFields>\r\n"

  • 07-01-2016 10:01 AM In reply to

    Re: Anyway to Publish a InfoPath Form with C# Code and SQL Server Connections without Full Trust?

     XPathNodeIterator dataNodes = QVData.Select("/dfs:myFields/dfs:dataFields/d:AD_Mirror", this.NamespaceManager);

    Should get you an iterator - then you can loop through them - you may need to right click AD_Mirror in the fields taskpane in the designer and select copy XPath to make sure the XPath is right above...

    Then you can do stuff like

    foreach(XPathNavigator node in dataNodes)

    {

     string lastName= node.SelectSingleNode("Last_Name", this.NamespaceManager).Value;

    Etc - again, this is just rough - Intellisense in VS should help correct anything I've fat fingered. You may have to check the XPaths, since the snippet of the returned XML above is a little hard to read.

    Hilary Stoupa

  • 07-06-2016 11:50 AM In reply to

    Re: Anyway to Publish a InfoPath Form with C# Code and SQL Server Connections without Full Trust?

     Thank you very much for your insight and help. Your suggestions worked and I was able to get my form working!

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