本文介绍了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 topopulate a simple <asp:ListBoxcontrol.2. The UserDetails.cs file creates a Namespace named UserComponents andcreates 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 mydatasource. I actually am alsoretrieving FirstName, MiddleName, etc. This code works fine as long as nouser has a UserName equal toNULL. If any of the fields I include in the UserDetails object, I get thefollowing 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 Imake sure all records have aUserName not set to NULL the code works great. This can become a big problembecause in many casesnot every field will contain a value. I''ve thought I woudl just set thedefault value of each fieldto a blank space, as this will stop the error from happening, but I''m not sosure I want to do this. It seems to me this code should work with NULL values. NULL Values frustrateme at times. Is there a wayI can make the code listed below work with NULL values? FYI: One of mythoughts was to check for a NULLvalue 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 workwhen any of the source fieldsmay contain a NULL value? Users.aspx (Web Page displaying listbox with UserNames) <asp:ObjectDataSourceID="sourceUsers" runat="server"TypeName="UserComponents.UserDB" SelectMethod="GetUsers"></asp:ObjectDataSource><asp:ListBoxID="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值停止代码模块。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-28 12:42