tshad said:
tshad said:
I am sending data to my Sql Server and in some cases get null back.
In this case, I am sending back an int, but if it is null, I get a
"Specified cast not valid".
So I did the following:
if (parameters[7].Value == null)
return -5;
else
return (int)parameters[7].Value;
I also tried:
return (parameters[7].Value ?? 0);
and
return ((int)parameters[7].Value ? (int)parameters[7].Value : 0);
The error here was:
User.cs(198,13): error CS0029: Cannot implicitly convert type 'int' to
'bool'
Tom
I just ended up always testing for null - which is probably best.
************************************************************************************
public class User
{
private string clientID = "";
private int userID = 0;
private long companyID= 0;
private string firstName = "";
private string lastName = "";
private string userName = "";
private string secretQuestion = "";
private string secretAnswer = "";
private string email = "";
private string password = "";
private bool firstTime = true;
private bool emailHtml = true;
private string dateCreated;
private string connectionString = "";
private int status = 0;
...
private void LoadDetails (DataRow user)
{
if (user["FirstName"] != DBNull.Value)
firstName = (string)user["FirstName"];
if (user["LastName"] != DBNull.Value)
lastName = (string)user["LastName"];
if (user["UserName"] != DBNull.Value)
userName = (string)user["UserName"];
if (user["SecretQuestion"] != DBNull.Value)
secretQuestion = (string)user["SecretQuestion"];
if (user["SecretAnswer"] != DBNull.Value)
secretAnswer = (string)user["SecretAnswer"];
if (user["Email"] != DBNull.Value)
email = (string)user["Email"];
if (user["Password"] != DBNull.Value)
password = (string)user["Password"];
if (user["DateCreated"] != DBNull.Value)
dateCreated = user["DateCreated"].ToString();
if (user["UserID"] != DBNull.Value)
userID = (int)user["UserID"];
if (user["FirstTime"] != DBNull.Value)
firstTime = (bool)user["FirstTime"];
if (user["emailHtml"] != DBNull.Value)
emailHtml = (bool)user["emailHtml"];
if (user["CompanyID"] != DBNull.Value)
companyID = (int)user["CompanyID"];
if (user["ClientID"] != DBNull.Value)
clientID = (string)user["ClientID"];
}
******************************************************************
Seems like the best way to handle it to make sure there are no errors.
Thanks,
Tom