ASP.NET 2.0
This code crashes. It generate this error:
Value cannot be null.
Parameter name: type
I've created some custom web.config settings and this crash is related to
accessing theme custom settings. This code is where the crash occur:
_instance =
(DAL)Activator.CreateInstance(Type.GetType(min.test.Config.Settings.msgEleme
nt.ProviderType));
This code is trying to do is to create an instance of the DAL provider,
************* custom web.config settings: **********************
<configSections>
<section name="msg" type="min.test.ConfigSection, __code"/>
</configSections>
<msg>
<messages providerType="min.test.DAL.SqlServer2005"/>
</msg>
********* In this code the crash occur *********
/* This code is from the DAL class, this code is trying to create an
instance of the DAL set in web.config - min.test.DAL.SqlServer2005
static public DAL Instance
{
get
{
if (_instance == null)
_instance =
(DAL)Activator.CreateInstance(Type.GetType(min.test.Config.Settings.msgEleme
nt.ProviderType));
<-- code crashes here
return _instance;
}
}
******** definition of the custom settings in web.config
**********************
using System;
using System.Collections.Generic;
using System.Configuration;
namespace min.test
{
public class ConfigSection : ConfigurationSection
{
[ConfigurationProperty("messages", IsRequired = true)]
public MsgElement msgElement
{
get { return (MsgElement)base["messages"]; }
}
}
public class MsgElement : ConfigurationElement
{
[ConfigurationProperty("providerType", DefaultValue =
"min.test.DAL.SqlServer2005")]
public string ProviderType
{
get { return (string)base["providerType"]; }
set { base["providerType"] = value; }
}
}
}
Any suggestions'
JeffWell,
The exception message doesn't lie - it's telling you that you've got a null
value, and it cannot accept it and continue.
One way to get at this is to "take apart" your compound statement so that it
will becom easier to set breakpoints and examine values, e.g.:
instance =
(DAL)Activator.CreateInstance(Type.GetType(min.test.Config.Settings.msgEleme
nt.ProviderType));
could be:
Type t = Type.GetType(min.test.Config.Settings.msgElement.ProviderType);
// check to see if t above is null.
instance = (DAL)Activator.CreateInstance(t);
Peter
--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net
"Jeff" wrote:
> ASP.NET 2.0
> This code crashes. It generate this error:
> Value cannot be null.
> Parameter name: type
> I've created some custom web.config settings and this crash is related to
> accessing theme custom settings. This code is where the crash occur:
> _instance =
> (DAL)Activator.CreateInstance(Type.GetType(min.test.Config.Settings.msgEle
ment.ProviderType));
> This code is trying to do is to create an instance of the DAL provider,
> ************* custom web.config settings: **********************
> <configSections>
> <section name="msg" type="min.test.ConfigSection, __code"/>
> </configSections>
> <msg>
> <messages providerType="min.test.DAL.SqlServer2005"/>
> </msg>
> ********* In this code the crash occur *********
> /* This code is from the DAL class, this code is trying to create an
> instance of the DAL set in web.config - min.test.DAL.SqlServer2005
> static public DAL Instance
> {
> get
> {
> if (_instance == null)
> _instance =
> (DAL)Activator.CreateInstance(Type.GetType(min.test.Config.Settings.msgEle
ment.ProviderType));
> <-- code crashes here
> return _instance;
> }
> }
> ******** definition of the custom settings in web.config
> **********************
> using System;
> using System.Collections.Generic;
> using System.Configuration;
> namespace min.test
> {
> public class ConfigSection : ConfigurationSection
> {
> [ConfigurationProperty("messages", IsRequired = true)]
> public MsgElement msgElement
> {
> get { return (MsgElement)base["messages"]; }
> }
> }
> public class MsgElement : ConfigurationElement
> {
> [ConfigurationProperty("providerType", DefaultValue =
> "min.test.DAL.SqlServer2005")]
> public string ProviderType
> {
> get { return (string)base["providerType"]; }
> set { base["providerType"] = value; }
> }
> }
> }
> Any suggestions'
> Jeff
>
>
Thanks
Type t = Type.GetType(min.test.Config.Settings.msgElement.ProviderType);
t gets a NULL value here. I've looked at the web.config settings and the
code that creates the custom web.config settings and I cannot see what I'm
doing wrong here... or maybe I'm becoming blind to my own programming
errors. Do you see why it get NULL value'
Jeff
"Peter Bromberg [C# MVP]" <pbromberg@.yahoo.yabbadabbadoo.com> wrote in
message news:ACE60486-118C-4BB2-A1C1-EB2416FB1CED@.microsoft.com...
> Well,
> The exception message doesn't lie - it's telling you that you've got a
> null
> value, and it cannot accept it and continue.
> One way to get at this is to "take apart" your compound statement so that
> it
> will becom easier to set breakpoints and examine values, e.g.:
> instance =
> (DAL)Activator.CreateInstance(Type.GetType(min.test.Config.Settings.msgEle
ment.ProviderType));
> could be:
> Type t = Type.GetType(min.test.Config.Settings.msgElement.ProviderType);
> // check to see if t above is null.
> instance = (DAL)Activator.CreateInstance(t);
> Peter
> --
> Site: http://www.eggheadcafe.com
> UnBlog: http://petesbloggerama.blogspot.com
> Short urls & more: http://ittyurl.net
>
>
> "Jeff" wrote:
>
Type.GetType gives you the ability to get a type back from a string. You pas
s
a known, available type name string to it, and expect it return that type.
If the assembly name is specified in the typeName string, Type.GetType()
will search inside this assembly only; otherwise, it tries to find one in th
e
caller assembly and then the system assembly (mscorlib.dll). Anything after
',' in the typeName string is treated as assembly name. To make it work,you
need to specify "YourAssembly.dll" in typeName. In contrast,
Type.GetType("System.Int32") can return type System.Int32 without mentioning
"mscorlib.dll".
Type.AssemblyQualifiedName is the right way to tell what kind of typeName
you should provide.
typeof(System.Data.SqlClient.SqlException).AssemblyQualifiedName equals:
"System.Data.SqlClient.SqlException, System.Data, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089". Type.GetType with this
long string will succeed in .NET 2.0.
In .NET, namespace and the associated assembly name are not necessarily
related. Type "System.Data.SqlClient.SqlException" is not associated with
"System.Data.SqlClient.dll" (which does not exist).
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net
"Jeff" wrote:
> Thanks
> Type t = Type.GetType(min.test.Config.Settings.msgElement.ProviderType);
> t gets a NULL value here. I've looked at the web.config settings and the
> code that creates the custom web.config settings and I cannot see what I'm
> doing wrong here... or maybe I'm becoming blind to my own programming
> errors. Do you see why it get NULL value'
> Jeff
> "Peter Bromberg [C# MVP]" <pbromberg@.yahoo.yabbadabbadoo.com> wrote in
> message news:ACE60486-118C-4BB2-A1C1-EB2416FB1CED@.microsoft.com...
>
>
Jeff,
In addition to Peter's reply, there's another way of obtaining the reference
in ASP.NET 2.0 dynamic compilation enviroment. Please use
System.Web.Compilation.BuildManager.GetType(min.test.Config.Settings.msgElem
ent.ProviderType, true);
instead.
Regards
--
Milosz
"Peter Bromberg [C# MVP]" wrote:
> Type.GetType gives you the ability to get a type back from a string. You p
ass
> a known, available type name string to it, and expect it return that type.
> If the assembly name is specified in the typeName string, Type.GetType()
> will search inside this assembly only; otherwise, it tries to find one in
the
> caller assembly and then the system assembly (mscorlib.dll). Anything afte
r
> ',' in the typeName string is treated as assembly name. To make it work,yo
u
> need to specify "YourAssembly.dll" in typeName. In contrast,
> Type.GetType("System.Int32") can return type System.Int32 without mentioni
ng
> "mscorlib.dll".
> Type.AssemblyQualifiedName is the right way to tell what kind of typeName
> you should provide.
> typeof(System.Data.SqlClient.SqlException).AssemblyQualifiedName equals:
> "System.Data.SqlClient.SqlException, System.Data, Version=2.0.0.0,
> Culture=neutral, PublicKeyToken=b77a5c561934e089". Type.GetType with this
> long string will succeed in .NET 2.0.
> In .NET, namespace and the associated assembly name are not necessarily
> related. Type "System.Data.SqlClient.SqlException" is not associated with
> "System.Data.SqlClient.dll" (which does not exist).
> -- Peter
> Site: http://www.eggheadcafe.com
> UnBlog: http://petesbloggerama.blogspot.com
> Short urls & more: http://ittyurl.net
>
>
> "Jeff" wrote:
>
thank you guys, you helped me solve this problem.
This is a bit embarrassing. I tryed to create an instance of this class:
min.test.DAL.SqlServer2005, but this class don't exist in my project.
min.test.DAL.SqlServer2005.SqlServer2005 is the class I needed... I had
forgot that I had a namespace with the same name as the class...
anyway I changed the .SqlServer2005 namespace to SqlClient so now I'm
accessing min.test.DAL.SqlClient.SqlServer2005
once again, thank you
cheers
Jeff
"Milosz Skalecki [MCAD]" <mily242@.DONTLIKESPAMwp.pl> wrote in message
news:FB62B8F3-8DAE-4E81-917C-935316F2CFE8@.microsoft.com...
> Jeff,
> In addition to Peter's reply, there's another way of obtaining the
> reference
> in ASP.NET 2.0 dynamic compilation enviroment. Please use
> System.Web.Compilation.BuildManager.GetType(min.test.Config.Settings.msgEl
ement.ProviderType,
> true);
> instead.
> Regards
> --
> Milosz
>
> "Peter Bromberg [C# MVP]" wrote:
>
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment