Invalid Cast DateTime?
Invalid Cast DateTime?
I have a class with the following code
public cCase(string pCaseNo, string pMode) { if (pMode == "new") { this._caseNo = Validate_CaseNo(pCaseNo); } if (pMode == "existing") { try { int intValidatedCaseNo = Validate_CaseNo(pCaseNo); string sqlText = "SELECT * FROM tblCases WHERE CaseNo = @CaseNo;"; string strConnection = cConnectionString.BuildConnectionString(); SqlConnection linkToDB = new SqlConnection(strConnection); linkToDB.Open(); SqlCommand sqlCom = new SqlCommand(sqlText, linkToDB); sqlCom.Parameters.Add("@CaseNo", SqlDbType.Int); sqlCom.Parameters["@CaseNo"].Value = intValidatedCaseNo; SqlDataReader caseReader = sqlCom.ExecuteReader(); if (caseReader.HasRows) while (caseReader.Read()) { this._claimant = caseReader["Claimant"].ToString(); this._defendant = caseReader["Defendant"].ToString(); this._caseType = caseReader["CaseType"].ToString(); this._occupation = caseReader["Occupation"].ToString(); this._doa = (DateTime?)caseReader["DOA"]; this._dateClosed = (DateTime?)caseReader["DateClosed"]; this._dateSettled = (DateTime?)caseReader["DateSettled"]; this._dateInstructed = (DateTime?)caseReader["DateInstructed"]; this._status = caseReader["Status"].ToString(); this._instructionType = caseReader["InstructionType"].ToString(); this._feeEstimate = (decimal?)caseReader["FeeEstimate"]; this._amountClaimed = (decimal?)caseReader["AmountClaimed"]; this._amountSettled = (decimal?)caseReader["AmountSettled"]; this._caseManager = caseReader["CaseManager"].ToString(); } caseReader.Close(); linkToDB.Close(); linkToDB.Dispose(); } catch (Exception eX) { throw new Exception("Error finding case" + Environment.NewLine + eX.Message); } } }
However the Datetime? casts fail with an 'Invalid Cast'. I've checked the SQL database and the field is storing valid dates So I cant work out why, as I extract info via the DataReader into my app, the datetime fields are causing an Invalid Cast.
Please help.
Answer by Khan for Invalid Cast DateTime?
You're going to want to change the line that reads:
this._doa = (DateTime?)caseReader["DOA"];
to:
if (caseReader["DOA"] != DBNull.Value) this._doa.Value = (DateTime)caseReader["DOA"];
As well as all similar lines.
DBNull values cannot be casted from Nullable types.
Answer by Sai Kalyan Kumar Akshinthala for Invalid Cast DateTime?
Try with the following code part
this._doa = (caseReader["DOA"] == DBNull.Value ? DBNull.Value : Convert.ToDateTime(caseReader["DOA"]);
Answer by Alex for Invalid Cast DateTime?
Your DateTime
fields probably hold a DBNull
value which you cannot convert directly.
However, I'd use an extension method on your DataReader
for convinience.
public static class DataReaderExtensions { public static DateTime? ReadNullableDateTime(this IDataReader reader, string column) { return reader.IsDBNull(column) ? (DateTime?)null : reader.GetDateTime(column); } }
// Usage
this._dateInstructed = CaseReader.ReadNullableDateTime("DateInstructed");
Answer by husnain for Invalid Cast DateTime?
Try to Convert Date Time as
this._doa = Convert.ToDateTime(caseReader["DOA"]);
Answer by Dummy01 for Invalid Cast DateTime?
I often deal with DBNull.Value
...
So I use this method that will return the value of the object or the default value of the given value type if object's value is DBNull.Value
.
public static object GetValueOrDefault(object value, Type type) { if (value != DBNull.Value) return value; if (type.IsValueType == false) return null; Array array = Array.CreateInstance(type, 1); return array.GetValue(0); }
Usage:
GetValueOrDefault(dataRecord.GetValue(fieldIndex), dataRecord.GetFieldType(fieldIndex)
Answer by saramgsilva for Invalid Cast DateTime?
You should use
this not throw exception, like
var mydate =(DateTime)datetimeString
or var mydate =DateTime.Parse(datetimeString)
does!!!
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72
0 comments:
Post a Comment