There are several answered questions detailing how to refer to a single value from web.config file. I'd like to maintain lists of values with which to populate combo boxes in the Views. Should I use some sort of key/value pair structure, with the key identifying the particular combo box to populate?
<add key="CalculationMethod" value="Fixed"/>
<add key="CalculationMethod" value="Cost Plus"/>
<add key="CalculationMethod" value="Formula"/>
I'm not sure how I'd read this, or if that would even work (don't keys have to be unique?). The IntelliSense for web.config doesn't seem to allow much in the appSettings section that looks applicable to a more robust structure like
<list name="CalculationMethod">
<item value="Fixed"/>
<item value="Cost Plus"/>
<item value="Formula"/>
</list>
I see that the root <configuration> has a lot of options for children, but which one to use, if any?
You really should be using a database for something like this but if you must put it in the web.config then you could delimit the values and parse them out at run time.
<add key="CalculationMethod" value="Fixed,Cost Plus,Formula"/>
myComboBox.DataSource = new List<String>(lstrCalcMethodsFromWebConfig.Split(','));
Related
Can I split appSettings into multiple external config files and include them in main web.config?
Here is what I have tried, but its not working :(
In web.config I defined a new section:
<configSections>
<section name="ssoConfiguration" type="System.Configuration.NameValueSectionHandler"/>
</configSections>
below I have this section:
<ssoConfiguration>
<add key="SSOEnabled" value="true"/>
</ssoConfiguration>
When I call System.Configuration.ConfigurationManager.AppSettings["SSOEnabled"] it returns null.
Any ideas why?
Also, I will have multiple sections with such appSettins - is it possible to define them in multiple external config files and get them included in main web.config?
Thank you.
Because you're accessing AppSettings, but the "SSOEnabled" setting is in another section ("ssoConfiguration"). Try
var ssoConfiguration = (NameValueCollection)ConfigurationManager.GetSection("ssoConfiguration")
var ssoEnabled = ssoConfiguration["SSOEnabled"];
I have follwing in app.config file:
<appSettings>
<add key="Name" value="Office"/>
...
<add key="Name" value="HotSpot"/>
...
<add key="Name" value="Home"/>
</appSettings>
I tried
ConfigurationManager.AppSettings["Name"]
But it only gives me one Value? How can i get list of all values? I am using c# 3.5. Is there lambda expression or something i can use to get that?
You can only use one key per value, so this approach will not work.
There are two alternate approaches I can think of:
Use a single key with a delimiter, and retrieve with ConfigurationManager.AppSettings["Name"].Split(new [] { "," });.
<add key="Name" value="Office,Hotspot,Home" />
Use a custom section to create a section that can contain your array of strings.
I want to display a message in my homepage (default.aspx), which is different for each "installation" of my web app. I would like to avoid making a call to the database to show this message.. so I thought of using web.config to store something like this
<add key="WelcomeString" value="lorem ipsus <b>doloret sit amen</b>" />
But I've noticed I can't use html markup in the web.config ...
Is there a better approach, or is there a way to insert html markup into web.config?
Thank you again stack overflow guru's... i'm learning from you a lot of things !
You need to XML encode it, to store it in the XML as a valid attribute value. eg:
<add key="WelcomeString" value="lorem ipsus <b>doloret sit amen</b>" />
Use "<" and ">" instead of "<" and ">":
<add key="WelcomeString" value="lorem ipsus <b>doloret sit amen</b>" />
You have a couple of examples of how to add it to the web.config file, but I would suggest that you consider adding a "localization" XML file to App_Data and read it from there rather than polluting the web.config file with customizations for each installation. You could read this file during application start up and store the values in the HttpRuntime.Cache by key, retrieving them from there as needed. Note that you need a way to regenerate them if they get flushed from the Cache (or mark them as not removable). Use the same technique to encode it for an attribute in the XML file or, if longer, store it in CDATA in the node value.
I use a technique like this with two XML files, defaults and localizations. Defaults supplies default values for the localizable aspects of the application. Localizations, if present, will override the defaults. These are loaded, in my case, into a Singleton object for the application that has strongly-typed properties for the values. Note that this encompasses much more than simply localized strings; they can be arbitrarily complex. The Singleton object has methods to read and apply both defaults and localizations given the path to the XML file.
so I'm very very far from an expert on MVC or ASP.NET. I just want to make a few simple Controllers in C# at the moment, so I have the following question;
Right now I have the connection string used by the controller, -inside- the controller itself. Which is kind of silly when there are multiple controllers using the same string. I'd like to be able to change the connection string in just one place and have it affect all controllers.
Not knowing a lot about asp.net or the 'm' and 'v' part of MVC, what would be the best (and simplest) way of going about accomplishing just this?
I'd appreciate any input on this, examples would be great too.
Put it in your web.config file like so:
<connectionStrings>
<add name="ConnectionName" connectionString="TheConnectionString" providerName="System.Data.SqlClient" />
</connectionStrings>
<connectionStrings> is just a child of the root <configuration> element.
Then you can read it like:
string myConnStr = ConfigurationManager.ConnectionStrings["ConnectionName"].ConnectionString;
HTHs,
Charles
You may add a connection string to the web.config file like this:
<configuration>
<appSettings>
<add key="ConnectionString"
value="server=localhost;database=Northwind;uid=sa;password=secret;"/>
</appSettings>
<system.web>
<customErrors mode="Off"/>
</system.web>
</configuration>
and use it in your code like this:
strConnection = ConfigurationSettings.AppSettings("ConnectionString") // <-----
sqlConn = New SqlConnection(strConnection)
sqlCmd = New SqlCommand("SELECT * FROM Customers WHERE " & "(CompanyName LIKE 'A%') OR (CompanyName LIKE 'B%')", sqlConn)
Notice the first line. This example is for VB but you should be able to do the same in C#.
The example is taken from this link http://www.dotnetjohn.com/articles.aspx?articleid=3, but there are tons of them everywhere.
Hope this helps.
I have a table defnition as given below:
License
ClientId
Type
Total
Used
ClientId and Type together uniquely identifies a row. I have a mapping file as given below:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true">
<class name="Acumen.AAM.Domain.Model.License, Acumen.AAM.Domain" lazy="false" table="License">
<id name="ClientId" access="field" column="ClientID" />
<property name="Total" access="field" column="Total"/>
<property name="Used" access="field" column="Used"/>
<property name="Type" access="field" column="Type"/>
</class>
</hibernate-mapping>
If a client used a license to create a user, I need to update the Used column in the table. As I set ClientId column as the id column for this table in the mapping xml, I am getting TooManyRowsAffectedException.
could you please let me know how to set a composite key at mapping level so that NHibernate can udpate based on ClientId and Type.
Something like: Update License SET Used=Used-1 WHERE ClientId='xxx' AND Type=1
Please help.
Thanks,
Mahesh
You have to use a composite-id
http://nhibernate.info/doc/nh/en/index.html#mapping-declaration-compositeid
If you primary key is composite, your mapping should reflect that, and your class needs to override Equals and GetHashCode.
Also, if ClientId is the primary key of your Client entity, you should map it as many-to-many, not just an Id.
Also, why are you specifying lazy="false"? Are you aware of the implications?
Also, why map everything with access="field"? Do the properties have some special logic?
This is a revised mapping considering everything I just wrote. Feel free to ignore those parts that don't apply :-)
<class name="Acumen.AAM.Domain.Model.License, Acumen.AAM.Domain" table="License">
<composite-id>
<key-many-to-one name="Client" column="ClientID" />
<key-property name="Type" />
</composite-id>
<property name="Total" />
<property name="Used" />
</class>
As the other comrades mentioned above, you have to use a composite-id, which is not a best but acceptable practice.
On the other hand, you can simply write an update interceptor and make sure your Type = 1 within it.
Here are some link about the topic to help you see clear in this.
Elegant code : Implementing NHibernate Interceptors
NHibernate Documentation : Interceptors
Sample NHibernate IInterceptor implementation
Enterprise .NET Community : NHibernate Part 2 (Scroll down to : Interceptors and Persistent Lifecycle)
NHibernate Interceptor Auditing Inserted Object Id (SO question)
The main advantage of using interceptors over a composite key is that it doesn't break your DBRM and provides a definitely more flexible solution, without "polluting" your mapping file which will more precisely represent your model.