Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > BOOK: Beginning ASP.NET 4 : in C# and VB
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning ASP.NET 4 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4: in C# and VB by Imar Spaanjaars; ISBN: 9780470502211
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4 : in C# and VB section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 1st, 2011, 09:57 AM
Registered User
Points: 87, Level: 1
Points: 87, Level: 1 Points: 87, Level: 1 Points: 87, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2011
Posts: 18
Thanks: 0
Thanked 2 Times in 2 Posts
Question Code-Behind and SQL Connection Strings

I've read all the relevant chapters in the book and skimmed the others, but can't seem to find where it covers accessing SQL in the C# code-behind. In examples I find elsewhere, the connection screen is always hard-coded in the code-behind. I find this hard to believe, since you would think there would be closer consistency and integration with the web.config file - whenever the connection string info changes, I need to go through all my code-behind data strings and change the information there as well. I've tried doing the following, and got an error:

Web.config:

Code:
<configuration>
  <connectionStrings>
    <add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
      providerName="System.Data.SqlClient" />
    <add name="TestConnectionString" connectionString="Data Source=SERVERNAME;Initial Catalog=TESTDB;Persist Security Info=True;User ID=sa;Password=********"
      providerName="System.Data.SqlClient" />
  </connectionStrings>

C# code behind:

Code:
SqlConnection conn = new SqlConnection("TestConnectionString");
Yields the error:
"Exception: Format of the initialization string does not conform to specification starting at index 0."
Reply With Quote
  #2 (permalink)  
Old November 1st, 2011, 12:45 PM
Imar's Avatar
Wrox Author
Points: 72,045, Level: 100
Points: 72,045, Level: 100 Points: 72,045, Level: 100 Points: 72,045, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,083
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Direct SQL using ADO.NET is not covered in my book. Instead I use technologies such as Entity Framework. I think the Pro edition of this book covers this. In addition you can check out my two series on N-Layered design that deal with this extensively:

http://imar.spaanjaars.com/416/build...pnet-20-part-1
http://imar.spaanjaars.com/476/n-lay...l-introduction

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #3 (permalink)  
Old November 3rd, 2011, 05:22 PM
Registered User
Points: 87, Level: 1
Points: 87, Level: 1 Points: 87, Level: 1 Points: 87, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2011
Posts: 18
Thanks: 0
Thanked 2 Times in 2 Posts
Default A Work Around

I took a hint from chapter 19, page 699 - "The web.config File - Expression Syntax", and now have the code-behind connection string within the web.config file, where it's easier to modify and keep track of.

In the web.config file:

Code:
  <appSettings>
    <add key="csConnectionString" value="data source=SOMESERVER;initial catalog=TEST; integrated security=SSPI;persist security info=False; Trusted_Connection=Yes"/>
  </appSettings>
Just below the content tag of the web page:

Code:
<asp:Literal ID = "csConnectionString" runat="server" Visible="false" Text="<%$ AppSettings:csConnectionString %>" />

In the C# code-behind:

Code:
SqlConnection conn = new SqlConnection(@csConnectionString.Text);
If you need any help on your next book, please let me know, Imar!
Reply With Quote
  #4 (permalink)  
Old November 3rd, 2011, 05:47 PM
Imar's Avatar
Wrox Author
Points: 72,045, Level: 100
Points: 72,045, Level: 100 Points: 72,045, Level: 100 Points: 72,045, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,083
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Ouch; I think this is a bad idea as the connection string is now available in the page (hidden from the client or not).

What you are looking for is this:

WebConfigurationManager.ConnectionStrings["csConnectionString}'.ConnectionString

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Connection strings arun_babu_a BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 3 November 1st, 2011 12:52 PM
Connection Strings WFletch Visual Basic 2008 Essentials 1 July 14th, 2008 09:42 AM
Securing connection strings haines ASP.NET 2.0 Basics 1 March 2nd, 2008 04:28 PM
Connection Strings WillyWonker BOOK: Beginning VB.NET 2nd Edition/Beginning VB.NET 2003 3 January 25th, 2005 10:40 AM
Connection Strings hcweb Classic ASP Basics 6 September 23rd, 2004 03:29 AM



All times are GMT -4. The time now is 09:35 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.