Null值停止代码模块。 [英] Null Values stopping code module.

查看:65
本文介绍了Null值停止代码模块。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下三个文件。

1. Users.aspx是一个网页,它使用< asp:ObjectDataSourcecontrol来支持
填充一个简单的< asp: ListBoxcontrol。

2. UserDetails.cs文件创建一个名为UserComponents的命名空间,

创建一个名为UserDetails的对象。

3. UserDB。 cs文件从数据库中检索实际数据。


下面的代码已经压缩,只包含我的

数据源中的两个字段。我实际上也是

检索FirstName,MiddleName等。只要没有

用户有一个UserName等于

NULL这个代码工作正常。如果我在UserDetails对象中包含任何字段,我在第46行得到

跟随错误。


无法转换类型为''System的对象.DBNull''键入''System.String''。

第46行:UserDetails user = new UserDetails((int)rdrUsers [" UserID"],

第47行:

(字符串)rdrUsers [" UserName"],


无论设置哪个字段,我都会收到错误< DataTextField>。如果我

确保所有记录都有一个

UserName未设置为NULL,则代码效果很好。这可能会成为一个大问题

因为在很多情况下

并非每个字段都包含一个值。我以为我只想设置每个字段的

默认值

到一个空白区域,因为这样可以防止错误发生,但我不是这样。

我确定要这样做。


在我看来,这段代码应该使用NULL值.NULL值frus trate

我有时候。有没有办法

我可以使下面列出的代码使用NULL值吗?仅供参考:我的一个想法是为每个字段检查一个NULL

值,但这听起来不是一个好的解决方案。


任何人都可以提供一些有关我如何使以下代码工作的见解

当任何源字段

可能包含NULL值?


Users.aspx(显示带有UserNames的列表框的网页)


< asp:ObjectDataSource

ID = QUOT; sourceUsers" runat =" server"

TypeName =" UserComponents.UserDB" SelectMethod =" GetUsers">

< / asp:ObjectDataSource>

< asp:ListBox

ID =" lstUserID" RUNAT = QUOT;服务器" DataSourceID =" sourceUsers"

DataTextField =" UserName" Width =" 224px">

< / asp:ListBox>


UserDetails.cs(在App_Code文件夹中)

namespace UserComponents

{

public class UserDetails

{

public int UserID

{

get {return m_userID; }

set {m_userID = value; }

}

公共字符串UserName

{

get {return m_userName; }

set {m_userName = value; }

}


public UserDetails(int userID,string userName)

{

this。 UserID = userID;

this.UserName = userName;

}

public UserDetails()

{

//默认构造函数

}

}

}

UserDB.cs(in App_Code文件夹)

命名空间UserComponents

{

[DataObject]

公共类UserDB

{

[DataObjectMethod(DataObjectMethodType.Select,true)]

public List< UserDetailsGetUsers()

{

SqlConnection sqlConn = new SqlConnection(m_strConn);

SqlCommand cmdUsers = new SqlCommand(" sUsers",sqlConn);

cmdUsers.CommandType = CommandType.StoredProcedure;

//创建所有用户的集合。

列表< UserDetailsusers =新列表< UserDetails>();

尝试

$

sqlConn.Open();

SqlDataReader rdrUsers = cmdUsers .ExecuteReader();


while(rdrUsers.Read())

{

UserDetails user = new UserDetails((int )rdrUsers [" UserID"],

(字符串)rdrUsers [" UserName"]);

users.Add(user);

}

rdrUsers.Close();

返回用户;

}

catch(SqlException ex)

{

抛出新的ApplicationException(" Error Occured");

}

终于

{

sqlConn.Close();

}

}

}

}}

I have the following three files.
1. Users.aspx is a webpage that uses the <asp:ObjectDataSourcecontrol to
populate a simple <asp:ListBoxcontrol.
2. The UserDetails.cs file creates a Namespace named UserComponents and
creates an object named UserDetails.
3. The UserDB.cs file retrieves the actual data from the database.

The code below has been condensed and only includes two fields from my
datasource. I actually am also
retrieving FirstName, MiddleName, etc. This code works fine as long as no
user has a UserName equal to
NULL. If any of the fields I include in the UserDetails object, I get the
following error on line 46.

Unable to cast object of type ''System.DBNull'' to type ''System.String''.

Line 46: UserDetails user = new UserDetails((int)rdrUsers["UserID"],
Line 47:
(string)rdrUsers["UserName"],

I get the error regardless of what field is set as the <DataTextField>. If I
make sure all records have a
UserName not set to NULL the code works great. This can become a big problem
because in many cases
not every field will contain a value. I''ve thought I woudl just set the
default value of each field
to a blank space, as this will stop the error from happening, but I''m not so
sure I want to do this.

It seems to me this code should work with NULL values. NULL Values frustrate
me at times. Is there a way
I can make the code listed below work with NULL values? FYI: One of my
thoughts was to check for a NULL
value for each field, but this even doesn''t sound like a good solution.

Can anyone provide some insight as to how I can make the following code work
when any of the source fields
may contain a NULL value?

Users.aspx (Web Page displaying listbox with UserNames)

<asp:ObjectDataSource
ID="sourceUsers" runat="server"
TypeName="UserComponents.UserDB" SelectMethod="GetUsers">
</asp:ObjectDataSource>
<asp:ListBox
ID="lstUserID" runat="server" DataSourceID="sourceUsers"
DataTextField="UserName" Width="224px">
</asp:ListBox>

UserDetails.cs (in App_Code folder)
namespace UserComponents
{
public class UserDetails
{
public int UserID
{
get { return m_userID; }
set { m_userID = value; }
}
public string UserName
{
get { return m_userName; }
set { m_userName = value; }
}

public UserDetails(int userID, string userName)
{
this.UserID = userID;
this.UserName = userName;
}
public UserDetails()
{
// Default Constructor
}
}
}
UserDB.cs (in App_Code folder)
namespace UserComponents
{
[DataObject]
public class UserDB
{
[DataObjectMethod(DataObjectMethodType.Select, true)]
public List<UserDetailsGetUsers()
{
SqlConnection sqlConn = new SqlConnection(m_strConn);
SqlCommand cmdUsers = new SqlCommand("sUsers", sqlConn);
cmdUsers.CommandType = CommandType.StoredProcedure;
// Create collection of all Users.
List<UserDetailsusers = new List<UserDetails>();
try
{
sqlConn.Open();
SqlDataReader rdrUsers = cmdUsers.ExecuteReader();

while (rdrUsers.Read())
{
UserDetails user = new UserDetails( (int)rdrUsers["UserID"],
(string)rdrUsers["UserName"]);
users.Add(user);
}
rdrUsers.Close();
return users;
}
catch (SqlException ex)
{
throw new ApplicationException ("Error Occured");
}
finally
{
sqlConn.Close();
}
}
}
}}


推荐答案

您需要在使用之前测试DBNull的值


String UserID ="" ;;


if(rdrUsers [" UserID"] = System.DBNull)

{ UserID ="" ;;}

else

{UserID = rdrUsers [" UserID"];}


那里是读者的IsDBNull函数,但我认为它只需要一个

序数,而不是列名。我可能错了。如果我没错,你可以使用自己的函数将datareader值作为输入,并使用上述逻辑返回一个字符串。

>

" Greg"写道:
You need to test the values for DBNull before using

String UserID = "";

if (rdrUsers["UserID"] = System.DBNull)
{UserID= "";}
else
{ UserID = rdrUsers["UserID"];}

There is an IsDBNull function on the reader, but I think it only takes an
ordinal, not a column name. I could be wrong on this. If I''m not wrong, you
can make your own function that takes the datareader value as input, and
returns a string using the above logic.

"Greg" wrote:

我有以下三个文件。

1. Users.aspx是一个使用< asp:ObjectDataSourcecontrol的网页

填充一个简单的< asp:ListBoxcontrol。

2. UserDetails.cs文件创建一个名为UserComponents的命名空间,

创建一个名为的对象UserDetails。

3. UserDB.cs文件从数据库中检索实际数据。


下面的代码已经压缩,只包括我的两个字段

数据源。我实际上也是

检索FirstName,MiddleName等。只要没有

用户有一个UserName等于

NULL这个代码工作正常。如果我在UserDetails对象中包含任何字段,我在第46行得到

跟随错误。


无法转换类型为''System的对象.DBNull''键入''System.String''。

第46行:UserDetails user = new UserDetails((int)rdrUsers [" UserID"],

第47行:

(字符串)rdrUsers [" UserName"],


无论设置哪个字段,我都会收到错误< DataTextField>。如果我

确保所有记录都有一个

UserName未设置为NULL,则代码效果很好。这可能会成为一个大问题

因为在很多情况下

并非每个字段都包含一个值。我以为我只想设置每个字段的

默认值

到一个空白区域,因为这样可以防止错误发生,但我不是这样。

我确定要这样做。


在我看来,这段代码应该使用NULL值.NULL值沮丧e

我有时候。有没有办法

我可以使下面列出的代码使用NULL值吗?仅供参考:我的一个想法是为每个字段检查一个NULL

值,但这听起来不是一个好的解决方案。


任何人都可以提供一些有关我如何使以下代码工作的见解

当任何源字段

可能包含NULL值?


Users.aspx(显示带有UserNames的列表框的网页)


< asp:ObjectDataSource

ID = QUOT; sourceUsers" runat =" server"

TypeName =" UserComponents.UserDB" SelectMethod =" GetUsers">

< / asp:ObjectDataSource>

< asp:ListBox

ID =" lstUserID" RUNAT = QUOT;服务器" DataSourceID =" sourceUsers"

DataTextField =" UserName" Width =" 224px">

< / asp:ListBox>


UserDetails.cs(在App_Code文件夹中)

namespace UserComponents

{

public class UserDetails

{

public int UserID

{

get {return m_userID; }

set {m_userID = value; }

}

公共字符串UserName

{

get {return m_userName; }

set {m_userName = value; }

}


public UserDetails(int userID,string userName)

{

this。 UserID = userID;

this.UserName = userName;

}

public UserDetails()

{

//默认构造函数

}

}

}

UserDB.cs(in App_Code文件夹)

命名空间UserComponents

{

[DataObject]

公共类UserDB

{

[DataObjectMethod(DataObjectMethodType.Select,true)]

public List< UserDetailsGetUsers()

{

SqlConnection sqlConn = new SqlConnection(m_strConn);

SqlCommand cmdUsers = new SqlCommand(" sUsers",sqlConn);

cmdUsers.CommandType = CommandType.StoredProcedure;

//创建所有用户的集合。

列表< UserDetailsusers =新列表< UserDetails>();

try

{

sqlConn.Open();

SqlDataReader rdrUsers = cmdUsers.ExecuteReader();


while(rdrUsers.Read())

{

UserDetails user = new UserDetails((int)rdrUsers [" UserID"],

(字符串)rdrUsers [" UserName"]);

users.Add(user);

}

rdrUsers.Close();

返回用户;

}

catch(SqlException ex)

{

抛出新的ApplicationException(" Error Occured");

}

终于

{

sqlConn.Close();

}

}

}

}}

I have the following three files.
1. Users.aspx is a webpage that uses the <asp:ObjectDataSourcecontrol to
populate a simple <asp:ListBoxcontrol.
2. The UserDetails.cs file creates a Namespace named UserComponents and
creates an object named UserDetails.
3. The UserDB.cs file retrieves the actual data from the database.

The code below has been condensed and only includes two fields from my
datasource. I actually am also
retrieving FirstName, MiddleName, etc. This code works fine as long as no
user has a UserName equal to
NULL. If any of the fields I include in the UserDetails object, I get the
following error on line 46.

Unable to cast object of type ''System.DBNull'' to type ''System.String''.

Line 46: UserDetails user = new UserDetails((int)rdrUsers["UserID"],
Line 47:
(string)rdrUsers["UserName"],

I get the error regardless of what field is set as the <DataTextField>. If I
make sure all records have a
UserName not set to NULL the code works great. This can become a big problem
because in many cases
not every field will contain a value. I''ve thought I woudl just set the
default value of each field
to a blank space, as this will stop the error from happening, but I''m not so
sure I want to do this.

It seems to me this code should work with NULL values. NULL Values frustrate
me at times. Is there a way
I can make the code listed below work with NULL values? FYI: One of my
thoughts was to check for a NULL
value for each field, but this even doesn''t sound like a good solution.

Can anyone provide some insight as to how I can make the following code work
when any of the source fields
may contain a NULL value?

Users.aspx (Web Page displaying listbox with UserNames)

<asp:ObjectDataSource
ID="sourceUsers" runat="server"
TypeName="UserComponents.UserDB" SelectMethod="GetUsers">
</asp:ObjectDataSource>
<asp:ListBox
ID="lstUserID" runat="server" DataSourceID="sourceUsers"
DataTextField="UserName" Width="224px">
</asp:ListBox>

UserDetails.cs (in App_Code folder)
namespace UserComponents
{
public class UserDetails
{
public int UserID
{
get { return m_userID; }
set { m_userID = value; }
}
public string UserName
{
get { return m_userName; }
set { m_userName = value; }
}

public UserDetails(int userID, string userName)
{
this.UserID = userID;
this.UserName = userName;
}
public UserDetails()
{
// Default Constructor
}
}
}
UserDB.cs (in App_Code folder)
namespace UserComponents
{
[DataObject]
public class UserDB
{
[DataObjectMethod(DataObjectMethodType.Select, true)]
public List<UserDetailsGetUsers()
{
SqlConnection sqlConn = new SqlConnection(m_strConn);
SqlCommand cmdUsers = new SqlCommand("sUsers", sqlConn);
cmdUsers.CommandType = CommandType.StoredProcedure;
// Create collection of all Users.
List<UserDetailsusers = new List<UserDetails>();
try
{
sqlConn.Open();
SqlDataReader rdrUsers = cmdUsers.ExecuteReader();

while (rdrUsers.Read())
{
UserDetails user = new UserDetails( (int)rdrUsers["UserID"],
(string)rdrUsers["UserName"]);
users.Add(user);
}
rdrUsers.Close();
return users;
}
catch (SqlException ex)
{
throw new ApplicationException ("Error Occured");
}
finally
{
sqlConn.Close();
}
}
}
}}


拼写错误警告:


if(rdrUsers [" UserID"] == System.DBNull)

{U serID ="" ;;}

else

{UserID = rdrUsers [" UserID"];}

" Chuck D"写道:
typo alert:

if (rdrUsers["UserID"] == System.DBNull)
{UserID= "";}
else
{ UserID = rdrUsers["UserID"];}
"Chuck D" wrote:

你需要在使用之前测试DBNull的值


String UserID ="" ;;


if(rdrUsers [" UserID"] = System.DBNull)

{UserID ="" ;;}

else

{UserID = rdrUsers [" UserID"];}


读卡器上有一个IsDBNull函数,但我认为它只需要一个

ordinal,不是列名。我可能错了。如果我没错,你可以使用自己的函数将datareader值作为输入,并使用上述逻辑返回一个字符串。

>

" Greg"写道:
You need to test the values for DBNull before using

String UserID = "";

if (rdrUsers["UserID"] = System.DBNull)
{UserID= "";}
else
{ UserID = rdrUsers["UserID"];}

There is an IsDBNull function on the reader, but I think it only takes an
ordinal, not a column name. I could be wrong on this. If I''m not wrong, you
can make your own function that takes the datareader value as input, and
returns a string using the above logic.

"Greg" wrote:

我有以下三个文件。

1. Users.aspx是一个使用< asp:ObjectDataSourcecontrol的网页

填充一个简单的< asp:ListBoxcontrol。

2. UserDetails.cs文件创建一个名为UserComponents的命名空间,

创建一个名为的对象UserDetails。

3. UserDB.cs文件从数据库中检索实际数据。


下面的代码已经压缩,只包括我的两个字段

数据源。我实际上也是

检索FirstName,MiddleName等。只要没有

用户有一个UserName等于

NULL这个代码工作正常。如果我在UserDetails对象中包含任何字段,我在第46行得到

跟随错误。


无法转换类型为''System的对象.DBNull''键入''System.String''。

第46行:UserDetails user = new UserDetails((int)rdrUsers [" UserID"],

第47行:

(字符串)rdrUsers [" UserName"],


无论设置哪个字段,我都会收到错误< DataTextField>。如果我

确保所有记录都有一个

UserName未设置为NULL,则代码效果很好。这可能会成为一个大问题

因为在很多情况下

并非每个字段都包含一个值。我以为我只想设置每个字段的

默认值

到一个空白区域,因为这样可以防止错误发生,但我不是这样。

我确定要这样做。


在我看来,这个代码应该使用NULL值.NU LL值令人沮丧

我有时候。有没有办法

我可以使下面列出的代码使用NULL值吗?仅供参考:我的一个想法是为每个字段检查一个NULL

值,但这听起来不是一个好的解决方案。


任何人都可以提供一些有关我如何使以下代码工作的见解

当任何源字段

可能包含NULL值?


Users.aspx(显示带有UserNames的列表框的网页)


< asp:ObjectDataSource

ID = QUOT; sourceUsers" runat =" server"

TypeName =" UserComponents.UserDB" SelectMethod =" GetUsers">

< / asp:ObjectDataSource>

< asp:ListBox

ID =" lstUserID" RUNAT = QUOT;服务器" DataSourceID =" sourceUsers"

DataTextField =" UserName" Width =" 224px">

< / asp:ListBox>


UserDetails.cs(在App_Code文件夹中)

namespace UserComponents

{

public class UserDetails

{

public int UserID

{

get {return m_userID; }

set {m_userID = value; }

}

公共字符串UserName

{

get {return m_userName; }

set {m_userName = value; }

}


public UserDetails(int userID,string userName)

{

this。 UserID = userID;

this.UserName = userName;

}

public UserDetails()

{

//默认构造函数

}

}

}

UserDB.cs(in App_Code文件夹)

命名空间UserComponents

{

[DataObject]

公共类UserDB

{

[DataObjectMethod(DataObjectMethodType.Select,true)]

public List< UserDetailsGetUsers()

{

SqlConnection sqlConn = new SqlConnection(m_strConn);

SqlCommand cmdUsers = new SqlCommand(" sUsers",sqlConn);

cmdUsers.CommandType = CommandType.StoredProcedure;

//创建所有用户的集合。

列表< UserDetailsusers = new List&l t; UserDetails>();

尝试

{

sqlConn.Open();

SqlDataReader rdrUsers = cmdUsers.ExecuteReader();


while(rdrUsers.Read())

{

UserDetails user = new UserDetails(( int)rdrUsers [" UserID"],

(string)rdrUsers [" UserName"]);

users.Add(user);

}

rdrUsers.Close();

返回用户;

}

catch(SqlException ex )

{

抛出新的ApplicationException(" Error Occured");

}

finally

{

sqlConn.Close();

}

}

}

}}


I have the following three files.
1. Users.aspx is a webpage that uses the <asp:ObjectDataSourcecontrol to
populate a simple <asp:ListBoxcontrol.
2. The UserDetails.cs file creates a Namespace named UserComponents and
creates an object named UserDetails.
3. The UserDB.cs file retrieves the actual data from the database.

The code below has been condensed and only includes two fields from my
datasource. I actually am also
retrieving FirstName, MiddleName, etc. This code works fine as long as no
user has a UserName equal to
NULL. If any of the fields I include in the UserDetails object, I get the
following error on line 46.

Unable to cast object of type ''System.DBNull'' to type ''System.String''.

Line 46: UserDetails user = new UserDetails((int)rdrUsers["UserID"],
Line 47:
(string)rdrUsers["UserName"],

I get the error regardless of what field is set as the <DataTextField>. If I
make sure all records have a
UserName not set to NULL the code works great. This can become a big problem
because in many cases
not every field will contain a value. I''ve thought I woudl just set the
default value of each field
to a blank space, as this will stop the error from happening, but I''m not so
sure I want to do this.

It seems to me this code should work with NULL values. NULL Values frustrate
me at times. Is there a way
I can make the code listed below work with NULL values? FYI: One of my
thoughts was to check for a NULL
value for each field, but this even doesn''t sound like a good solution.

Can anyone provide some insight as to how I can make the following code work
when any of the source fields
may contain a NULL value?

Users.aspx (Web Page displaying listbox with UserNames)

<asp:ObjectDataSource
ID="sourceUsers" runat="server"
TypeName="UserComponents.UserDB" SelectMethod="GetUsers">
</asp:ObjectDataSource>
<asp:ListBox
ID="lstUserID" runat="server" DataSourceID="sourceUsers"
DataTextField="UserName" Width="224px">
</asp:ListBox>

UserDetails.cs (in App_Code folder)
namespace UserComponents
{
public class UserDetails
{
public int UserID
{
get { return m_userID; }
set { m_userID = value; }
}
public string UserName
{
get { return m_userName; }
set { m_userName = value; }
}

public UserDetails(int userID, string userName)
{
this.UserID = userID;
this.UserName = userName;
}
public UserDetails()
{
// Default Constructor
}
}
}
UserDB.cs (in App_Code folder)
namespace UserComponents
{
[DataObject]
public class UserDB
{
[DataObjectMethod(DataObjectMethodType.Select, true)]
public List<UserDetailsGetUsers()
{
SqlConnection sqlConn = new SqlConnection(m_strConn);
SqlCommand cmdUsers = new SqlCommand("sUsers", sqlConn);
cmdUsers.CommandType = CommandType.StoredProcedure;
// Create collection of all Users.
List<UserDetailsusers = new List<UserDetails>();
try
{
sqlConn.Open();
SqlDataReader rdrUsers = cmdUsers.ExecuteReader();

while (rdrUsers.Read())
{
UserDetails user = new UserDetails( (int)rdrUsers["UserID"],
(string)rdrUsers["UserName"]);
users.Add(user);
}
rdrUsers.Close();
return users;
}
catch (SqlException ex)
{
throw new ApplicationException ("Error Occured");
}
finally
{
sqlConn.Close();
}
}
}
}}



查克,


我尝试了你所提供的东西。我得到了同样的错误

我也得到了这个样本。这是我添加到我的源代码

代码。在这种情况下,我正在检查名为" password的字段的值。


string password ="" ;;

if(rdrUsers [" Password"] == System.DBNull)

{password ="" ;; }

else

{password = rdrUsers [" Password"]; }


我最初启动Web应用程序时收到以下错误消息,

甚至到达使用此代码模块的页面。


CS0119:''System.DBNull''是''type'',在给定的上下文中无效


有什么想法?

" Chuck D"写道:
Chuck,

I tried something along the line of what you provided. I got the same error
that I am getting with this sample as well. Here is what I added to my source
code. In this case, I am checking the value of a field named: "password".

string password = "";
if (rdrUsers["Password"] == System.DBNull)
{ password = ""; }
else
{ password = rdrUsers["Password"]; }

I get the following error message when I initially start my web application,
before even getting to the page that uses this code module.

CS0119: ''System.DBNull'' is a ''type'', which is not valid in the given context

Any ideas?
"Chuck D" wrote:

错字警报:


if(rdrUsers [" UserID"] == System.DBNull)

{UserID ="" ;;}

else

{UserID = rdrUsers [" UserID"];}


" Chuck D"写道:
typo alert:

if (rdrUsers["UserID"] == System.DBNull)
{UserID= "";}
else
{ UserID = rdrUsers["UserID"];}
"Chuck D" wrote:

你需要在使用之前测试DBNull的值


String UserID ="" ;;


if(rdrUsers [" UserID"] = System.DBNull)

{UserID ="" ;;}

else

{UserID = rdrUsers [" UserID"];}


读卡器上有一个IsDBNull函数,但我认为它只需要一个

ordinal,不是列名。我可能错了。如果我没错,你可以使用自己的函数将datareader值作为输入,并使用上述逻辑返回一个字符串。

>

" Greg"写道:
You need to test the values for DBNull before using

String UserID = "";

if (rdrUsers["UserID"] = System.DBNull)
{UserID= "";}
else
{ UserID = rdrUsers["UserID"];}

There is an IsDBNull function on the reader, but I think it only takes an
ordinal, not a column name. I could be wrong on this. If I''m not wrong, you
can make your own function that takes the datareader value as input, and
returns a string using the above logic.

"Greg" wrote:

我有以下三个文件。

1. Users.aspx是一个使用< asp:ObjectDataSourcecontrol的网页

填充一个简单的< asp:ListBoxcontrol。

2. UserDetails.cs文件创建一个名为UserComponents的命名空间,

创建一个名为的对象UserDetails。

3. UserDB.cs文件从数据库中检索实际数据。

>

以下代码已经压缩,只包括我的

数据源中的两个字段。我实际上也是

检索FirstName,MiddleName等。只要没有

用户有一个UserName等于

NULL这个代码工作正常。如果我在UserDetails对象中包含任何字段,我会在第46行收到

跟随错误。

>

无法投射类型''System.DBNull''的对象输入''System.String''。

>

第46行:UserDetails user = new UserDetails((int) rdrUsers [" UserID"],

第47行:

(字符串)rdrUsers [" UserName"],

>

无论将哪个字段设置为< DataTextField>,我都会收到错误。如果我

,请确保所有记录都有一个

用户名未设置为了NULL,代码工作得很好。这可能会成为一个大问题

因为在很多情况下

并非每个字段都包含一个值。我以为我只是设置了woudl

每个字段的默认值

到一个空格,因为这会阻止错误发生,但我不是这样的
$ b $我肯定要这样做。

>

在我看来代码应该使用NULL值。 NULL值令人沮丧

我有时候。有没有办法

我可以使下面列出的代码使用NULL值吗?仅供参考:我的一个想法是为每个字段检查一个NULL

值,但这听起来不是一个好的解决方案。

>

任何人都可以提供一些见解,了解我如何使以下代码工作

当任何源字段

可能包含NULL值?

>

Users.aspx(显示带有UserNames的列表框的网页)

>

< asp:ObjectDataSource

ID =" sourceUsers" runat =" server"

TypeName =" UserComponents.UserDB" SelectMethod =" GetUsers">

< / asp:ObjectDataSource>

< asp:ListBox

ID =" lstUserID" RUNAT = QUOT;服务器" DataSourceID =" sourceUsers"

DataTextField =" UserName" Width =" 224px">

< / asp:ListBox>

>

UserDetails.cs(在App_Code文件夹中)< br $>
命名空间UserComponents

{

公共类UserDetails

{

public int UserID

{

get {return m_userID; }

set {m_userID = value; }

}

公共字符串UserName

{

get {return m_userName; }

set {m_userName = value; }

}

>

public UserDetails(int userID,string userName)

{

this.UserID = userID;

this.UserName = userName;

}

public UserDetails()

{

//默认构造函数

}

}

}

UserDB.cs(在App_Code文件夹中)

命名空间UserComponents

{

[DataObject]

公共类UserDB

{

[DataObjectMethod(DataObjectMethodType.Select,true)]

public List< UserDetailsGetUsers()

{

SqlConnection sqlConn = new SqlConnection(m_strConn);

SqlCommand cmdUsers = new SqlCommand(" sUsers",sqlConn);

cmdUsers.CommandType = CommandType.StoredProcedure;

//创建所有用户的集合。

列表< UserDetailsusers =新列表< UserDetails>();

尝试

{

sqlConn.Open();

SqlDataReader rdrUsers = cmdUsers.ExecuteReader();

>

while(rdrUsers.Read())

{

UserDetails user = new UserDetails((int)rdrUsers [" UserID"],

(string)rdrUsers [" UserName"]);

users.Add(user );

}

rdrUsers.Close();

返回用户;

}

catch(SqlException ex)

{

抛出新的ApplicationException(" Error Occured");

}

终于

{

sqlConn.Close();

}

}

}

}}

>

>

>

>
I have the following three files.
1. Users.aspx is a webpage that uses the <asp:ObjectDataSourcecontrol to
populate a simple <asp:ListBoxcontrol.
2. The UserDetails.cs file creates a Namespace named UserComponents and
creates an object named UserDetails.
3. The UserDB.cs file retrieves the actual data from the database.
>
The code below has been condensed and only includes two fields from my
datasource. I actually am also
retrieving FirstName, MiddleName, etc. This code works fine as long as no
user has a UserName equal to
NULL. If any of the fields I include in the UserDetails object, I get the
following error on line 46.
>
Unable to cast object of type ''System.DBNull'' to type ''System.String''.
>
Line 46: UserDetails user = new UserDetails((int)rdrUsers["UserID"],
Line 47:
(string)rdrUsers["UserName"],
>
I get the error regardless of what field is set as the <DataTextField>. If I
make sure all records have a
UserName not set to NULL the code works great. This can become a big problem
because in many cases
not every field will contain a value. I''ve thought I woudl just set the
default value of each field
to a blank space, as this will stop the error from happening, but I''m not so
sure I want to do this.
>
It seems to me this code should work with NULL values. NULL Values frustrate
me at times. Is there a way
I can make the code listed below work with NULL values? FYI: One of my
thoughts was to check for a NULL
value for each field, but this even doesn''t sound like a good solution.
>
Can anyone provide some insight as to how I can make the following code work
when any of the source fields
may contain a NULL value?
>
Users.aspx (Web Page displaying listbox with UserNames)
>
<asp:ObjectDataSource
ID="sourceUsers" runat="server"
TypeName="UserComponents.UserDB" SelectMethod="GetUsers">
</asp:ObjectDataSource>
<asp:ListBox
ID="lstUserID" runat="server" DataSourceID="sourceUsers"
DataTextField="UserName" Width="224px">
</asp:ListBox>
>
UserDetails.cs (in App_Code folder)
namespace UserComponents
{
public class UserDetails
{
public int UserID
{
get { return m_userID; }
set { m_userID = value; }
}
public string UserName
{
get { return m_userName; }
set { m_userName = value; }
}
>
public UserDetails(int userID, string userName)
{
this.UserID = userID;
this.UserName = userName;
}
public UserDetails()
{
// Default Constructor
}
}
}
UserDB.cs (in App_Code folder)
namespace UserComponents
{
[DataObject]
public class UserDB
{
[DataObjectMethod(DataObjectMethodType.Select, true)]
public List<UserDetailsGetUsers()
{
SqlConnection sqlConn = new SqlConnection(m_strConn);
SqlCommand cmdUsers = new SqlCommand("sUsers", sqlConn);
cmdUsers.CommandType = CommandType.StoredProcedure;
// Create collection of all Users.
List<UserDetailsusers = new List<UserDetails>();
try
{
sqlConn.Open();
SqlDataReader rdrUsers = cmdUsers.ExecuteReader();
>
while (rdrUsers.Read())
{
UserDetails user = new UserDetails( (int)rdrUsers["UserID"],
(string)rdrUsers["UserName"]);
users.Add(user);
}
rdrUsers.Close();
return users;
}
catch (SqlException ex)
{
throw new ApplicationException ("Error Occured");
}
finally
{
sqlConn.Close();
}
}
}
}}
>
>
>
>


这篇关于Null值停止代码模块。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆