Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
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 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 January 11th, 2008, 04:08 PM
Friend of Wrox
 
Join Date: Mar 2007
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)?
 
Old January 11th, 2008, 04:17 PM
Wrox Author
 
Join Date: Oct 2005
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
================================================== =========
 
Old January 12th, 2008, 11:10 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
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





Similar Threads
Thread Thread Starter Forum Replies Last Post
Must declare the scalar variable stapes ASP.NET 2.0 Basics 1 August 15th, 2008 10: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 10: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 11:02 AM





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