0

I have a table Employee having a column JoiningDate of type DateTime.

I am fetching this value in asp.net.

I know to check DBnull we use dr["JoiningDate"] != DBNull.Value .

But if this JoiningDate is empty like '', which fetching it is making it like 1/1/1900.

What is the best way to check DB EMPTY in asp.net for a datetime column.

Details :

JoiningDate is a DateTime field..I made it empty string in stead of NULL..

And while fetching this in asp.net in dr["JoiningDate"] which is a datarow, it gave 1/1/1900.

Code Example

DataSet dsEmployee = null;
dsEmployee = Data.Select("SELECT * FROM Employee WHERE EmployeeID = '" + _id + "'");

DataRow drEmployee = dsEmployee.Tables[0].Rows[0];

if(drEmployee["JoiningDate"] != string.Empty)
{

}
11
  • What is the type of this column? Commented Jul 31, 2013 at 7:22
  • @JonSkeet it is DateTime. Commented Jul 31, 2013 at 7:22
  • 2
    You need a bit more detail in your question. What SQL server? What data types are the SQL column and the C# variable? Some additional sample code (for context) would be nice, too. Commented Jul 31, 2013 at 7:23
  • 1
    @Tim DateTime Min value is 1/1/0001 Commented Jul 31, 2013 at 7:32
  • 1
    How is it automatically fetching it as 1/1/1900, Can you show the select i guess somwething there is doing the transform or is it the default value for that column ? Commented Jul 31, 2013 at 7:36

5 Answers 5

0

Try this-

 if(!String.IsNullOrEmpty(Convert.ToString(dr["JoiningDate"])))
 {

 }
Sign up to request clarification or add additional context in comments.

Comments

0

you can do something like this to check...

 if(  dr["JoiningDate"] != DBNull.Value && (DateTime)dr["JoiningDate"] != DateTime.Parse("1/1/1900")) {
//do...
}

Comments

0
if(string.IsNullOrEmpty(........))
{

......

}

Comments

0

You have to edit your check like this

if(dr["JoiningDate"] != DBNull.Value && !string.IsNullOrEmpty(Convert.ToString(dr["JoiningDate"])))
{
     //Your Code
}

Comments

0

If you say dr["JoiningDate"] is not returning null then

  • you are changing the value in the SELECT query
  • The default value of the column is set to 1/1/1900

Also please check where you insert the records in that case you would be passing null for that particular column so that it assumes the default value which is being returned

So you should rely on checking the condition by making a DateTime instance like new DateTime(1900,1,1) and then compare

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.