I have an InfoPath form that is bound to a schema. I am trying to implement some code in Visual Studio (C#) that will make a call to a database to initialize some fields on the form load. I have hit a wall though with the following exception:
--------------------------------------------------------------------
System.Security.SecurityException was unhandled by user code
Message="Request for the permission of type 'System.Data.SqlClient.SqlClientPermission, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed."
Source="mscorlib"
StackTrace:
at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet)
at System.Security.PermissionSet.Demand()
at System.Data.Common.DbConnectionOptions.DemandPermission()
at System.Data.SqlClient.SqlConnection.PermissionDemand()
at System.Data.SqlClient.SqlConnectionFactory.PermissionDemand(DbConnection outerConnection)
at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
at System.Data.SqlClient.SqlConnection.Open()
at AICPA.InfoPath.FormCode.GetNextMasterCode()
at AICPA.InfoPath.FormCode.SetMasterCode()
at AICPA.InfoPath.FormCode.FormEvents_Loading(Object sender, LoadingEventArgs e)
at Microsoft.Office.InfoPath.Internal.FormEventsHost.OnLoad(DocReturnEvent pEvent)
at Microsoft.Office.Interop.InfoPath.SemiTrust._XDocumentEventSink2_SinkHelper.OnLoad(DocReturnEvent pEvent)
--------------------------------------------------------------------
This code is running on my local development machine, which fully trusts assemblies by default.
Here's the function that's causing a problem:
-------------------------------------------------------------------
private string GetNextMasterCode()
{
System.Data.SqlClient.SqlConnection sqlConnection = new System.Data.SqlClient.SqlConnection("Data Source=SERVER;Initial Catalog=DATABASE;User Id=user;Password=password;");
System.Data.SqlClient.SqlCommand sqlCommand;
string sql = "";
object currentMasterCode;
sql = "SELECT MAX(mastercode) FROM MasterCodeDistribution";
sqlCommand = new System.Data.SqlClient.SqlCommand(sql, sqlConnection);
sqlConnection.Open();
currentMasterCode = (string)sqlCommand.ExecuteScalar();
sqlConnection.Close();
return currentMasterCode;
}
----------------------------------------------------------------------
I have tried both sql account authentication and trusted (domain admin).
Has anyone run into this?