Showing posts with label connection. Show all posts
Showing posts with label connection. Show all posts

Wednesday, March 28, 2012

Trouble connecting to AS/400 with ASP.NET

Hello,

I am writing an ASP.NET application and trying to connect to an as400
using an ADODB connection. I recently completed a Windows application
that connects to the 400. My code works in the Windows world, so
theoretically, it should work for the ASP application as well. But it
doesn't. I keep getting the error CWBSY1006 - User ID is Invalid. I'm
using my Windows login information to automatically log into the 400,
so a userid and password is not required. It does work, however, if I
hard code my userid and password into the connection string. The
only problem with that is that the users need to log in with their
information for security purposes. Here is my code. Any ideas would
be greatly appreciated.

Thanks,
Amaryllis

AS400.Define("MyAS400")
AS400.Connect(cwbx.cwbcoServiceEnum.cwbcoServiceDa taQueues)
conn400 = New ADODB.Connection
conn400.ConnectionString = "Provider=IBMDA400.DataSource.1;" & _
"Data source=MyAS400"
conn400.Open()That is because ASP.NET runs under the ASPNET account. It does not run
under you. So of course, the ASPNET account does not have integrated
security access to your database.

You neither need to give that account rights to the database, or use a
username/password to log in.

"Amaryllis" <amy_hankins@.swn-dot-com.no-spam.invalid> wrote in message
news:410fe1bf$1_5@.Usenet.com...
> Hello,
> I am writing an ASP.NET application and trying to connect to an as400
> using an ADODB connection. I recently completed a Windows application
> that connects to the 400. My code works in the Windows world, so
> theoretically, it should work for the ASP application as well. But it
> doesn't. I keep getting the error CWBSY1006 - User ID is Invalid. I'm
> using my Windows login information to automatically log into the 400,
> so a userid and password is not required. It does work, however, if I
> hard code my userid and password into the connection string. The
> only problem with that is that the users need to log in with their
> information for security purposes. Here is my code. Any ideas would
> be greatly appreciated.
> Thanks,
> Amaryllis
> AS400.Define("MyAS400")
> AS400.Connect(cwbx.cwbcoServiceEnum.cwbcoServiceDa taQueues)
> conn400 = New ADODB.Connection
> conn400.ConnectionString = "Provider=IBMDA400.DataSource.1;" & _
> "Data source=MyAS400"
> conn400.Open()
I agree, but when you add <impersonate=TRUE /> to the web.config
file, it should pick up your information and use it rather than the
ASPNET account information. I've always been able to connect to SQL
Server using that approach. I'm just not sure what is different
about DB2 on the AS/400.

Amaryllis

Trouble connecting to AS/400 with ASP.NET

Hello,
I am writing an ASP.NET application and trying to connect to an as400
using an ADODB connection. I recently completed a Windows application
that connects to the 400. My code works in the Windows world, so
theoretically, it should work for the ASP application as well. But it
doesn't. I keep getting the error CWBSY1006 - User ID is Invalid. I'm
using my Windows login information to automatically log into the 400,
so a userid and password is not required. It does work, however, if I
hard code my userid and password into the connection string. The
only problem with that is that the users need to log in with their
information for security purposes. Here is my code. Any ideas would
be greatly appreciated.
Thanks,
Amaryllis
AS400.Define("MyAS400")
AS400.Connect(cwbx.cwbcoServiceEnum.cwbcoServiceDataQueues)
conn400 = New ADODB.Connection
conn400.ConnectionString = "Provider=IBMDA400.DataSource.1;" & _
"Data source=MyAS400"
conn400.Open()That is because ASP.NET runs under the ASPNET account. It does not run
under you. So of course, the ASPNET account does not have integrated
security access to your database.
You neither need to give that account rights to the database, or use a
username/password to log in.
"Amaryllis" <amy_hankins@.swn-dot-com.no-spam.invalid> wrote in message
news:410fe1bf$1_5@.mcse.ms...
> Hello,
> I am writing an ASP.NET application and trying to connect to an as400
> using an ADODB connection. I recently completed a Windows application
> that connects to the 400. My code works in the Windows world, so
> theoretically, it should work for the ASP application as well. But it
> doesn't. I keep getting the error CWBSY1006 - User ID is Invalid. I'm
> using my Windows login information to automatically log into the 400,
> so a userid and password is not required. It does work, however, if I
> hard code my userid and password into the connection string. The
> only problem with that is that the users need to log in with their
> information for security purposes. Here is my code. Any ideas would
> be greatly appreciated.
> Thanks,
> Amaryllis
> AS400.Define("MyAS400")
> AS400.Connect(cwbx.cwbcoServiceEnum.cwbcoServiceDataQueues)
> conn400 = New ADODB.Connection
> conn400.ConnectionString = "Provider=IBMDA400.DataSource.1;" & _
> "Data source=MyAS400"
> conn400.Open()
>
I agree, but when you add <impersonate=TRUE /> to the web.config
file, it should pick up your information and use it rather than the
ASPNET account information. I've always been able to connect to SQL
Server using that approach. I'm just not sure what is different
about DB2 on the AS/400.
Amaryllis

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

Saturday, March 24, 2012

Trouble with connection string in web.config

I am attempting to access a connection string from the web.config file, but I am receiving a "The ConnectionString property has not been initialized" error. If I place the connection string inline with my code it works perferctly, but when I attempt to access it via the web.config file I run into trouble. I have listed my code below which is very simple, because I am just learning .NET

Any help is greatly appreciated...

<configuration>
<appSettings>
<add key="ConnectionString" value="Server=localhost;database=msdb;Trusted_Connection=yes" />
</appSettings>
</configuration
Sub Page_Load(Sender as Object, E as EventArgs)
Dim strConnectionString as string = ConfigurationSettings.AppSettings("ConnectionString")
Dim strSQL As String = "SELECT * FROM log_shipping_databases"
Dim MyConnection as new Sqlconnection(strConnectionString)
Dim MyCommand as new SqlCommand(strSQL, MyConnection)

MyConnection.Open()
MyDataGrid.DataSource = MyCommand.ExecuteReader()
MyDataGrid.DataBind()
MyConnection.Close()
End Sub

Bump, still stuck on this problem. Any help would be greatly appreciated.
Have you had anything work from your web.config file?? The only thing I can think of is that your IIS application is not configured correctly. If this is the case, your .aspx pages will work, but IIS will not know where to look for your web.config file. This may not be the case at all....but it is my best guess at your problem. Check your site in IIS an make sure your application is configured correctly. And also double check to see that your web.config file is in the root of said application.

Hope that helps

-Jason
Jason,

Thanks for the reply. I went into IIS and made sure everything was ok, and as far as I can tell it seemed to be set up correctly. However, I deleted the existing virtual directory and created a brand new directory and set up the application again. It is still not working as intended though.

I also attempted to obtain other information that was contained in the web.config file and that did not work either.

Lastly, I am creating all my .aspx files in the root directory and that is where web.config resides as well.

Can you think of a particular setting I can check to verify everything is set up correctly? Thanks for the input thus far.

-Corey
Have you imported System.Configuration?

If you are going to use ConfigurationSettings.AppSettings("ConnectionString") then you need to import System.Configuration. I usually import System.Configuration.ConfigurationSettings and then just use AppSettings("ConnectionString") so there is less code to write.
Yes, I have also tried that as well. Below is the complete code for my project.


<%@. Page Language="VB" %>
Imports System.Configuration

<script runat="server"
Sub Page_Load(Sender as Object, E as EventArgs)
Dim strConnectionString as string = ConfigurationSettings.AppSettings("ConnectionString")
Dim strSQL As String = "SELECT * FROM log_shipping_databases"
Dim MyConnection as new System.Data.OleDb.OleDbConnection(strConnectionString)
Dim MyCommand as new System.Data.OleDb.OleDBCommand(strSQL, MyConnection)

MyConnection.Open()
MyDataGrid.DataSource = MyCommand.ExecuteReader()
MyDataGrid.DataBind()
MyConnection.Close()
End Sub

</script>
<html>
<head>
</head>
<body>
<form runat="server">
<asp:datagrid id="MyDataGrid" runat="server"></asp:datagrid>
</form>
</body>
</html


Delete any web.configs that are outside of your virtual apps.
This can cause problems.
So inside wwwroot there will be no web.config file, they will only be inside your applications folders.
HTH
Not that it matters in this case, but you got the syntax for importing a namespace wrong. It should have been:
<%@. Page Language="VB" %>
<%@. Import Namespace="System.Configuration" %>

It doesn't matter in this case, as it's not necessary. Your code, as shown, is sufficient to call information from web.config.

So, your problem is not in code. It is likely a setup problem. For example, the web.config file must go in the root folder of your Application. Like this:

c:\
\inetpub
\wwwroot
\MyApp
web.config
default.aspx

If you've placed it inside wwwroot, rather than the root of your own Application, this would cause your problem.

Hence, can you confirm that you've placed your web.config and your ASPX page in their own App folder, as shown? And that that app has been made a Web Application with IIS?
I think the following link could be useful.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconaspnetconfiguration.asp

The web.config is inheritable. This means you can have a web.config in wwwroot and in your application folder. When you call ConfigurationSettings.AppSettings("ConnectionString")
the MyApp web.config would be checked first. If that key isn't found then it would look in wwwroot. So, it is not required that you have a web.config in the app folder if you have one in wwwroot. I create a lot of applications that use a connection string so the connection string is placed in the wwwroot web.config that way it resides in one location. Then the web.config files in the app folder only hold application level settings.

Also, virtual roots or IIS applications do not affect this at all. I have tested it both ways. With or without a virtual root the web.config inheritance still works.

The last post is a good test but I don't think it's going to solve the problem. If you had a web.config in wwwroot and a connection string key in there, you wouldn't have to have a web.config in your application folder.

I actually am curious to see if the namespace correction mentioned above fixes things. SomeNewKid2, if he was using incorrect syntax on importing the namespace then I think it would matter, right? No namespace would mean no ConfigurationSettings.AppSettings exists to the application.
No, System.Configuration is a namespace that's implicitly imported with inline code. Hence, he can use ConfigurationSettings.AppSettings, without explicitly importing its namespace.

The way he had it meant it was simply considered literal text. No harm done, and it wouldn't have solved his problem even if his syntax was correct.

(I have used his own code to access a web.config. Works fine. The problem is not in his code.)
Below is where my web.config and default.aspx files reside:

c:\inetpub\wwwroot\training\web.config
c:\inetpub\wwwroot\training\default.aspx

Also, I have checked IIS to verify that it is set up as an application. I also deleted the virtual directory and setup it up as an application from scratch in IIS. I may very well be excluding a minor detail somewhere, but I can't seem to indetify it.

Thanks for all the help,
Corey
Back up your current default.aspx and web.config files. (Or just rename them temporarily.)

Try replacing your web.config with this very simple one:

<?xml version="1.0" encoding="UTF-8" ?>
<configuration
<appSettings>
<add key="ConnectionString" value="Server=localhost;database=msdb;Trusted_Connection=yes" />
</appSettings
<system.web>
</system.web
</configuration>

Now, replace your default.aspx with this modified version:

<%@. Page Language="VB" Debug="true" %
<script runat="server"
Sub Page_Load(Sender as Object, E as EventArgs)
Dim strConnectionString as string = ConfigurationSettings.AppSettings("ConnectionString")
Dim strSQL As String = "SELECT * FROM log_shipping_databases"
'Dim MyConnection as new System.Data.OleDb.OleDbConnection(strConnectionString)
'Dim MyCommand as new System.Data.OleDb.OleDBCommand(strSQL, MyConnection)

'MyConnection.Open()
'MyDataGrid.DataSource = MyCommand.ExecuteReader()
'MyDataGrid.DataBind()
'MyConnection.Close()
Message.Text = strConnectionString
End Sub

</script>
<html>
<head>
</head>
<body>
<form runat="server">
<asp:datagrid id="MyDataGrid" runat="server"></asp:datagrid>
<asp:Label id="Message" runat="server" />
</form>
</body>
</html>

This code works fine on my machine. Let me know if it works okay on yours. (It should simply show the ConnectionString value that it retrieved from web.config.)
It prints the connection string to the browser:

Server=localhost;database=msdb;Trusted_Connection=yes
Which means it worked.

Well, the .aspx file is just a simplified version of your own. Which suggests that there is merely an error in your web.config file.

Since the web.config file is an XML file, it is very sensitive to syntax and proper nesting of statements.
You probably just have a statement in the wrong place.

Do you want to post the code for your web.config file?

Tuesday, March 13, 2012

Troubles in .cs compile with unc type oledb connections strings..

I have an .cs file I need to compile with csc.exe for codebehind
access (no VS.NET). Trouble is that my oledb connection needs to use
a unc type path.. when I use the unc path in conn I get
escape sequence errors from csc.exe.. so I tried a static
mapped drive with reversed \ / from a google I found. The
.cs compiles but the conn doesn't work.. so if I can find
a method to get unc style conn to compile, I think I would be
golden. It's a known working oledb conn used all over the place
in standard asp and aspx files.. any clues into getting the
escape sequences corrected would be appreciated.. here
is what the conn would looks like of the .cs
"Provider=Advantage OLE DB Provider; Data Source=\\server\path\dir; ServerTy
pe=ADS_REMOTE_SERVER;
TableType=ADS_CDX;FilterOptions=RESPECT_
WHEN_COUNTING;"
Thanks, BobIn a C# string, backslashes are escape characters. To escape a backslash,
escape it with a backslash.
HTH,
Kevin Spencer
Microsoft MVP
.Net Developer
Neither a follower nor a lender be.
"Bob [BVP]" <bb4@.pmount.com> wrote in message
news:uzkZyQy$EHA.1296@.TK2MSFTNGP10.phx.gbl...
> I have an .cs file I need to compile with csc.exe for codebehind
> access (no VS.NET). Trouble is that my oledb connection needs to use
> a unc type path.. when I use the unc path in conn I get
> escape sequence errors from csc.exe.. so I tried a static
> mapped drive with reversed \ / from a google I found. The
> .cs compiles but the conn doesn't work.. so if I can find
> a method to get unc style conn to compile, I think I would be
> golden. It's a known working oledb conn used all over the place
> in standard asp and aspx files.. any clues into getting the
> escape sequences corrected would be appreciated.. here
> is what the conn would looks like of the .cs
> "Provider=Advantage OLE DB Provider; Data Source=\\server\path\dir;
> ServerType=ADS_REMOTE_SERVER;
> TableType=ADS_CDX;FilterOptions=RESPECT_
WHEN_COUNTING;"
> Thanks, Bob
>
Thanks.. If I have \\server\share it would be \\\\server\\share ?
"Kevin Spencer" <kevin@.DIESPAMMERSDIEtakempis.com> wrote in message
news:udIvrMz$EHA.2196@.TK2MSFTNGP14.phx.gbl...
> In a C# string, backslashes are escape characters. To escape a backslash,
> escape it with a backslash.
> --
> HTH,
> Kevin Spencer
> Microsoft MVP
> .Net Developer
> Neither a follower nor a lender be.
> "Bob [BVP]" <bb4@.pmount.com> wrote in message
> news:uzkZyQy$EHA.1296@.TK2MSFTNGP10.phx.gbl...
>

Troubles in .cs compile with unc type oledb connections strings..

I have an .cs file I need to compile with csc.exe for codebehind
access (no VS.NET). Trouble is that my oledb connection needs to use
a unc type path.. when I use the unc path in conn I get
escape sequence errors from csc.exe.. so I tried a static
mapped drive with reversed \ / from a google I found. The
..cs compiles but the conn doesn't work.. so if I can find
a method to get unc style conn to compile, I think I would be
golden. It's a known working oledb conn used all over the place
in standard asp and aspx files.. any clues into getting the
escape sequences corrected would be appreciated.. here
is what the conn would looks like of the .cs

"Provider=Advantage OLE DB Provider; Data Source=\\server\path\dir; ServerType=ADS_REMOTE_SERVER;
TableType=ADS_CDX;FilterOptions=RESPECT_WHEN_COUNT ING;"

Thanks, BobIn a C# string, backslashes are escape characters. To escape a backslash,
escape it with a backslash.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Neither a follower nor a lender be.

"Bob [BVP]" <bb4@.pmount.com> wrote in message
news:uzkZyQy$EHA.1296@.TK2MSFTNGP10.phx.gbl...
> I have an .cs file I need to compile with csc.exe for codebehind
> access (no VS.NET). Trouble is that my oledb connection needs to use
> a unc type path.. when I use the unc path in conn I get
> escape sequence errors from csc.exe.. so I tried a static
> mapped drive with reversed \ / from a google I found. The
> .cs compiles but the conn doesn't work.. so if I can find
> a method to get unc style conn to compile, I think I would be
> golden. It's a known working oledb conn used all over the place
> in standard asp and aspx files.. any clues into getting the
> escape sequences corrected would be appreciated.. here
> is what the conn would looks like of the .cs
> "Provider=Advantage OLE DB Provider; Data Source=\\server\path\dir;
> ServerType=ADS_REMOTE_SERVER;
> TableType=ADS_CDX;FilterOptions=RESPECT_WHEN_COUNT ING;"
> Thanks, Bob
Thanks.. If I have \\server\share it would be \\\\server\\share ?

"Kevin Spencer" <kevin@.DIESPAMMERSDIEtakempis.com> wrote in message
news:udIvrMz$EHA.2196@.TK2MSFTNGP14.phx.gbl...
> In a C# string, backslashes are escape characters. To escape a backslash,
> escape it with a backslash.
> --
> HTH,
> Kevin Spencer
> Microsoft MVP
> .Net Developer
> Neither a follower nor a lender be.
> "Bob [BVP]" <bb4@.pmount.com> wrote in message
> news:uzkZyQy$EHA.1296@.TK2MSFTNGP10.phx.gbl...
> > I have an .cs file I need to compile with csc.exe for codebehind
> > access (no VS.NET). Trouble is that my oledb connection needs to use
> > a unc type path.. when I use the unc path in conn I get
> > escape sequence errors from csc.exe.. so I tried a static
> > mapped drive with reversed \ / from a google I found. The
> > .cs compiles but the conn doesn't work.. so if I can find
> > a method to get unc style conn to compile, I think I would be
> > golden. It's a known working oledb conn used all over the place
> > in standard asp and aspx files.. any clues into getting the
> > escape sequences corrected would be appreciated.. here
> > is what the conn would looks like of the .cs
> > "Provider=Advantage OLE DB Provider; Data Source=\\server\path\dir;
> > ServerType=ADS_REMOTE_SERVER;
> > TableType=ADS_CDX;FilterOptions=RESPECT_WHEN_COUNT ING;"
> > Thanks, Bob