Showing posts with label class. Show all posts
Showing posts with label class. Show all posts

Wednesday, March 28, 2012

Trouble creating an array of a class I created

What am I doing wrong? I made a class and want to create an array of that class and I keep getting "object reference not set ot an instance of an object" when the code hits line 4. Tile is the class I made

1private void usrTurnTile_Load(object sender, EventArgs e)2{3Tile[] objTile =new Tile[20];4objTile[3].Visible =false;5}

adding this fixed it...

for (int i23 = 0; i23 < 20; i23++)

objTile[i23] =newTile();


adding this fixed it...

for (int i23 = 0; i23 < 20; i23++)

objTile[i23] =newTile();


adding this fixed it...

for (int i23 = 0; i23 < 20; i23++)

objTile[i23] =newTile();

Monday, March 26, 2012

Trouble opening a connection to SQL 2000 DB

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("@dotnet.itags.org.NT_UserID", SqlDbType.VarChar)
inNTUserParam.Direction = ParameterDirection.Input
inNTUserParam.Value = strNTUserID
cmd.Parameters.Add(inNTUserParam)
'Output parameters
Dim outHRIDParam As New SqlParameter("@dotnet.itags.org.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!!
Manuel Garr, MCPYou 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
"mlg1906" <mlg1906@.discussions.microsoft.com> wrote in message
news:86C67AB7-8F4E-42A4-B0C8-CB7FC7DE7CD6@.microsoft.com...
> 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!!
>
> --
> Manuel Garr, MCP
>
Winista wrote:
> You have created a Commad object with a connection object which is not ope
n
> 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.
> "mlg1906" <mlg1906@.discussions.microsoft.com> wrote in message
> news:86C67AB7-8F4E-42A4-B0C8-CB7FC7DE7CD6@.microsoft.com...
--
Manuel
"Winista" wrote:
> You have created a Commad object with a connection object which is not ope
n
> yet.
> Dim DBConnection As SqlConnection = Connection()
> DBConnection.Open()
> Dim cmd As New SqlCommand("usp_Get_HRID_By_NTUserID", DBConnection)
> cmd.CommandType = CommandType.StoredProcedure
> "mlg1906" <mlg1906@.discussions.microsoft.com> wrote in message
> news:86C67AB7-8F4E-42A4-B0C8-CB7FC7DE7CD6@.microsoft.com...
Thanks for you help too!
--
Manuel
"tdavisjr" wrote:
> Winista wrote:
>
> 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.
>
>
>
Thanks so much for your help. It's working now.
--
Manuel
"Winista" wrote:
> You have created a Commad object with a connection object which is not ope
n
> yet.
> Dim DBConnection As SqlConnection = Connection()
> DBConnection.Open()
> Dim cmd As New SqlCommand("usp_Get_HRID_By_NTUserID", DBConnection)
> cmd.CommandType = CommandType.StoredProcedure
> "mlg1906" <mlg1906@.discussions.microsoft.com> wrote in message
> news:86C67AB7-8F4E-42A4-B0C8-CB7FC7DE7CD6@.microsoft.com...

Trouble opening a connection to SQL 2000 DB

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("@dotnet.itags.org.NT_UserID", SqlDbType.VarChar)
inNTUserParam.Direction = ParameterDirection.Input

inNTUserParam.Value = strNTUserID

cmd.Parameters.Add(inNTUserParam)

'Output parameters

Dim outHRIDParam As New SqlParameter("@dotnet.itags.org.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!!

--
Manuel Garr, MCPYou 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

"mlg1906" <mlg1906@.discussions.microsoft.com> wrote in message
news:86C67AB7-8F4E-42A4-B0C8-CB7FC7DE7CD6@.microsoft.com...
> 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!!
>
> --
> Manuel Garr, MCP
Winista wrote:
> 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.

> "mlg1906" <mlg1906@.discussions.microsoft.com> wrote in message
> news:86C67AB7-8F4E-42A4-B0C8-CB7FC7DE7CD6@.microsoft.com...
> > 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!!
> > --
> > Manuel Garr, MCP
Thanks for you help too!
--
Manuel

"tdavisjr" wrote:

> Winista wrote:
> > 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.
>
>
> > "mlg1906" <mlg1906@.discussions.microsoft.com> wrote in message
> > news:86C67AB7-8F4E-42A4-B0C8-CB7FC7DE7CD6@.microsoft.com...
> > > 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>
> > > <!--
> >
--
Manuel

"Winista" wrote:

> 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
> "mlg1906" <mlg1906@.discussions.microsoft.com> wrote in message
> news:86C67AB7-8F4E-42A4-B0C8-CB7FC7DE7CD6@.microsoft.com...
> > 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.
> > -->
> > <
Thanks so much for your help. It's working now.
--
Manuel

"Winista" wrote:

> 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
> "mlg1906" <mlg1906@.discussions.microsoft.com> wrote in message
> news:86C67AB7-8F4E-42A4-B0C8-CB7FC7DE7CD6@.microsoft.com...
> > 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.
> > -->
> > <

Tuesday, March 13, 2012

Troubles reference this pointer in external class file

I'm wanting to reference page controls from a cs file in the app_code
directory. I'm getting a _Defaullt could not be found, missing
directive or assembly reference error. Very simple example below.
Default.aspx.cs:
using System;
using System.Web.UI;
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Class1.UpdateButtonText(this);
}
}
Class1.cs
using System;
public class Class1
{
public Class1()
{
}
public static void UpdateButtonText(_Default global_Default)
{
}
}
Peter Kellner
http://peterkellner.netThis is impossible as the App_Code directory is compiled prior to your aspx
files.
-Brock
http://staff.develop.com/ballen

> I'm wanting to reference page controls from a cs file in the app_code
> directory. I'm getting a _Defaullt could not be found, missing
> directive or assembly reference error. Very simple example below.
> Default.aspx.cs:
> using System;
> using System.Web.UI;
> public partial class _Default : Page
> {
> protected void Page_Load(object sender, EventArgs e)
> {
> }
> protected void Button1_Click(object sender, EventArgs e)
> {
> Class1.UpdateButtonText(this);
> }
> }
> --
> Class1.cs
> using System;
> public class Class1
> {
> public Class1()
> {
> }
> public static void UpdateButtonText(_Default global_Default)
> {
> }
> }
> Peter Kellner
> http://peterkellner.net
On Fri, 9 Jun 2006 23:24:03 +0000 (UTC), Brock Allen
<ballen@.NOSPAMdevelop.com> wrote:

>This is impossible as the App_Code directory is compiled prior to your aspx
>files.
>-Brock
>http://staff.develop.com/ballen
>
Thanks,
Makes sense to me now. When I moved Class2 directly into the aspx.cs
file it of course worked. Hadn't really thought about the order of
compilation.
-peter
----
using System;
using System.Web.UI;
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Class2.UpdateButtonText(this);
}
}
public class Class2
{
public Class2()
{
}
public static void UpdateButtonText(_Default global_Default)
{
throw new Exception("The method or operation is not
implemented.");
}
}
Peter Kellner
http://peterkellner.net

Troubles reference this pointer in external class file

I'm wanting to reference page controls from a cs file in the app_code
directory. I'm getting a _Defaullt could not be found, missing
directive or assembly reference error. Very simple example below.

Default.aspx.cs:

using System;
using System.Web.UI;

public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
}

protected void Button1_Click(object sender, EventArgs e)
{
Class1.UpdateButtonText(this);
}
}

------

Class1.cs

using System;

public class Class1
{
public Class1()
{
}

public static void UpdateButtonText(_Default global_Default)
{
}
}
Peter Kellner
http://peterkellner.netThis is impossible as the App_Code directory is compiled prior to your aspx
files.

-Brock
http://staff.develop.com/ballen

> I'm wanting to reference page controls from a cs file in the app_code
> directory. I'm getting a _Defaullt could not be found, missing
> directive or assembly reference error. Very simple example below.
> Default.aspx.cs:
> using System;
> using System.Web.UI;
> public partial class _Default : Page
> {
> protected void Page_Load(object sender, EventArgs e)
> {
> }
> protected void Button1_Click(object sender, EventArgs e)
> {
> Class1.UpdateButtonText(this);
> }
> }
> ------
> Class1.cs
> using System;
> public class Class1
> {
> public Class1()
> {
> }
> public static void UpdateButtonText(_Default global_Default)
> {
> }
> }
> Peter Kellner
> http://peterkellner.net
On Fri, 9 Jun 2006 23:24:03 +0000 (UTC), Brock Allen
<ballen@.NOSPAMdevelop.com> wrote:

>This is impossible as the App_Code directory is compiled prior to your aspx
>files.
>-Brock
>http://staff.develop.com/ballen
>
Thanks,

Makes sense to me now. When I moved Class2 directly into the aspx.cs
file it of course worked. Hadn't really thought about the order of
compilation.

-peter

----------------

using System;
using System.Web.UI;

public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
}

protected void Button1_Click(object sender, EventArgs e)
{
Class2.UpdateButtonText(this);
}
}

public class Class2
{
public Class2()
{
}

public static void UpdateButtonText(_Default global_Default)
{
throw new Exception("The method or operation is not
implemented.");
}
}
Peter Kellner
http://peterkellner.net