How to check if a Date field is NULL C# ASP.Net

Asked

Viewed 1,726 times

1

I have a webservice where I make a query in a table of my bank where I have a column of type Datetime and I am wanting to make a check if this field Datetime is filled or if you are NULL

I’m doing it this way

command.Connection = connection;
command.CommandText = "select  DateDesabled from IdUser where UserName = @userName";
command.Parameters.AddWithValue("userName", user.UserName);
DateTime meuValor = Convert.ToDateTime(command.ExecuteScalar());
Debug.WriteLine("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
Debug.WriteLine("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"+meuValor);
DateTime CurrentlyDay = DateTime.Today;
if (meuValor.equals(null))

I get the following error message:

"Object cannot be converted from Dbnull to other types."

  • 2

    Because it is not nullable...? DateTime? meuValor

  • Leandro Angelo Explaining better, in my table there is a field Datalock, where at first this date is not filled. This date is assigned by a support person.. So you wanted to check: select Datalock from User Where Username = @username; with this select would like to check this date if it is NULL the user has no lock otherwise the user is locked. ?

  • If you receive from the bank a Date that can be null... you must use the type DateTime?

  • 1

    @Leandroangelo the return of ExecuteScalar() is not null. IS DBNull.

  • Very well noted @Augustovasques

1 answer

2


I believe the error is generated on this line:

DateTime meuValor = Convert.ToDateTime(command.ExecuteScalar());

According to the question command.ExecuteScalar() can return either a date, in which case all right, or a DBNull, which is when the error occurs because DBNull cannot be converted to date and the method generates the InvalidCastException mentioned in the question.

Another problem with this line is that meuValor is the type DateTime who is descended from ValueType which by definition cannot receive null.

Another critical point is the line:

if (meuValor.equals(null)) ... 

It still does not generate an error but from the moment the previous error is fixed, this line will generate a NullReferenceException.

The solution is simple just work the value obtained with command.ExecuteScalar(), suit the type of meuValor for Nullable, which can receive null, and fix the line which will then generate error by matching the check.

command.Connection = connection;
command.CommandText = "select  DateDesabled from IdUser where UserName = @userName";
command.Parameters.AddWithValue("userName", user.UserName);

// Obtem o valor bruto do DB
var rawMeuValor = command.ExecuteScalar();
// Trabalha rawMeuValor comparando com DBNull. Se afirmativo meuValor referenciará null senão referenciará uma data
DateTime? meuValor = DBNull.Value.Equals(rawMeuValor)? null : Convert.ToDateTime(rawMeuValor);

Debug.WriteLine("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
Debug.WriteLine("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"+meuValor);
DateTime CurrentlyDay = DateTime.Today;

//Não sei a versão do seu compilador então não usei meuValor is null
if (meuValor == null) /* Faça alguma coisa */

Browser other questions tagged

You are not signed in. Login or sign up in order to post.