Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Object cannot be cast from DBNull to other type
Message
De
24/05/2013 16:37:47
 
 
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01574643
Message ID:
01574655
Vues:
66
This message has been marked as the solution to the initial question of the thread.
>>>Hi everybody,
>>>
>>>I am getting this error
>>>
>>>"Object cannot be cast from DBNull to other types"
>>>
>>>on this piece of code:
>>>
>>>start_time = Convert.ToDateTime((row["start_time"] ?? DTSqlMinDate));
>>>
>>>As you see here, I attempt to use C# coalesce operator in order to avoid this error, but it doesn't work. How should I correct this error without introducing extra nullable datetime variable?
>>>
>>>Thanks in advance.
>>>
>>>UPDATE. This seems to fix the problem
>>>
>>>Convert.ToDateTime((row["start_time"]== DBNull.Value)?DTSqlMinDate:row["start_time"]);
>>
>>I think you were confusing DBNull with C# null which is what the ?? operator uses.......
>
>I thought the above would prevent the problem. Is there any other way to fix it besides my solution?

Depending on the actual type stored in the column, you could try
start_time = row.Field<DateTime?>("start_time") ?? DTSqlMinDate;
It won't work if you have a string you need converted to a DateTime though.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform