none
Connecting to a database in Infopath Custom Code RRS feed

  • Question

  • I am connecting an infopath form to a database and need to do some custom code, but when i run the code I get an error at SQLConn.Open() 'open the connection

    Infopath is using the program: Microsoft Visual Studio Tools for Applications. 

    This code works perfectly in Microsoft Visual Studio, so i'm not sure what the problem is. 

    this is my code: 

    Imports System.Data
    Imports System.Data.SqlClient

                Dim ConnString As String = "Data Source=XXXXXXXX\PROJXXXXXFARM;Initial Catalog=BIReportServer;Integrated Security=True" 'Connstring = Server Name, Database Name, Windows Authentication'Write to SQL
                Dim SQLStr As String
                Dim SQLConn As New SqlConnection() 'The SQL Connection
                Dim SQLCmd As New SqlCommand() 'The SQL Command
                Dim Sqldr As SqlDataReader
                Dim uniqueRequestID As Integer

                SQLStr = "SELECT max(RequestID) AS RequestID FROM BIReportServer.dbo.TDI_Budget_Asset"

                SQLConn.ConnectionString = ConnString 'Set the Connection String
                SQLConn.Open() 'Open the connection
                SQLCmd.Connection = SQLConn 'Sets the Connection to use with the SQL Command
                SQLCmd.CommandText = SQLStr  'Sets the SQL String
                Sqldr = SQLCmd.ExecuteReader() 'Executes SQL Commands Non-Querys only

    The error I get: 

    Request for the permission of type 'System.Data.SqlClient.SqlClientPermission, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

    The exception detail is: 

    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 TemplateTest1000.FormCode.CTRL252_5_Clicked(Object sender, ClickedEventArgs e)
           at Microsoft.Office.InfoPath.Internal.ButtonEventHost.OnButtonClick(DocActionEvent pEvent)
           at Microsoft.Office.Interop.InfoPath.SemiTrust._ButtonEventSink_SinkHelper.OnClick(DocActionEvent pEvent)

    Thank you for your help. If there is any further information please ask!

    • Moved by Mike Feng Monday, July 2, 2012 6:34 AM Office related (From:Visual Basic General)
    Thursday, June 28, 2012 5:24 PM

All replies

  • I assume that the forum for Visual Studio Tools for application is more dedicated to your question.

    http://social.msdn.microsoft.com/Forums/en-US/vsto/threads


    Success
    Cor

    Friday, June 29, 2012 7:51 AM
  • 

    I am connecting an infopath form to a database and need to do some custom code, but when i run the code I get an error at SQLConn.Open() 'open the connection

    Infopath is using the program: Microsoft Visual Studio Tools for Applications. 

    This code works perfectly in Microsoft Visual Studio, so i'm not sure what the problem is. 

    this is my code: 

    Imports System.Data
    Imports System.Data.SqlClient

                Dim ConnString As String = "Data Source=XXXXXXXX\PROJXXXXXFARM;Initial Catalog=BIReportServer;Integrated Security=True" 'Connstring = Server Name, Database Name, Windows Authentication'Write to SQL
                Dim SQLStr As String
                Dim SQLConn As New SqlConnection() 'The SQL Connection
                Dim SQLCmd As New SqlCommand() 'The SQL Command
                Dim Sqldr As SqlDataReader
                Dim uniqueRequestID As Integer

                SQLStr = "SELECT max(RequestID) AS RequestID FROM BIReportServer.dbo.TDI_Budget_Asset"

                SQLConn.ConnectionString = ConnString 'Set the Connection String
                SQLConn.Open() 'Open the connection
                SQLCmd.Connection = SQLConn 'Sets the Connection to use with the SQL Command
                SQLCmd.CommandText = SQLStr  'Sets the SQL String
                Sqldr = SQLCmd.ExecuteReader() 'Executes SQL Commands Non-Querys only

    The error I get: 

    Request for the permission of type 'System.Data.SqlClient.SqlClientPermission, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

    The exception detail is: 

    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 TemplateTest1000.FormCode.CTRL252_5_Clicked(Object sender, ClickedEventArgs e)
           at Microsoft.Office.InfoPath.Internal.ButtonEventHost.OnButtonClick(DocActionEvent pEvent)
           at Microsoft.Office.Interop.InfoPath.SemiTrust._ButtonEventSink_SinkHelper.OnClick(DocActionEvent pEvent)

    Thank you for your help. If there is any further information please ask!

    Friday, June 29, 2012 12:34 PM
  • Hi kcal92,

    Show me your InfoPath version please.

    T.X.


    征诛志异,三让两家王朝
    功同开辟,一桮万古江南

    Saturday, June 30, 2012 3:14 AM
  • I am working with infopath 2010.
    Monday, July 2, 2012 12:08 PM
  • Hi kcal92,

    You mention you get the error in VSTA. Do you receive the error when previewing the InfoPath template? Or do you receive it when opening the template after it has been published?

    Is your InfoPath template set to Full Trust? If this will be an InfoPath Client template, you will also need to digitally sign your template.

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
     
    Regards,
    Ian Kavanagh
    Microsoft Online Community Support

    Thursday, July 26, 2012 8:27 PM