Cannot implicitly convert type 'System.DateTime?' to 'System.DateTime'
-
DateTime dateRented = rental_InvoiceTableAdapter1.getTimeRentedbyInvoiceNo(int.Parse(txtInvoiceNo.Text));
I can't figure out why I'm getting this exception thrown by this code. I'm trying to collect a single cell from the DateTime column "Time_Rented" using a SQL statement. The SQL statement works fine in the query builder, so it has to be something in the C# code. The entire error message is: "Cannot implicitly convert type 'System.DateTime?' to 'System.DateTime'. An explicit conversion exists ( are you missing a case?)" Both sides of the statement are DateTime, aren't they? I don't understand why it can't convert it. Please help!
-
DateTime dateRented = rental_InvoiceTableAdapter1.getTimeRentedbyInvoiceNo(int.Parse(txtInvoiceNo.Text));
I can't figure out why I'm getting this exception thrown by this code. I'm trying to collect a single cell from the DateTime column "Time_Rented" using a SQL statement. The SQL statement works fine in the query builder, so it has to be something in the C# code. The entire error message is: "Cannot implicitly convert type 'System.DateTime?' to 'System.DateTime'. An explicit conversion exists ( are you missing a case?)" Both sides of the statement are DateTime, aren't they? I don't understand why it can't convert it. Please help!
Lodeclaw wrote:
System.DateTime?
What is
System.DateTime**?**
? I have only heard ofSystem.DateTime
. Try removing the "?".Kristian Sixhoej "You can't undo the past... but you can certainly not repeat it." - Bruce Willis
HAI
CAN HAS STDIO?
PLZ OPEN FILE "SIGNATURE.TXT"?
AWSUM THX
VISIBLE FILE
O NOES
INVISIBLE "ERROR!"
KTHXBYE -
Lodeclaw wrote:
System.DateTime?
What is
System.DateTime**?**
? I have only heard ofSystem.DateTime
. Try removing the "?".Kristian Sixhoej "You can't undo the past... but you can certainly not repeat it." - Bruce Willis
HAI
CAN HAS STDIO?
PLZ OPEN FILE "SIGNATURE.TXT"?
AWSUM THX
VISIBLE FILE
O NOES
INVISIBLE "ERROR!"
KTHXBYE -
DateTime dateRented = rental_InvoiceTableAdapter1.getTimeRentedbyInvoiceNo(int.Parse(txtInvoiceNo.Text));
I can't figure out why I'm getting this exception thrown by this code. I'm trying to collect a single cell from the DateTime column "Time_Rented" using a SQL statement. The SQL statement works fine in the query builder, so it has to be something in the C# code. The entire error message is: "Cannot implicitly convert type 'System.DateTime?' to 'System.DateTime'. An explicit conversion exists ( are you missing a case?)" Both sides of the statement are DateTime, aren't they? I don't understand why it can't convert it. Please help!
-
Look closely at the message. DateTime? is a nullable DateTime. You'll have to do the explicit conversion [EDIT] or alternatively declare you date rented as the nullable DateTime? type. Alan.
-
DateTime dateRented = rental_InvoiceTableAdapter1.getTimeRentedbyInvoiceNo(int.Parse(txtInvoiceNo.Text));
I can't figure out why I'm getting this exception thrown by this code. I'm trying to collect a single cell from the DateTime column "Time_Rented" using a SQL statement. The SQL statement works fine in the query builder, so it has to be something in the C# code. The entire error message is: "Cannot implicitly convert type 'System.DateTime?' to 'System.DateTime'. An explicit conversion exists ( are you missing a case?)" Both sides of the statement are DateTime, aren't they? I don't understand why it can't convert it. Please help!
DateTime?
is short-hand forNullable<DateTime>
, using theNullable<T>
class introduced in .NET Framework 2.0. TheNullable
types are actually a class that is a wrapper around a value type. To get the underlying value from aNullable<T>
, use theValue
property like so:DateTime dateRented = rental_InvoiceTableAdapter1.getTimeRentedbyInvoiceNo(int.Parse(txtInvoiceNo.Text)).Value;
Or you can declare
dateRented
asNullable<DateTime>
(same asDateTime?
) rather than a normalDateTime
, like so:DateTime? dateRented = rental_InvoiceTableAdapter1.getTimeRentedbyInvoiceNo(int.Parse(txtInvoiceNo.Text));
OR
Nullable<DateTime> dateRented = rental_InvoiceTableAdapter1.getTimeRentedbyInvoiceNo(int.Parse(txtInvoiceNo.Text));
The message really means that the
getTimeRentedbyInvoiceNo()
function is returning aDateTime?
and you are trying to stuff that return value into aDateTime
; and they are two different types.Hope in one hand and poop in the other; see which fills up first. Hope and change were good slogans, now show us more than words.
-
DateTime?
is short-hand forNullable<DateTime>
, using theNullable<T>
class introduced in .NET Framework 2.0. TheNullable
types are actually a class that is a wrapper around a value type. To get the underlying value from aNullable<T>
, use theValue
property like so:DateTime dateRented = rental_InvoiceTableAdapter1.getTimeRentedbyInvoiceNo(int.Parse(txtInvoiceNo.Text)).Value;
Or you can declare
dateRented
asNullable<DateTime>
(same asDateTime?
) rather than a normalDateTime
, like so:DateTime? dateRented = rental_InvoiceTableAdapter1.getTimeRentedbyInvoiceNo(int.Parse(txtInvoiceNo.Text));
OR
Nullable<DateTime> dateRented = rental_InvoiceTableAdapter1.getTimeRentedbyInvoiceNo(int.Parse(txtInvoiceNo.Text));
The message really means that the
getTimeRentedbyInvoiceNo()
function is returning aDateTime?
and you are trying to stuff that return value into aDateTime
; and they are two different types.Hope in one hand and poop in the other; see which fills up first. Hope and change were good slogans, now show us more than words.
-
I tried those and even though the debugger is ok with them, and the other conversions I've tried, I get an exception telling me the input value is not in the correct format.
-
Lodeclaw wrote:
I get an exception telling me the input value is not in the correct format.
This may be a different problem now. Where did the exception arise and do you know that the result of int.Parse is valid input for your function? Alan.
For some reason the query that fills the txtInvoiceNo textbox is not yielding a result, so my input is an empty string. I'll have to figure out why that is. In any case, this problem is solved, then. Thanks everyone for helping this poor noob. :doh: I learned some new things.
-
Lodeclaw wrote:
System.DateTime?
What is
System.DateTime**?**
? I have only heard ofSystem.DateTime
. Try removing the "?".Kristian Sixhoej "You can't undo the past... but you can certainly not repeat it." - Bruce Willis
HAI
CAN HAS STDIO?
PLZ OPEN FILE "SIGNATURE.TXT"?
AWSUM THX
VISIBLE FILE
O NOES
INVISIBLE "ERROR!"
KTHXBYEIt's a nullable datetime. int? is a nullable int, for example.
Christian Graus Driven to the arms of OSX by Vista.