Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics 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 Display Modes
  #1 (permalink)  
Old January 11th, 2008, 04:08 PM
Friend of Wrox
 
Join Date: Mar 2007
Location: West Palm Beach, Florida, USA.
Posts: 205
Thanks: 4
Thanked 0 Times in 0 Posts
Default Declare a variable attribute in WebConfig

I have a select string that uses a variable. This variable is defined as a double. I would like to move the connection string from my code-behind to the web config. When I add the connection string

(add key="ConnectionString" value="Select Diameter, Pitch, Quantity, Rotation, Bore, Blades, Material, Manufacturer, Model, Condition from PropInventory where Sold = False and Diameter >= " & myDouble & " Order by Diameter" />)

it tells me that variable myDouble has missing attribute value

How would I declare my variable (as a double)?
Reply With Quote
  #2 (permalink)  
Old January 11th, 2008, 04:17 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

That isn't a connection string it is an actual query?

In any event you can't drop variables from XYZ class into your web.config unless you physically write the value to the web.config at run time.

You best bet is to put this query into a stored procedure.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor :.
Wrox Books 24 x 7
================================================== =========
Reply With Quote
  #3 (permalink)  
Old January 12th, 2008, 11:10 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,413
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You could store the value with replacement tokens such as:

<add key="ConnectionString" value="Select Diameter, Pitch, Quantity, Rotation, Bore, Blades, Material, Manufacturer, Model, Condition from PropInventory where Sold = False and Diameter >= ##DIAMETERPARAM## Order by Diameter" />

Then when you actually use this value you simply replace the token with the desired value in the code.

strQuery = ConfigurationManager.ApplicationSettings["ConnectionString"].Replace("##DIAMETERPARAM##", myDouble.ToString());

Alteratively, you could use the String.Format replacement tokens ( {0}, {1}, ...{n} ):

<add key="ConnectionString" value="Select Diameter, Pitch, Quantity, Rotation, Bore, Blades, Material, Manufacturer, Model, Condition from PropInventory where Sold = False and Diameter >= {0} Order by Diameter" />

then you use the standard String.Format method to do the replacement:

strQuery = string.Format(ConfigurationManager.ApplicationSett ings["ConnectionString"], myDouble);

This provide a slightly cleaner line of code, however there is a not an immediate relationship between the string going in (the appsetting value) and the replacement tokens. Using explicit token strings with the .Replace method will make it a bit more readable and maintainable.

-Peter
Reply With Quote
Reply


Thread Tools
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
Must declare the scalar variable stapes ASP.NET 2.0 Basics 1 August 15th, 2008 11:03 AM
Must declare the scalar variable kofibull Visual Studio 2005 0 November 24th, 2006 05:47 PM
"Must declare the scalar variable" Orchid85 BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 1 September 22nd, 2006 11:56 AM
How to declare global variable gaurav_jain2403 General .NET 3 February 21st, 2006 01:31 AM
declare empty variable jtyson General .NET 2 April 8th, 2004 12:02 PM



All times are GMT -4. The time now is 12:41 AM.


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