G
Greg
I have the following three files.
1. Users.aspx is a webpage that uses the <asp:ObjectDataSource> control to
populate a simple <asp:ListBox> control.
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<UserDetails> GetUsers()
{
SqlConnection sqlConn = new SqlConnection(m_strConn);
SqlCommand cmdUsers = new SqlCommand("sUsers", sqlConn);
cmdUsers.CommandType = CommandType.StoredProcedure;
// Create collection of all Users.
List<UserDetails> users = 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();
}
}
}
}}
1. Users.aspx is a webpage that uses the <asp:ObjectDataSource> control to
populate a simple <asp:ListBox> control.
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<UserDetails> GetUsers()
{
SqlConnection sqlConn = new SqlConnection(m_strConn);
SqlCommand cmdUsers = new SqlCommand("sUsers", sqlConn);
cmdUsers.CommandType = CommandType.StoredProcedure;
// Create collection of all Users.
List<UserDetails> users = 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();
}
}
}
}}