Upgr asp.net/ado.net connection method from VS 2003 to 2005

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Am upgrading SQL server access app from 2003 to 2005. Old code works but I
can see safer methods:
My web.config (ASP.Net 1.1)
<configuration>
<appSettings>
<add key="ConnectString" value="Data Source=Vaio1;Initial Catalog=IMA;User
ID=sa;Password="/>
<add key="ImagePath" value="/IMA/Images/"/>
</appSettings> etc

and vb code to open in page:
Private ConnectString As String =
ConfigurationSettings.AppSettings("ConnectString")
Private invConn As SqlConnection
Private invCom As SqlCommand
Private objDR As SqlClient.SqlDataReader
and in
invConn = New SqlConnection(ConnectString)
invConn.Open()
invCom = New SqlCommand("iUserCheck", invConn)
invCom.CommandType = CommandType.StoredProcedure

I realise I need to encrypt this but want to upgrade this to ASP.net ver 2.
Old code works fine but realise it is out of date.
 
Hi,
Keeping unencrypted connection strings in your web.config file isn't ideal.
Fortunately, in addition to reading connection strings from a configuration
file, you can also use the Configuration APIs to encrypt individual sections
so that confidential information such as connections to your databases are
not stored in plain text. To encrypt a configuration section, you invoke the
ProtectSection() method of the SectionInformation object.
The NET 2.0 Framework ships with two types of providers:
DataProtectionConfigurationProvider and RSAProtectedConfigurationProvider.
You supply the ProtectSection() method with the encryption algorithm to use.
Below link explains how to use the Aspnet_Regiis.exe tool with the
RSAProtectedConfigurationProvider to encrypt sections of your configuration
file. This provider uses RSA public key encryption. Note that ASP.NET
automatically decrypts configuration sections when processing them, and so
you do not need to write any additional decryption code.
http://channel9.msdn.com/wiki/defau...onfigurationSectionsUsingRsaInAspNet20?diff=y
 
Hi,
If you can't execute commands in your web server, for example, when using
shared hosting, you still can encrypt it programatically:
Configuration config =
Configuration.GetWebConfiguration(Request.ApplicationPath);
ConfigurationSection section = config.Sections["connectionStrings"];
section.ProtectSection ("DataProtectionConfigurationProvider");
config.Update(); Now, the section in your Web.config file will look like this:

<connectionStrings>
<EncryptedData>
<CipherData>
<CipherValue>AQAAANCMndjHoAw...</CipherValue>
</CipherData>
</EncryptedData>
</connectionStrings>

And for decrypting the code is:
Configuration config = Configuration.GetWebConfiguration(
Request.ApplicationPath);
ConfigurationSection section =
config.Sections["connectionStrings"];
section.UnProtectSection ();
config.Update();
ref :msdn site
 
Hi,
I think i have gone little off the topic.All you wanted was to retrieve
value of connectionstring in ASP.NET 2.0 way.Suppose you have defined
Connectionstring tag in web.config in following way:
<connectionStrings>
<add name="NorthwindConn"
connectionString="Server='local'; database='NORTHWIND';
Trusted_Connection=False; uid=sa;pwd=*****"
providerName="System.Data.SqlClient" />
</connectionStrings>

What you must take care that tag <ConnectionStrings> is not included
inside <appSettings>
Include it after <configuration>Also, don't include it inside <system.web>
Then below code retrieves it:
ConfigurationManager.ConnectionStrings["NorthwindConn"].ConnectionString

In .NET 2.0 there is a new configuration class called ConfigurationManager.
It supercedes the ConfigurationSettings class that most .NET developers are
familiar with today
System.Configuration.dll - ensure you add a reference to this assembly. In
..NET 2.0 all configuration functionality is in this separate assembly
now.Once this reference is added, System.Configuration.ConfigurationManager
will once more be available to you.
 
Thanks for your ansa.
Am I pulling back value from web.config as a string or as a sqlconnection
i.e System.Data.SqlClient.sqlConnection?

Manish Bafna said:
Hi,
I think i have gone little off the topic.All you wanted was to retrieve
value of connectionstring in ASP.NET 2.0 way.Suppose you have defined
Connectionstring tag in web.config in following way:
<connectionStrings>
<add name="NorthwindConn"
connectionString="Server='local'; database='NORTHWIND';
Trusted_Connection=False; uid=sa;pwd=*****"
providerName="System.Data.SqlClient" />
</connectionStrings>

What you must take care that tag <ConnectionStrings> is not included
inside <appSettings>
Include it after <configuration>Also, don't include it inside <system.web>
Then below code retrieves it:
ConfigurationManager.ConnectionStrings["NorthwindConn"].ConnectionString

In .NET 2.0 there is a new configuration class called ConfigurationManager.
It supercedes the ConfigurationSettings class that most .NET developers are
familiar with today
System.Configuration.dll - ensure you add a reference to this assembly. In
.NET 2.0 all configuration functionality is in this separate assembly
now.Once this reference is added, System.Configuration.ConfigurationManager
will once more be available to you.
--
Hope this helps.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.



EdwardH said:
Am upgrading SQL server access app from 2003 to 2005. Old code works but I
can see safer methods:
My web.config (ASP.Net 1.1)
<configuration>
<appSettings>
<add key="ConnectString" value="Data Source=Vaio1;Initial Catalog=IMA;User
ID=sa;Password="/>
<add key="ImagePath" value="/IMA/Images/"/>
</appSettings> etc

and vb code to open in page:
Private ConnectString As String =
ConfigurationSettings.AppSettings("ConnectString")
Private invConn As SqlConnection
Private invCom As SqlCommand
Private objDR As SqlClient.SqlDataReader
and in
invConn = New SqlConnection(ConnectString)
invConn.Open()
invCom = New SqlCommand("iUserCheck", invConn)
invCom.CommandType = CommandType.StoredProcedure

I realise I need to encrypt this but want to upgrade this to ASP.net ver 2.
Old code works fine but realise it is out of date.
 
Hi,
You would be pulling back as string.That is
string strConn =
ConfigurationManager.ConnectionStrings["NorthwindConn"].ConnectionString
--
Hope this helps.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.



EdwardH said:
Thanks for your ansa.
Am I pulling back value from web.config as a string or as a sqlconnection
i.e System.Data.SqlClient.sqlConnection?

Manish Bafna said:
Hi,
I think i have gone little off the topic.All you wanted was to retrieve
value of connectionstring in ASP.NET 2.0 way.Suppose you have defined
Connectionstring tag in web.config in following way:
<connectionStrings>
<add name="NorthwindConn"
connectionString="Server='local'; database='NORTHWIND';
Trusted_Connection=False; uid=sa;pwd=*****"
providerName="System.Data.SqlClient" />
</connectionStrings>

What you must take care that tag <ConnectionStrings> is not included
inside <appSettings>
Include it after <configuration>Also, don't include it inside <system.web>
Then below code retrieves it:
ConfigurationManager.ConnectionStrings["NorthwindConn"].ConnectionString

In .NET 2.0 there is a new configuration class called ConfigurationManager.
It supercedes the ConfigurationSettings class that most .NET developers are
familiar with today
System.Configuration.dll - ensure you add a reference to this assembly. In
.NET 2.0 all configuration functionality is in this separate assembly
now.Once this reference is added, System.Configuration.ConfigurationManager
will once more be available to you.
--
Hope this helps.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.



EdwardH said:
Am upgrading SQL server access app from 2003 to 2005. Old code works but I
can see safer methods:
My web.config (ASP.Net 1.1)
<configuration>
<appSettings>
<add key="ConnectString" value="Data Source=Vaio1;Initial Catalog=IMA;User
ID=sa;Password="/>
<add key="ImagePath" value="/IMA/Images/"/>
</appSettings> etc

and vb code to open in page:
Private ConnectString As String =
ConfigurationSettings.AppSettings("ConnectString")
Private invConn As SqlConnection
Private invCom As SqlCommand
Private objDR As SqlClient.SqlDataReader
and in
invConn = New SqlConnection(ConnectString)
invConn.Open()
invCom = New SqlCommand("iUserCheck", invConn)
invCom.CommandType = CommandType.StoredProcedure

I realise I need to encrypt this but want to upgrade this to ASP.net ver 2.
Old code works fine but realise it is out of date.
 
On that line of code I now get
"System.Configuration.ConnectionStringSettingCollection cannot be converted
to a String". I have System.Configuration as a reference and the following
Import statements:
Imports System.Data.SqlClient
Imports System.Web.Security
Imports System.Configuration

Am I missing something else? Thanks again
Manish Bafna said:
Hi,
You would be pulling back as string.That is
string strConn =
ConfigurationManager.ConnectionStrings["NorthwindConn"].ConnectionString
--
Hope this helps.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.



EdwardH said:
Thanks for your ansa.
Am I pulling back value from web.config as a string or as a sqlconnection
i.e System.Data.SqlClient.sqlConnection?

Manish Bafna said:
Hi,
I think i have gone little off the topic.All you wanted was to retrieve
value of connectionstring in ASP.NET 2.0 way.Suppose you have defined
Connectionstring tag in web.config in following way:
<connectionStrings>
<add name="NorthwindConn"
connectionString="Server='local'; database='NORTHWIND';
Trusted_Connection=False; uid=sa;pwd=*****"
providerName="System.Data.SqlClient" />
</connectionStrings>

What you must take care that tag <ConnectionStrings> is not included
inside <appSettings>
Include it after <configuration>Also, don't include it inside <system.web>
Then below code retrieves it:
ConfigurationManager.ConnectionStrings["NorthwindConn"].ConnectionString

In .NET 2.0 there is a new configuration class called ConfigurationManager.
It supercedes the ConfigurationSettings class that most .NET developers are
familiar with today
System.Configuration.dll - ensure you add a reference to this assembly. In
.NET 2.0 all configuration functionality is in this separate assembly
now.Once this reference is added, System.Configuration.ConfigurationManager
will once more be available to you.
--
Hope this helps.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.



:

Am upgrading SQL server access app from 2003 to 2005. Old code works but I
can see safer methods:
My web.config (ASP.Net 1.1)
<configuration>
<appSettings>
<add key="ConnectString" value="Data Source=Vaio1;Initial Catalog=IMA;User
ID=sa;Password="/>
<add key="ImagePath" value="/IMA/Images/"/>
</appSettings> etc

and vb code to open in page:
Private ConnectString As String =
ConfigurationSettings.AppSettings("ConnectString")
Private invConn As SqlConnection
Private invCom As SqlCommand
Private objDR As SqlClient.SqlDataReader
and in
invConn = New SqlConnection(ConnectString)
invConn.Open()
invCom = New SqlCommand("iUserCheck", invConn)
invCom.CommandType = CommandType.StoredProcedure

I realise I need to encrypt this but want to upgrade this to ASP.net ver 2.
Old code works fine but realise it is out of date.
 
Hi,
sorry i posted in hurry.This is a problem with posting in hurry.Actually it
will return SqlConnection.You can write something like this:
Dim myConnection As New
SqlConnection(ConfigurationManager.ConnectionStrings(
"NorthwindConn").ConnectionString)
'Place the data in a DataTable
Dim myCommand As New SqlCommand(sql, myConnection)
Dim myAdapter As New SqlDataAdapter(myCommand)
myConnection.Open()
Dim dt As New DataTable
myAdapter.Fill(dt)
--
Hope this helps.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.



EdwardH said:
On that line of code I now get
"System.Configuration.ConnectionStringSettingCollection cannot be converted
to a String". I have System.Configuration as a reference and the following
Import statements:
Imports System.Data.SqlClient
Imports System.Web.Security
Imports System.Configuration

Am I missing something else? Thanks again
Manish Bafna said:
Hi,
You would be pulling back as string.That is
string strConn =
ConfigurationManager.ConnectionStrings["NorthwindConn"].ConnectionString
--
Hope this helps.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.



EdwardH said:
Thanks for your ansa.
Am I pulling back value from web.config as a string or as a sqlconnection
i.e System.Data.SqlClient.sqlConnection?

:

Hi,
I think i have gone little off the topic.All you wanted was to retrieve
value of connectionstring in ASP.NET 2.0 way.Suppose you have defined
Connectionstring tag in web.config in following way:
<connectionStrings>
<add name="NorthwindConn"
connectionString="Server='local'; database='NORTHWIND';
Trusted_Connection=False; uid=sa;pwd=*****"
providerName="System.Data.SqlClient" />
</connectionStrings>

What you must take care that tag <ConnectionStrings> is not included
inside <appSettings>
Include it after <configuration>Also, don't include it inside <system.web>
Then below code retrieves it:
ConfigurationManager.ConnectionStrings["NorthwindConn"].ConnectionString

In .NET 2.0 there is a new configuration class called ConfigurationManager.
It supercedes the ConfigurationSettings class that most .NET developers are
familiar with today
System.Configuration.dll - ensure you add a reference to this assembly. In
.NET 2.0 all configuration functionality is in this separate assembly
now.Once this reference is added, System.Configuration.ConfigurationManager
will once more be available to you.
--
Hope this helps.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.



:

Am upgrading SQL server access app from 2003 to 2005. Old code works but I
can see safer methods:
My web.config (ASP.Net 1.1)
<configuration>
<appSettings>
<add key="ConnectString" value="Data Source=Vaio1;Initial Catalog=IMA;User
ID=sa;Password="/>
<add key="ImagePath" value="/IMA/Images/"/>
</appSettings> etc

and vb code to open in page:
Private ConnectString As String =
ConfigurationSettings.AppSettings("ConnectString")
Private invConn As SqlConnection
Private invCom As SqlCommand
Private objDR As SqlClient.SqlDataReader
and in
invConn = New SqlConnection(ConnectString)
invConn.Open()
invCom = New SqlCommand("iUserCheck", invConn)
invCom.CommandType = CommandType.StoredProcedure

I realise I need to encrypt this but want to upgrade this to ASP.net ver 2.
Old code works fine but realise it is out of date.
 
Manish,
Works fine
Many, many thanks - I have been struggling with this for a few days.

Manish Bafna said:
Hi,
sorry i posted in hurry.This is a problem with posting in hurry.Actually it
will return SqlConnection.You can write something like this:
Dim myConnection As New
SqlConnection(ConfigurationManager.ConnectionStrings(
"NorthwindConn").ConnectionString)
'Place the data in a DataTable
Dim myCommand As New SqlCommand(sql, myConnection)
Dim myAdapter As New SqlDataAdapter(myCommand)
myConnection.Open()
Dim dt As New DataTable
myAdapter.Fill(dt)
--
Hope this helps.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.



EdwardH said:
On that line of code I now get
"System.Configuration.ConnectionStringSettingCollection cannot be converted
to a String". I have System.Configuration as a reference and the following
Import statements:
Imports System.Data.SqlClient
Imports System.Web.Security
Imports System.Configuration

Am I missing something else? Thanks again
Manish Bafna said:
Hi,
You would be pulling back as string.That is
string strConn =
ConfigurationManager.ConnectionStrings["NorthwindConn"].ConnectionString
--
Hope this helps.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.



:

Thanks for your ansa.
Am I pulling back value from web.config as a string or as a sqlconnection
i.e System.Data.SqlClient.sqlConnection?

:

Hi,
I think i have gone little off the topic.All you wanted was to retrieve
value of connectionstring in ASP.NET 2.0 way.Suppose you have defined
Connectionstring tag in web.config in following way:
<connectionStrings>
<add name="NorthwindConn"
connectionString="Server='local'; database='NORTHWIND';
Trusted_Connection=False; uid=sa;pwd=*****"
providerName="System.Data.SqlClient" />
</connectionStrings>

What you must take care that tag <ConnectionStrings> is not included
inside <appSettings>
Include it after <configuration>Also, don't include it inside <system.web>
Then below code retrieves it:
ConfigurationManager.ConnectionStrings["NorthwindConn"].ConnectionString

In .NET 2.0 there is a new configuration class called ConfigurationManager.
It supercedes the ConfigurationSettings class that most .NET developers are
familiar with today
System.Configuration.dll - ensure you add a reference to this assembly. In
.NET 2.0 all configuration functionality is in this separate assembly
now.Once this reference is added, System.Configuration.ConfigurationManager
will once more be available to you.
--
Hope this helps.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.



:

Am upgrading SQL server access app from 2003 to 2005. Old code works but I
can see safer methods:
My web.config (ASP.Net 1.1)
<configuration>
<appSettings>
<add key="ConnectString" value="Data Source=Vaio1;Initial Catalog=IMA;User
ID=sa;Password="/>
<add key="ImagePath" value="/IMA/Images/"/>
</appSettings> etc

and vb code to open in page:
Private ConnectString As String =
ConfigurationSettings.AppSettings("ConnectString")
Private invConn As SqlConnection
Private invCom As SqlCommand
Private objDR As SqlClient.SqlDataReader
and in
invConn = New SqlConnection(ConnectString)
invConn.Open()
invCom = New SqlCommand("iUserCheck", invConn)
invCom.CommandType = CommandType.StoredProcedure

I realise I need to encrypt this but want to upgrade this to ASP.net ver 2.
Old code works fine but realise it is out of date.
 
Manish,
Apologies for bringing this case up again but can I ask how secure this
ConfigurationMethod is as it is not passing a string? Is the web.config
encryption now still necessary?

EdwardH said:
Manish,
Works fine
Many, many thanks - I have been struggling with this for a few days.

Manish Bafna said:
Hi,
sorry i posted in hurry.This is a problem with posting in hurry.Actually it
will return SqlConnection.You can write something like this:
Dim myConnection As New
SqlConnection(ConfigurationManager.ConnectionStrings(
"NorthwindConn").ConnectionString)
'Place the data in a DataTable
Dim myCommand As New SqlCommand(sql, myConnection)
Dim myAdapter As New SqlDataAdapter(myCommand)
myConnection.Open()
Dim dt As New DataTable
myAdapter.Fill(dt)
--
Hope this helps.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.



EdwardH said:
On that line of code I now get
"System.Configuration.ConnectionStringSettingCollection cannot be converted
to a String". I have System.Configuration as a reference and the following
Import statements:
Imports System.Data.SqlClient
Imports System.Web.Security
Imports System.Configuration

Am I missing something else? Thanks again
:

Hi,
You would be pulling back as string.That is
string strConn =
ConfigurationManager.ConnectionStrings["NorthwindConn"].ConnectionString
--
Hope this helps.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.



:

Thanks for your ansa.
Am I pulling back value from web.config as a string or as a sqlconnection
i.e System.Data.SqlClient.sqlConnection?

:

Hi,
I think i have gone little off the topic.All you wanted was to retrieve
value of connectionstring in ASP.NET 2.0 way.Suppose you have defined
Connectionstring tag in web.config in following way:
<connectionStrings>
<add name="NorthwindConn"
connectionString="Server='local'; database='NORTHWIND';
Trusted_Connection=False; uid=sa;pwd=*****"
providerName="System.Data.SqlClient" />
</connectionStrings>

What you must take care that tag <ConnectionStrings> is not included
inside <appSettings>
Include it after <configuration>Also, don't include it inside <system.web>
Then below code retrieves it:
ConfigurationManager.ConnectionStrings["NorthwindConn"].ConnectionString

In .NET 2.0 there is a new configuration class called ConfigurationManager.
It supercedes the ConfigurationSettings class that most .NET developers are
familiar with today
System.Configuration.dll - ensure you add a reference to this assembly. In
.NET 2.0 all configuration functionality is in this separate assembly
now.Once this reference is added, System.Configuration.ConfigurationManager
will once more be available to you.
--
Hope this helps.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.



:

Am upgrading SQL server access app from 2003 to 2005. Old code works but I
can see safer methods:
My web.config (ASP.Net 1.1)
<configuration>
<appSettings>
<add key="ConnectString" value="Data Source=Vaio1;Initial Catalog=IMA;User
ID=sa;Password="/>
<add key="ImagePath" value="/IMA/Images/"/>
</appSettings> etc

and vb code to open in page:
Private ConnectString As String =
ConfigurationSettings.AppSettings("ConnectString")
Private invConn As SqlConnection
Private invCom As SqlCommand
Private objDR As SqlClient.SqlDataReader
and in
invConn = New SqlConnection(ConnectString)
invConn.Open()
invCom = New SqlCommand("iUserCheck", invConn)
invCom.CommandType = CommandType.StoredProcedure

I realise I need to encrypt this but want to upgrade this to ASP.net ver 2.
Old code works fine but realise it is out of date.
 
Back
Top