J
Jonathan Wood
The following code raises the error "Specified cast is not valid."
MembershipUser user = Membership.GetUser(userId);
DataSet ds = DataLayer.ExecQueryData("SELECT * FROM mc_Clients WHERE
UserId='" + userId.ToString() + "'");
if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
return null;
ClientEx client = new ClientEx();
client.ClientData = new Client();
client.ClientData.Email = user.UserName;
client.ClientData.UserID = user.ProviderUserKey;
client.ClientData.LastLogin = user.LastLoginDate;
DataRow dr = ds.Tables[0].Rows[0];
// ...
if (!Convert.IsDBNull(dr["Sex"]))
client.Sex = (int)dr["Sex"]; <<== ERROR HERE!
// ...
In the database table, Sex has a data type=smallint and nullable=True, and
client.Sex has a data type of int.
If I attempt to examine the contents of dr["Sex"] in the debugger, it shows
a value of 0x0000. If I attempt to examine the contents of (int)dr["Sex"] in
the debugger, it shows an error about not being able to unbox (sorry I no
longer have the exact text).
Any suggestions?
MembershipUser user = Membership.GetUser(userId);
DataSet ds = DataLayer.ExecQueryData("SELECT * FROM mc_Clients WHERE
UserId='" + userId.ToString() + "'");
if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
return null;
ClientEx client = new ClientEx();
client.ClientData = new Client();
client.ClientData.Email = user.UserName;
client.ClientData.UserID = user.ProviderUserKey;
client.ClientData.LastLogin = user.LastLoginDate;
DataRow dr = ds.Tables[0].Rows[0];
// ...
if (!Convert.IsDBNull(dr["Sex"]))
client.Sex = (int)dr["Sex"]; <<== ERROR HERE!
// ...
In the database table, Sex has a data type=smallint and nullable=True, and
client.Sex has a data type of int.
If I attempt to examine the contents of dr["Sex"] in the debugger, it shows
a value of 0x0000. If I attempt to examine the contents of (int)dr["Sex"] in
the debugger, it shows an error about not being able to unbox (sorry I no
longer have the exact text).
Any suggestions?