I am unable to establish connection with my database through Custom Code. I have referenced to System.Data and System.Configuration and defined the assemblies permissions in the
vb page code like this:
Code:
Imports Microsoft.Reporting.WebForms
Imports System.Reflection
Imports System.Security.Policy
Imports System.Security.Permissions
Partial Class Practice_Reports_Statistics
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Dim customAssemblyName As String = "TEStatistics, Version=1.0.0.0, Culture=neutral, PublicKeyToken=1c14b99380c4e9b1"
Dim customAssemblyName2 As String = "System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
Dim customAssemblyName3 As String = "System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
Dim customAssembly As Assembly = Assembly.Load(customAssemblyName)
Dim customAssembly2 As Assembly = Assembly.Load(customAssemblyName2)
Dim customAssembly3 As Assembly = Assembly.Load(customAssemblyName3)
Dim assemblyStrongName As StrongName = CreateStrongName(customAssembly)
Dim assemblyStrongName2 As StrongName = CreateStrongName(customAssembly2)
Dim assemblyStrongName3 As StrongName = CreateStrongName(customAssembly3)
Dim reportEngine As LocalReport = ReportViewer1.LocalReport
reportEngine.AddFullTrustModuleInSandboxAppDomain(assemblyStrongName)
reportEngine.AddFullTrustModuleInSandboxAppDomain(assemblyStrongName2)
reportEngine.AddFullTrustModuleInSandboxAppDomain(assemblyStrongName3)
End Sub
Private Shared Function CreateStrongName(ByVal assembly As Assembly) As StrongName
Dim assemblyName As AssemblyName = assembly.GetName()
If assemblyName Is Nothing Then
Throw New InvalidOperationException("Could not get assemmbly name")
End If
Dim publickey As Byte() = assemblyName.GetPublicKey()
If publickey Is Nothing OrElse publickey.Length = 0 Then
Throw New InvalidOperationException("Assembly is not strongly named")
End If
Dim keyblob As New StrongNamePublicKeyBlob(publickey)
Return New StrongName(keyblob, assemblyName.Name, assemblyName.Version)
End Function
End Class
Then in the Report Custom Code:
Code:
Public Shared Function TestFer() as string
Dim myconnection As New System.Data.SqlClient.SqlConnection()
myconnection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings("ApplicationServices").ConnectionString
Dim myCommand As New System.Data.SqlClient.SqlCommand
myCommand.Connection = myconnection
myCommand.CommandText = "SELECT Classification FROM Accounts WHERE AccountID = @UserId"
myCommand.Parameters.AddWithValue("@UserId", 1)
dim msg as string = "Unable to open database"
Try
myconnection.open()
Catch ex As Exception
return msg
Finally
myconnection.close()
End Try
End Function
The report output textboxt then reads "Unable to open database". I'm still not able to connect to the database using the literal connection string.
Please Advise, Thanks!!