Saturday, March 24, 2012

newbie: runtime error

Hey

asp.net 2.0

When I run the code below I get a runtime error ("Object reference not set
to an instance of an object. "):

Here is the code:

public static ICollection getInbox()
{
ArrayList al = new ArrayList();
ConnectionStringSettings css =
ConfigurationManager.ConnectionStrings["aspnet_DB"];
SqlDataSource sds = new SqlDataSource(css.ConnectionString, "SELECT
UserName from aspnet_Users");
try
{
IEnumerable INames = sds.Select(DataSourceSelectArguments.Empty);
foreach (DataRowView row in INames)
{
al.Add(row["UserName"]);
}
}
catch (SqlException sqlException)
{
Debug.WriteLine(sqlException.Message.ToString());
}
sds.Dispose();
return al;
}

The error occur at this line in the code:
SqlDataSource sds = new SqlDataSource(css.ConnectionString, "SELECT
UserName from aspnet_Users");

Just in case here is my web.config file:
<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<appSettings>
<add key="aspnet_DB" value="Data Source=.\\SQLEXPRESS;Integrated
Security=True;AttachDBFilename=|DataDirectory|aspn etdb.mdf;User
Instance=true" />
</appSettings>
<system.web>
<roleManager enabled="true"/>
<authentication mode="Forms"/>
<compilation debug="true"/>
<profile enabled="true">
<properties>
<add name="Picture" type="System.Byte[]" />
<add name="PictureType" type="string" />
</properties>
</profile>
</system.web>
</configuration>

Any ideas what I'm doing wrong here?

JeffJeff wrote:
> Hey
>
> asp.net 2.0
>
>
> When I run the code below I get a runtime error ("Object reference not set
> to an instance of an object. "):
>
> Here is the code:
>
> public static ICollection getInbox()
> {
> ArrayList al = new ArrayList();
> ConnectionStringSettings css =
> ConfigurationManager.ConnectionStrings["aspnet_DB"];
> SqlDataSource sds = new SqlDataSource(css.ConnectionString, "SELECT
> UserName from aspnet_Users");
> try
> {
> IEnumerable INames = sds.Select(DataSourceSelectArguments.Empty);
> foreach (DataRowView row in INames)
> {
> al.Add(row["UserName"]);
> }
> }
> catch (SqlException sqlException)
> {
> Debug.WriteLine(sqlException.Message.ToString());
> }
> sds.Dispose();
> return al;
> }
>
> The error occur at this line in the code:
> SqlDataSource sds = new SqlDataSource(css.ConnectionString, "SELECT
> UserName from aspnet_Users");
>
> Just in case here is my web.config file:
> <?xml version="1.0"?>
> <configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
> <appSettings>
> <add key="aspnet_DB" value="Data Source=.\\SQLEXPRESS;Integrated
> Security=True;AttachDBFilename=|DataDirectory|aspn etdb.mdf;User
> Instance=true" />
> </appSettings>
> <system.web>
> <roleManager enabled="true"/>
> <authentication mode="Forms"/>
> <compilation debug="true"/>
> <profile enabled="true">
> <properties>
> <add name="Picture" type="System.Byte[]" />
> <add name="PictureType" type="string" />
> </properties>
> </profile>
> </system.web>
> </configuration>
>
> Any ideas what I'm doing wrong here?
>
> Jeff

Hi Jeff,

If looks like css is null. I'm guessing this is because there is no
<connectionStrings> in the web.config.

See http://www.devx.com/dotnet/Article/27562
--
David Hogue

0 comments:

Post a Comment