Read from database as I do from web.config

B

brent

Currently our application is reading from the web.config, and retrieves a
section, "softwareRequirements". Because the section will begin to grow
dramatically, I'd like to place it in the SQL database instead. I've
imported the values, and am retrieving them as FOR XML AUTO in the stored
proc to mimic the structure in the web.config. I'm confused now - what data
type is being returned by
system.configuration.configurationsettings.getconfig()? It looks like it has
to be an array or xml, but I'm not experienced enough at .NET to determine
how to best handle it. I would like to just provide a different data source
and leave the rest of my code the same.

Current code to read web.config:

Public Shared Function SoftwareRequirements() As
SoftwareRequirementsView
Return
System.Configuration.ConfigurationSettings.GetConfig("softwareRequirements")
End Function

<Serializable()> _
Public Class SoftwareRequirementView
Inherits UserProvisioningData.SoftwareRequirement

Private _ctrlId As String

Public Sub New(ByVal name As String, ByVal value As String, ByVal prompt
As String, ByVal availableTo As String)
MyBase.New(name, value, prompt, availableTo)
End Sub

Public Property ControlId() As String
Get
Return _ctrlId
End Get
Set(ByVal Value As String)
_ctrlId = Value
End Set
End Property

Public ReadOnly Property AdditionalRequirement(ByVal name As String) As
ItemDetail
Get
Dim result As ItemDetail
For Each req As ItemDetail In MyBase.AdditionalRequirements
If req.Name = name Then
result = req
Exit For
End If
Next
Return result
End Get
End Property
End Class

I'm attempting to pass data in from SQL instead:

Public Function GetSoftwareConfiguration(ByVal divisionId As Integer,
ByVal brandId As Integer, ByVal operationId As Integer, ByVal branchId As
Integer) As ArrayList
Dim methodPerf As New FNF.Services.Common.PerfAutoTrace
Dim result As ArrayList =
DBCommon.GetDataArray(GetType(UserProvisioningData.SoftwareRequirement),
DBCommon.ConnectionString("myDB"), "get_SoftwareConfig", New Object()
{divisionId, brandId, operationId, branchId})
methodPerf.Dispose()
Return result
End Function

Public Shared Function GetDataArray(ByVal returnType As Type, ByVal
connectionString As String, ByVal storedProcName As String, Optional ByVal
params() As Object = Nothing) As Object
Dim reader As SqlDataReader
Dim result As New ArrayList

Try
reader = SqlHelper.ExecuteReader(connectionString,
storedProcName, params)

While (reader.Read)
Dim item As Object = returnType.InvokeMember("New",
Reflection.BindingFlags.CreateInstance, Nothing, Nothing, New Object()
{reader})
result.Add(item)
End While
Catch ex As FNFException
Throw
Catch ex As Exception
Dim newEx As New FNFException("Error getting data from
database", New Object() {connectionString, storedProcName}, ex)
ExceptionManager.Publish(newEx)
Throw newEx
Finally
If Not (reader Is Nothing) Then reader.Close()
End Try

Return result
End Function

Public Shared Function getFromSQLInsteadOfWebConfigTest() As Object
Dim data As UserProvisioningData.DataRequest
Return data.GetSoftwareConfiguration(1, 1, 1, 1)
End Function

When I try to load the page I get an exception when calling my

Dim softReqView As Object =
SoftwareRequirementsConfigSection.SoftwareRequirements 'calling the
web.config & works great.
Dim softReqViewFromSQL As Object =
SoftwareRequirementsConfigSection.getFromSQLInsteadOfWebConfigTest 'calling
sql and throws exception

Exception is:
Exception Type: System.NullReferenceException
Message: Object reference not set to an instance of an object.

Any help would be appreciated.

Thanks,

Brent
 
P

Peter Rilling

You cannot because the framework itself loads the config when the app domain
starts. You cannot load it from a different location.

The object returned by GetConfig is dependent on the configuration handler
that is parsing the web.config XML. The reason for GetConfig is that you
can have an arbitrary XML structure in the config file that gets processed
and wrapped by some handler. That is why GetConfig returns an Object type.
 
K

Kevin Spencer

If you're reading a set of name=value pairs from a database, why does it
have to be XML? Create a table, put 2 columns in it, one for name, and one
for value. Pull the table, and use the records in it. No need for XML.

A configuration file is, by definition, an XML file. This is the way these
files were created. However, all they do is store data, just like a
database. An XML file is a good database, but unless you need to translate
the data into some form of cross-platform-compatible data source (like XML)
there's no need to use XML IN your database.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Neither a follower nor a lender be.
 
B

brent

It does. Thanks, Kevin.

Kevin Spencer said:
If you're reading a set of name=value pairs from a database, why does it
have to be XML? Create a table, put 2 columns in it, one for name, and one
for value. Pull the table, and use the records in it. No need for XML.

A configuration file is, by definition, an XML file. This is the way these
files were created. However, all they do is store data, just like a
database. An XML file is a good database, but unless you need to translate
the data into some form of cross-platform-compatible data source (like
XML) there's no need to use XML IN your database.

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
Neither a follower nor a lender be.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top