Wrox Programmer Forums
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 1st, 2011, 09:57 AM
Registered User
 
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."
 
Old November 1st, 2011, 12:45 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 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!
 
Old November 3rd, 2011, 05:22 PM
Registered User
 
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!
 
Old November 3rd, 2011, 05:47 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 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!





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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.