This code crashes, what am I doing wrong????

J

Jeff

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.msgElement.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.msgElement.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
 
G

Guest

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.msgElement.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
 
J

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 said:
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.msgElement.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 said:
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.msgElement.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.msgElement.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
 
G

Guest

Type.GetType gives you the ability to get a type back from a string. You pass
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 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 said:
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 said:
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.msgElement.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 said:
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.msgElement.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.msgElement.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
 
G

Guest

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.msgElement.ProviderType, true);
instead.

Regards
--
Milosz


Peter Bromberg said:
Type.GetType gives you the ability to get a type back from a string. You pass
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 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 said:
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 said:
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.msgElement.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




:

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.msgElement.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.msgElement.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
 
J

Jeff

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 said:
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.msgElement.ProviderType,
true);
instead.

Regards
--
Milosz


Peter Bromberg said:
Type.GetType gives you the ability to get a type back from a string. You
pass
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
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 said:
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

message 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.msgElement.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




:

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.msgElement.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.msgElement.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
 

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,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top