Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > .NET 4 and Visual Studio 2010 > BOOK: Professional Visual Studio 2010
|
BOOK: Professional Visual Studio 2010
This is the forum to discuss the Wrox book Professional Visual Studio 2010 by Nick Randolph, David Gardner, Chris Anderson, Michael Minutillo; ISBN: 9780470548653
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional Visual Studio 2010 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 28th, 2013, 11:49 AM
Authorized User
 
Join Date: Apr 2012
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 30: Report Custom Code - SqlConnection - Unable to Open Database

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!!

Last edited by fbuxo; March 28th, 2013 at 12:31 PM..





Similar Threads
Thread Thread Starter Forum Replies Last Post
Unable to open sample code downloaded williamgcl BOOK: Beginning Microsoft Visual Studio LightSwitch Development 0 August 17th, 2011 02:53 AM
Problem when SqlConnection.Open() hoetat C# 7 July 6th, 2008 07:43 PM
Report Manager - unable to open Reshmacb BOOK: Professional SQL Server Reporting Services ISBN: 0-7645-6878-7 0 October 23rd, 2007 02:22 AM
Unable to open report exported in Crystal rep jdaulagar Beginning VB 6 3 June 4th, 2007 08:06 AM
Access is unable to open database window amerk20 Access 1 April 21st, 2005 11:16 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.