473,473 Members | 1,847 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How can I put my connectionString from the Web.Config file into my SqlConnection();

bencoding
22 New Member
Wow, I've spent about 3 hrs now trying to figure this one out, none of the articles online were able to help me and some were using system libraries that were obsolete......anyways,

I am able to read my connection string from my web.config file withing a Response.Write(); but when I put it in my SqlConnection([here]) it won't display, I don't get why

Can someone PLEASE I'm begging for a simple solution for this, please help!


This works fine:
Expand|Select|Wrap|Line Numbers
  1.         string connectionInfo = ConfigurationManager.AppSettings["connectionString"];
  2.         Response.Write(connectionInfo);
But when I put it in my SqlConnection it won't load :(

Expand|Select|Wrap|Line Numbers
  1.         SqlConnection myConn = new SqlConnection(connectionInfo );
Jun 20 '08 #1
3 1629
Curtis Rutland
3,256 Recognized Expert Specialist
What do you mean, won't display? It's not supposed to display anything.

First of all, you're either putting it in the wrong place, or loading it wrong.
You should put it in the "ConnectionStrings" section:
Expand|Select|Wrap|Line Numbers
  1. <configuration>
  2.   <connectionStrings>
  3.     <add name="connStringName" connectionString="Data Source=datasource;Initial Catalog=db;Persist Security Info=True;User ID=sa;Password=yeahRight" providerName="System.Data.SqlClient" />
  4.   </connectionStrings>
  5.   .....
  6.   .....
  7.   .....
  8. </configuration>
  9.  
And retrieve the value like this:
Expand|Select|Wrap|Line Numbers
  1. string connStr = ConfigurationManager.ConnectionStrings["connStringName"].ConnectionString;
  2. SqlConnection conn = new SqlConnection(connStr);
  3.  
Jun 20 '08 #2
bencoding
22 New Member
yes I understand, I did put it there, I have tried this and I get "InvalidOperationException was unhandled by user code" and it points to:

Expand|Select|Wrap|Line Numbers
  1. conn.Open();
Using this code:

Expand|Select|Wrap|Line Numbers
  1.    <connectionStrings>
  2.     <add name="DatabaseConnectionString" connectionString="Data Source=[myDB];Integrated Security=True"
  3.      providerName="System.Data.SqlClient" />
  4.   </connectionStrings>


Expand|Select|Wrap|Line Numbers
  1.     string connStr = ConfigurationManager.ConnectionStrings["connStringName"].ConnectionString;
  2.     SqlConnection myConn = new SqlConnection(connStr);

...this was what I was getting before, still not working.
Jun 20 '08 #3
Curtis Rutland
3,256 Recognized Expert Specialist
Ok, do this:

Expand|Select|Wrap|Line Numbers
  1. try
  2. {
  3.      conn.Open();
  4. }
  5. catch (Exception ex)
  6. {
  7.      Response.Write("Exception! :: " + ex.Message);
  8.       Response.End();
  9. }
  10.  
And let us know what the message is.


yes I understand, I did put it there, I have tried this and I get "InvalidOperationException was unhandled by user code" and it points to:

Expand|Select|Wrap|Line Numbers
  1. conn.Open();
Using this code:

Expand|Select|Wrap|Line Numbers
  1.    <connectionStrings>
  2.     <add name="DatabaseConnectionString" connectionString="Data Source=[myDB];Integrated Security=True"
  3.      providerName="System.Data.SqlClient" />
  4.   </connectionStrings>


Expand|Select|Wrap|Line Numbers
  1.     string connStr = ConfigurationManager.ConnectionStrings["connStringName"].ConnectionString;
  2.     SqlConnection myConn = new SqlConnection(connStr);

...this was what I was getting before, still not working.
Jun 20 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Devante | last post by:
Hi, I am fairly new to ASP.NET and have been working on a web form that will allow a user to upload images to a database. I have found a sample web form that I have been trying to get working,...
4
by: Ed_P. | last post by:
Hello, I have the following scenario: I have a solution with 4 projects Project1 = MainWindow (Windows EXE) Project2 = PresentationLayer (DLL) Project3 = BusinessLayer (DLL) Project4 =...
5
by: josephrthomas | last post by:
hi...i am getting a error here...saying : **quote** The ConnectionString property has not been initialized. Description: An unhandled exception occurred during the execution of the current web...
6
by: Tony | last post by:
Dear All, When I run an example program on the http://www.dotnetjunkies.com/quickstart/util/srcview.aspx?path=/quickstart/aspplus/samples/webforms/data/datagrid1.src&file=VB\datagrid1.aspx&font=3 ...
10
by: Laura K | last post by:
Tried all kinds of things. I get the error. The ConnectionString property has not been initialized. Any help appreciated. ...
2
by: tshad | last post by:
I am trying to create an application in my VS 2002 application. I took some of the code from my web page where it works fine. I can connect fine from my Sql Query Analyser to both my local Sql...
5
by: Arpan | last post by:
I created a System DSN named "NETData" for a SQL Server 2005 Express database. Now to make use of this System DSN to access records in the DB table, this is how I framed the ConnectionString: ...
11
by: tbh | last post by:
is it possible in the ConnectionString of an asp:SqlDataSource construction to refer to a variable i define at the top of my ASPX script and initialize, for example, during Page_Init()? (the...
3
by: Michael Moreno | last post by:
Hi, We have an app that is made of many exes (WinForms and Win32 app). All those exes share and read their DB connection string from the registry. This guarantees that all the exes use the...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.