3

I've been working on an infopath form to migrate infopath 2007 to infopath 2013. To bind data to DropDownList controls FileQueryConnection has been used.

 // Retrieve the data connection bound to the Manager drop-down list box
 FileQueryConnection institutionConnection =(FileQueryConnection)DataConnections[ExternalUsersDC];
 // returned by the owssvr.dll with a filter on External Users of Institution
 institutionConnection.FileLocation = GetFileLocation(currentSite, externalUsersGuid, ExternalUserInstitution, institution);
 // Query the data connection to fill the Manager drop-down list box with items
 institutionConnection.Execute();

Here ExternalUsersDC is the name of the infopath connection file. GetFileLocation method gets the list physical location which works fine as expected.

Casting error occurs while trying to DataConnection to FileQueryConnection. Error message as follows;

Unable to cast object of type 'Microsoft.Office.InfoPath.Internal.SharePointListAdapterRWQueryAdapterHost' to type 'Microsoft.Office.InfoPath.FileQueryConnection

I searched everywhere to find a reason and failed. If someone has experience with this issue, please shed some light on my path.

anbuj
  • 499
  • 4
  • 15

1 Answers1

2

Try AS operator. It will try to cast to appropriate type.If Casting is not possible it will fail gracefully by returning NULL.

    FileQueryConnection institutionConnection =DataConnections[ExternalUsersDC] as FileQueryConnection;
 // returned by the owssvr.dll with a filter on External Users of Institution
 institutionConnection.FileLocation = GetFileLocation(currentSite, externalUsersGuid, ExternalUserInstitution, institution);
 // Query the data connection to fill the Manager drop-down list box with items
 institutionConnection.Execute();
Aslam Jiffry
  • 1,306
  • 6
  • 23
  • 57
  • Thanks for answering. Of course it's returning null, I need to know the correct casting for this or an alternative. – anbuj Aug 05 '15 at 10:25
  • You can't cast two different objects (Which is not inherited.). For example you can't cast an integer to string.But you can cast an object to string , because string is also object.So Make sure that FileQueryConnection is a type of DataConnection Before you cast. If that is not the case you cannot cast. – Aslam Jiffry Aug 05 '15 at 10:29
  • Please refer https://msdn.microsoft.com/en-us/library/office/Microsoft.Office.InfoPath.FileQueryConnection.aspx They have done the casting FileQueryConnection myDataSource = (FileQueryConnection)(DataSources["XMLFile"].QueryConnection); This casting also throws the same exception. – anbuj Aug 05 '15 at 11:17
  • @anbuj.Microsoft.Office.InfoPath.DataConnection class is parent of Microsoft.Office.InfoPath.FileQueryConnection class. In that case you can cast directly as your code. But the error throws here because of above classes implementation might be different.Did you check version of above classes you are using. Is it the latest implementation of those classes ?. Following link may be helpful to you. http://www.nullskull.com/q/10351669/infopath-2007-and-c-programming.aspx – Aslam Jiffry Aug 05 '15 at 11:27