473,802 Members | 1,971 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Specialized.Nam eValueCollectio n

Hi all,
I've been scouring the internet for help with this problem and every
occurance i've seen reconstructs the problem but no one seems to have a
solution. Hoping that you guys can help me.

I have a vb.net windows forms project that is using a app.config file.
the contents of the app.config is staggered. see a snippet below:

<?xml version="1.0" encoding="utf-8" ?>
<configuratio n>
<configSections >
<!-- tell .NET Framework to ignore CLR sections -->
<section name="frisbee"
type="System.Co nfiguration.Nam eValueFileSecti onHandler, System,
Version=1.0.500 0.0, Culture=neutral , PublicKeyToken= b77a5c561934e08 9" />
<section name="frmBatchi ng"
type="System.Co nfiguration.Nam eValueFileSecti onHandler, System,
Version=1.0.500 0.0, Culture=neutral , PublicKeyToken= b77a5c561934e08 9" />
<section name="frmFilter ing"
type="System.Co nfiguration.Nam eValueFileSecti onHandler, System,
Version=1.0.500 0.0, Culture=neutral , PublicKeyToken= b77a5c561934e08 9" />
<section name="frmConfig "
type="System.Co nfiguration.Nam eValueFileSecti onHandler, System,
Version=1.0.500 0.0, Culture=neutral , PublicKeyToken= b77a5c561934e08 9" />
<section name="frmSuppor ting"
type="System.Co nfiguration.Nam eValueFileSecti onHandler, System,
Version=1.0.500 0.0, Culture=neutral , PublicKeyToken= b77a5c561934e08 9" />
</configSections>
<frisbee>
<add key="intErrorLo gtype" value="1" />
<add key="EventLogKe yName" value="FRISBEE" />
<add key="SQLDBConnS tring" value="" />
<add key="sys_DBChec k" value="usp_Chec kSQLConnection" />
<add key="Maximised" value="true" />
</frisbee>
<frmBatching>
<add key="height" value="" />
<add key="width" value="" />
<add key="top-left-position" value="" />
<add key="ImgDir" value="" />
<add key="BatchDir" value="" />
<add key="ImgInBatch " value="" />
<add key="BatchNo" value="" />
<add key="Maximised" value="true" />
</frmBatching>
Now to read values out of this staggered app.config file i use the
specialised.nam evaluecollectio n in the following manner

Dim objConfigNV As New Specialized.Nam eValueCollectio n
Dim strConfigValue As String = Convert.ToStrin g(vbNullString)

objConfigNV = CType(Configura tionSettings.Ge tConfig(strIDic Name),
Specialized.Nam eValueCollectio n)

strConfigValue = Convert.ToStrin g(objConfigNV(s trKeyName))

And this works fine.

Now.. the problem..

to write a value back to the app.config file using the same
Specialized.Nam evalueCollectio n in the following manner:

'initialise collection object of app.config
Dim objNameValue As New Specialized.Nam eValueCollectio n

'app.ocnfig is staggered. this gets the relevant sectoin on
file
objNameValue =
CType(Configura tionSettings.Ge tConfig("frmSup porting"),
Specialized.Nam eValueCollectio n)

'this sets the the new value of a key
objNameValue(st rKey) = strValue
Generates an exception: Collection is read-only
Does ANYONE have any ideas how to create an instance of a
NameValueCollec tion that is not read-only so that I can write back to my
app.config file.
Any help on this would be greatly appreciated.

Kerr

*** Sent via Developersdex http://www.developersdex.com ***
Nov 21 '05 #1
4 7823
App.config file was never meant to be written to using the
NameValueCollec tion. It was not meant to store user settings. The
only way to write back to it would be to use the methods in the
System.Xml namespace.

What I typically do is design a class that models all the settings I
need and then I make that class serializable. Then I just need to
serialize and deserialize to/from the xml file and I have an object
with all my settings.

Nov 21 '05 #2
Chris,
Thanks for your reply. Unfortunately you've over estimated my .net
knowledge.

What you've described makes sense but I need to see a practical example
of what you describe for me to be able to implement a solution that
works.

I hear what your saying about not using the namevaluecollec tion to write
to the app.config file but because I am extending the use of the
app.config file by adding in the additional sections I was pointed at
using this method by other forums.

Because I am not sure how to implement your suggestion my question still
stands, which is, do you know of a way to create a NON-READ-ONLY version
of the NameValueCollec tion.

Cheers

Kerr

*** Sent via Developersdex http://www.developersdex.com ***
Nov 21 '05 #3
There is no way to create a NON-READ-ONLY NameValueCollec tion that I
know of. But the method below is just a simple.

What I mean is that I create a class with properties that hold the
values that I want to store, then use the XmlSerializer to save that
class with it's values to a file.

Here is a simple example.

Create a new class file and add the code at the end of this post. Then
add a Sub Main or sub with the following code:

Public Sub Main()

'Create an instance of the MySettings class
Dim settings As New MySettings

'Set some of the properties
settings.Frisbe e.Maximised = False
settings.frmBat ching.Width = 44

'Save the configuration to an .xml file
MySettings.Save ("c:\MySettings .xml", settings)
'To load the data from the file use this code:
Dim settings2 As MySettings =
MySettings.Load ("c:\MySettings .xml")

'Print the value of the Width property:
Console.WriteLi ne(settings2.fr mBatching.Width .ToString)

'Change the value of the width property
settings2.frmBa tching.Width = 100

'Resave the file
MySettings.Save ("c:\MySettings .xml", settings2)

End Sub
'Note that the classes are marked as Serializable() and that the
serializer
'will only save the properties and fields that are public. I used
public fields
'in this case, but you could use full properties.

'The Load and Save method are Shared methods for convenience.
'******** MySettings.Vb *************** ****

Imports System.Xml.Seri alization
Imports System.IO

<Serializable() > _
Public Class MySettings

Public Frisbee As FrisbeeClass
Public frmBatching As frmBatchingClas s

Public Sub New()
Frisbee = New FrisbeeClass
frmBatching = New frmBatchingClas s
End Sub

Shared Function Load(ByVal fname As String) As MySettings
Dim sr As StreamReader

Try
sr = New StreamReader(fn ame)
Dim xs As New XmlSerializer(G etType(MySettin gs))
Return DirectCast(xs.D eserialize(sr), MySettings)
Finally
sr.Close()
End Try

End Function

Shared Sub Save(ByVal fname As String, ByVal obj As MySettings)
Dim sw As StreamWriter

Try
sw = New StreamWriter(fn ame)
Dim xs As New XmlSerializer(G etType(MySettin gs))

xs.Serialize(sw , obj)
Finally
sw.Close()
End Try

End Sub
End Class

<Serializable() > _
Public Class FrisbeeClass
Public intErrorLogType As Integer
Public EventLogKeyName As String = String.Empty
Public SQLDBConnString As String = String.Empty
Public sys_DBCheck As String = String.Empty
Public Maximised As Boolean
End Class

<Serializable() > _
Public Class frmBatchingClas s
Public Height As Integer
Public Width As Integer
Public TopLeft As Integer
Public ImgDir As String = String.Empty
Public BatchDir As String = String.Empty
Public ImgInBatch As String = String.Empty
Public BatchNo As Integer
Public Maximised As Boolean
End Class

'************ END MySettings.vb *************** ****

Nov 21 '05 #4
Chris,
thanks for the post and the code. Your implementation works and I
understand how it works which is even better. However, I am not sure if
its something i've done wrong but whenever writing back (save) to the
xml document the structure of the document changes and then my entire
app stops working.

I am getting to the stage where i am thinking it is easier to do this
sort of dynamic configuration stuff through a database rather than the
app.config file. I can't spend too much longer trying to get this to
work.

Chris, thanks for your help on this anyway.

Kerr

*** Sent via Developersdex http://www.developersdex.com ***
Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
9386
by: Walter Zydhek | last post by:
I am having a problem using the NameValueCollection type. If I remove one of the items while iterating through an collection of this type, I end up with an exception. This exception is: Collection was modified after the enumerator was instantiated. This happens when it attempts to continue through the enumeration.
5
1685
by: anon | last post by:
I need a little clarity in the NameValueCollection. Within the MSDN .NET 1.1 Framework help is says: "This collection is based on the NameObjectCollectionBase class. However, unlike the NameObjectCollectionBase, this class stores multiple string values under a single key."
0
1232
by: Nicole_new C# Programmer | last post by:
I am creating an application where I have 3 listboxes. The 1st holds the attributes of our products like division, group, etc. The 2nd listbox holds the values attach to each attribute like division may have the code ED or DC and group by have ORP or DVS. Now I am suppose to allow user to select one attribute and list the values associated to each attribute in listbox two. Then once they selected the values they want to display this...
7
14748
by: davidw | last post by:
I always use NameValueCollection. But I read an article says the only differece between Hashtable and NameValueCollection is that NameValueCollection could accept more than one value with same key? I am not quite understand this. And I think NameValueCollection's Set is a nice method. If I use HashTable, how can I update a item in it? Do I have to check if it exists, and remove and add it again? Thanks.
1
4890
by: Mark Miller | last post by:
I just recently started getting the above error on a page I am posting MULTIPART/FORM-DATA. We have SoftArtisans FileUp component and Filter installed on the server in question and up until a day or so ago everything was working fine. I honestly can't remember changing anything since it was last working. But I tried reinstalling the .Net Framework along w/ the service pack, which didn't work. I also had v1.1 already installed but I hadn't...
0
1475
by: Yuri Vanzine | last post by:
Saw this requested awhile ago on Matthew Reynolds's blog here: http://www.dotnet247.com/247reference/msgs/11/57944.aspx Problem: often times I deal with querystring-formatted strings w/o the actual HttpRequest object involved. Solution: the function accepts such a querystring-type string and outputs a NameValueCollection object
3
10641
by: Mike Logan | last post by:
We are trying to serialize the Request.ServerVariables collection (NameValueCollection) to an XML formatted string, to insert into a database. The problem we are running into is that most of the example that we find discuss writing to a file. Does anyone have a simple function to do this? We would also like to deserialize the string from the database back into a NameValueCollection. -- Mike Logan
3
4035
by: Phil C. | last post by:
Hi. I need to translate the C# statement: if( this.validatorAssignments != null && this.validatorAssignments.HasKeys() ) where validatorAssignments is an instance of System.Collections.Specialized.NameValueCollection into VB.Net
0
1729
by: John Grandy | last post by:
My business-layer class has a method that returns a System.Collections.Specialized.NameValueCollection I wrapped this business-layer class in a Web Service. In an ASP.NET project, when I attempt to add a Web Reference to this Web Service, I receive the error : You must implement the Add(System.String) method on System.Collections.Specialized.NameValueCollection because it inherits from
0
9699
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9562
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10538
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10285
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9115
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7598
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5494
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4270
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2966
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.