Asp.net Forum

Ask Question   UnAnswered
Home » Forum » Asp.net       RSS Feeds

Operator '!=' cannot be applied to operands of type 'System.DateTim

  Asked By: Willie    Date: Oct 19    Category: Asp.net    Views: 9484

how can I test if a DateTime object was initialized?

when I try to test with the '!=' and '==' i get this error:

acompanhamento.ascx.cs(93): Operator '!=' cannot be applied to operands of type
'System.DateTime' and '<null>'



8 Answers Found

Answer #1    Answered By: Oscar Evans     Answered On: Oct 19

can u give us some more code, which is the situation, C#?...

Answer #2    Answered By: Michael Evans     Answered On: Oct 19

it´s c#
i have to compare the value of a textbox with the day in a datetime field.

this.DIA_RETIRADA.Text == this.ObjReserva.AgRetirada.DtRetirada.ToString("dd")

but in some cases, i need to know if the "DtRetirada" exists otherwise i get an

reference set to a non object  , in that line.

so, i tryed this:

if( this.ObjReserva.AgDevolucao.DtDevolucao != null &&
this.ObjReserva.AgDevolucao.DtDevolucao.ToString("dd/MM/yyyy HH:mm") != ""){

this.ObjReserva.AgDevolucao.DtDevolucao.ToString("dd/MM/yyyy HH:mm");

this.DATA_HORA_DEVOLUCAO.Visible = true;


Answer #3    Answered By: Woodrow Jones     Answered On: Oct 19

you're are dealing with 2 different types, so u have 2 possibilities, cast the
string to a date, or cast the date to a string,
I would try the other one: the string to a date, cos you could have some
different possibilities...

(DateTime) this.DIA_RETIRADAT.Text =

=> possible you have to write smth as system.data.sqlclient.datetime

Answer #4    Answered By: Iris Sanders     Answered On: Oct 19

The type  of comparison he is after, it is better I think if both are date.

Answer #5    Answered By: Olga Allen     Answered On: Oct 19

I read in the "inside c#" book that exists diferences between the types.
the help also said that a struct(DateTime is a Struct) is a value type  so, it
can´t be null.

a reference can be null so i know when it exists or not

My problem is know when this DateTime has been initializaed or not.

In this case I can´t cast my textbox to DateTime format because i have one
textbox for each part of the date (day,month,year,hour,minute).

Answer #6    Answered By: Charlie Evans     Answered On: Oct 19

The following is a piece of code which does date
comparison after taking a string date and converting
it to a date object. Hope it helps :-

DateTime dJoinedAfter =
iDay = dJoinedAfter.Day;
iMonth = dJoinedAfter.Month;
iYear = dJoinedAfter.Year;
sAmericanDate = "#"+ iMonth.ToString() +"/"+
iDay.ToString() +"/"+ iYear.ToString() +"#";
sSqlString = "SELECT * FROM PersonMaster WHERE
DateOfJoiningCompany > "+ sAmericanDate +" ";

Answer #7    Answered By: Jawna Mohammad     Answered On: Oct 19

check before u look after the (DateTime) this.DIA_RETIRADAT.Text =

if it exists...

if (this.obj....(dd) != dbNull)

... //exists


... //don't exists

sorry for my bad C#, have to learn it once after my thesis, where i use VB.NET,
is done...

Answer #8    Answered By: Venkat Rulez     Answered On: Oct 19

If typeof(YourDate) is Nothing, perhaps?