Onthrax
asked on
Using the ConfigurationManager in a webcontrol.
I am using the following code to fetch all connectionstrings from web.config:
Dim ConnString As ConnectionStringSettings
For Each ConnString In System.Configuration.Confi gurationMa nager.Conn ectionStri ngs()
List.Add(ConnString.Name.T oString())
Next
This is working fine on codebehind of default.aspx, but as soon as I try to implement it in a webcontrol it does not fetch the connectionstrings anymore except for the 'standard' LocalSqlServer one.
Is this some security feature or should I address it differently from a webcontrol. Any insight would be great.
Cheers
Dim ConnString As ConnectionStringSettings
For Each ConnString In System.Configuration.Confi
List.Add(ConnString.Name.T
Next
This is working fine on codebehind of default.aspx, but as soon as I try to implement it in a webcontrol it does not fetch the connectionstrings anymore except for the 'standard' LocalSqlServer one.
Is this some security feature or should I address it differently from a webcontrol. Any insight would be great.
Cheers
by a "webcontrol" do you mean a user control? that sould work fine. Or do you mean a custom web server control? You should probably post a snippet of your code for the control where you are using configmanager, we could help you better if you did.
ASKER
I mean a custom web server control. I have done some further experimenting and it seems it is working fine at run time but not on design time. I'll try to elaborate.
I have created the following function to fetch all connectionstring names:
Public Shared Function GetConnectionStringNames() As ArrayList
Dim ConnString As ConnectionStringSettings
Dim List As New ArrayList
For Each ConnString In System.Configuration.Confi gurationMa nager.Conn ectionStri ngs()
List.Add(ConnString.Name.T oString())
Next
Return List
End Function
When I import my control into the web page and call this function at run time the website displays the names perfectly.
However when I try to call this function at design time (visual studio) to incorporate at a property, only one name results back.
<TypeConverter(GetType(Enu mConverter ))> _
<Description("The connectionstring required to access the database.")> _
<Category("ValidationMessa ges")> _
Public Property ConnectionString() As String
Get
Throw New HttpException(GetConnectio nStringNam es.Count) <== this returns 1, not 3
Return CType(_ConnectionString, String)
End Get
Set(ByVal Value As String)
_ConnectionString = Value
End Set
End Property
Any idea's?
I have created the following function to fetch all connectionstring names:
Public Shared Function GetConnectionStringNames()
Dim ConnString As ConnectionStringSettings
Dim List As New ArrayList
For Each ConnString In System.Configuration.Confi
List.Add(ConnString.Name.T
Next
Return List
End Function
When I import my control into the web page and call this function at run time the website displays the names perfectly.
However when I try to call this function at design time (visual studio) to incorporate at a property, only one name results back.
<TypeConverter(GetType(Enu
<Description("The connectionstring required to access the database.")> _
<Category("ValidationMessa
Public Property ConnectionString() As String
Get
Throw New HttpException(GetConnectio
Return CType(_ConnectionString, String)
End Get
Set(ByVal Value As String)
_ConnectionString = Value
End Set
End Property
Any idea's?
ASKER
Also why do you have a different picture (man icon) then everyone else?
Is the web control in a DLL project, separate from the web site?
ASKER
Yes, but located in the bin folder of the website.
The ConfigurationManager will not be looking for the web.config file, but a config file like dllassembly.dll.config.
ASKER
That would mean the web.config file is read by the server and it's contents put in dllassembly.dll.config as at run time the correct number of connectionstrings are returned. Correct?
If so, I would have to read the web.config as XML and extract the connectionstrings node to fetch the names. Might be tricky as multiple web.config files can exist. Not impossible though.
If so, I would have to read the web.config as XML and extract the connectionstrings node to fetch the names. Might be tricky as multiple web.config files can exist. Not impossible though.
No, not correct. Your DLL assembly would need an App.config, which would be turned into that config file for the DLL. The web.config file doesn't even come into play here. I agree that you would have to read the web.config file yourself. I do that for one of my DLL assemblies.
ASKER
Then I don't get why this function will return the correct number of connection strings at run time but not at designtime..
Public Shared Function GetConnectionStringNames() As ArrayList
Dim ConnString As ConnectionStringSettings
Dim List As New ArrayList
For Each ConnString In System.Configuration.Confi gurationMa nager.Conn ectionStri ngs()
List.Add(ConnString.Name.T oString())
Next
Return List
End Function
Public Shared Function GetConnectionStringNames()
Dim ConnString As ConnectionStringSettings
Dim List As New ArrayList
For Each ConnString In System.Configuration.Confi
List.Add(ConnString.Name.T
Next
Return List
End Function
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Did you try a WebConfigurationManager?
ASKER
w000t I win.. but this time I don't want to win lol.
I didn't know WebConfigurationManager is similar, I'll check it out and get back..
I didn't know WebConfigurationManager is similar, I'll check it out and get back..
I thought we were playing stump the expert *BIG GRIN*
ASKER
Hehe.. well somehow it's comforting to know even an expert of status genius is yet unable to solve my problem :P
(still checking WebConfigurationManager though)
(still checking WebConfigurationManager though)
It is a humble man that will admit that he doesn't know everything, despite what others might think. *WINK*.
ASKER
WebConfigurationManager turns out exactly the same.
Works at run time but does only return LocalSqlServer at design time. Very strange....
Works at run time but does only return LocalSqlServer at design time. Very strange....
ASKER
I am leaning toward accepting ID 22659186 as answer :P
I went along with reading web.config as XML which is working fine now.
Thanks for all your help m8 :) Highly appreciated!
I went along with reading web.config as XML which is working fine now.
Thanks for all your help m8 :) Highly appreciated!
>>Also why do you have a different picture (man icon) then everyone else?
If one of us can beat The Learned One in a head-to-head quiz, we get to wear the "leader" jacket. :)
j/k, its a new feature you can customize it in your profile.
If one of us can beat The Learned One in a head-to-head quiz, we get to wear the "leader" jacket. :)
j/k, its a new feature you can customize it in your profile.
ASKER
Already noticed it today :P But thnx for letting me know :D
ASKER
LOL @ recommendation :p
Thanks again for all your help m8. guess I forgot to close the question.. Cheers
Thanks again for all your help m8. guess I forgot to close the question.. Cheers
ASKER
Something went wrong here. I had accepted an answer and rewarded points to 3 posts....