Trouble opening a connection to SQL 2000 DB

G

Guest

I'm developing an intranet site in ASP.NET 2.0 but I can't seem to
connect to the DB from within my code. I've created a .vb class that
houses a private Connection() that other functions within the class can
call to connect to the database. In the calling function, I've declared
my connection object and called the "Open" method on the object.
However, when I attempt to execute the stored procedure command by
calling the "ExecuteScalar" method, I get the following error:

"ExecuteScalar requires an open and available Connection. The
connection's current state is closed."


Here's the code from my class:


Imports System.Data
Imports System.Data.SqlClient


Namespace Encompass


Public Class EncompassSecurity


Public Shared Function GetHRIDByNTUserID(ByVal strNTUserID) As String


Dim strHRID As String


'Create command object


Dim cmd As New SqlCommand("usp_Get_HRID_By_NTUserID", Connection())
cmd.CommandType = CommandType.StoredProcedure


'Open DB connection


Dim DBConnection As SqlConnection = Connection()
DBConnection.Open()


'Input parameters


Dim inNTUserParam As New SqlParameter("@NT_UserID", SqlDbType.VarChar)
inNTUserParam.Direction = ParameterDirection.Input


inNTUserParam.Value = strNTUserID


cmd.Parameters.Add(inNTUserParam)


'Output parameters


Dim outHRIDParam As New SqlParameter("@HRID", SqlDbType.Int)
outHRIDParam.Direction = ParameterDirection.Output


cmd.Parameters.Add(outHRIDParam)


'Run stored procedure
strHRID = cmd.ExecuteScalar()


Return (strHRID)


'Close DB connection
DBConnection.Close()


End Function


Private Shared Function Connection() As SqlConnection


Dim strConnectionString As String
strConnectionString = ConfigurationManager.ConnectionStrings(


"Conn").ConnectionString


Return New SqlConnection(strConnectionString)


End Function


End Class
End


Namespace
Here's the code from my web.config file:


<?


xml version="1.0"?>
<!--


Note: As an alternative to hand editing this file you can use the


web admin tool to configure settings for your application. Use


the Website->Asp.Net Configuration option in Visual Studio.


A full list of settings and comments can be found in


machine.config.comments usually located in


\Windows\Microsoft.Net\Framework\v2.x\Config


-->


<


configuration
xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<


connectionStrings>
<


add name="Conn" connectionString="Data Source=ServerName;Initial
Catalog=NPASDV;uid=UserName;password=*******;"


providerName="System.Data.SqlClient" />
</


connectionStrings>


<system.web>
<!--


Set compilation debug="true" to insert debugging


symbols into the compiled page. Because this


affects performance, set this value to true only


during development.


Visual Basic options:


Set strict="true" to disallow all data type conversions


where data loss can occur.


Set explicit="true" to force declaration of all variables.


-->
<


roleManager defaultProvider="AspNetWindowsTokenRoleProvider" />
<


compilation debug="true" strict="false" explicit="true" />
<


pages>
<


namespaces>
<


clear />
<


add namespace="System" />
<


add namespace="System.Collections" />
<


add namespace="System.Collections.Specialized" />
<


add namespace="System.Configuration" />
<


add namespace="System.Text" />
<


add namespace="System.Text.RegularExpressions" />
<


add namespace="System.Web" />
<


add namespace="System.Web.Caching" />
<


add namespace="System.Web.SessionState" />
<


add namespace="System.Web.Security" />
<


add namespace="System.Web.Profile" />
<


add namespace="System.Web.UI" />
<


add namespace="System.Web.UI.WebControls" />
<


add namespace="System.Web.UI.WebControls.WebParts" />
<


add namespace="System.Web.UI.HtmlControls" />
</


namespaces>
</


pages>
<!--


The <authentication> section enables configuration


of the security authentication mode used by


ASP.NET to identify an incoming user.


-->
<


authentication mode="Windows" />


<!--


The <customErrors> section enables configuration


of what to do if/when an unhandled error occurs


during the execution of a request. Specifically,


it enables developers to configure html error pages


to be displayed in place of a error stack trace.


<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">


<error statusCode="403" redirect="NoAccess.htm" />


<error statusCode="404" redirect="FileNotFound.htm" />


</customErrors>


-->
</


system.web>
</


configuration>
What am I doing wrong? Any help would be most appreciated!!
 
W

Winista

You have created a Commad object with a connection object which is not open
yet.

Dim DBConnection As SqlConnection = Connection()
DBConnection.Open()

Dim cmd As New SqlCommand("usp_Get_HRID_By_NTUserID", DBConnection)
cmd.CommandType = CommandType.StoredProcedure
 
T

tdavisjr

Winista said:
You have created a Commad object with a connection object which is not open
yet.

Dim DBConnection As SqlConnection = Connection()
DBConnection.Open()

Dim cmd As New SqlCommand("usp_Get_HRID_By_NTUserID", DBConnection)
cmd.CommandType = CommandType.StoredProcedure


A command object can be created without the connection being opened.
However, your code is partially correct. The only thing that needs to
be changed is the call to DBConnection.Open() should be made right
before the cmd.ExecuteScalar() call.
 
G

Guest

--
Manuel



Winista said:
You have created a Commad object with a connection object which is not open
yet.

Dim DBConnection As SqlConnection = Connection()
DBConnection.Open()

Dim cmd As New SqlCommand("usp_Get_HRID_By_NTUserID", DBConnection)
cmd.CommandType = CommandType.StoredProcedure
 
G

Guest

Thanks for you help too!
--
Manuel



tdavisjr said:
A command object can be created without the connection being opened.
However, your code is partially correct. The only thing that needs to
be changed is the call to DBConnection.Open() should be made right
before the cmd.ExecuteScalar() call.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top