473,789 Members | 2,547 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 7821
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
9385
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
1231
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
4889
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
1474
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
10637
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
4034
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
1728
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
9663
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
10193
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10136
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
9979
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7525
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
6761
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5415
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
4089
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
2
3695
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.