-1

I'm using :

string date1 = reader. GetDateTime(reader. GetOrdinal("firstMove"). ToString() 

But it's throwing an exception especially if the value is null....

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Greg Adler
  • 99
  • 8
  • 1
    possible duplicate of [SQL Data Reader - handling Null column values](http://stackoverflow.com/questions/1772025/sql-data-reader-handling-null-column-values) – RichardOD Jul 04 '15 at 12:09
  • 1
    You code won't _even_ compile. Can you please show short but complete program demonstrating? What exception you get exactly? – Soner Gönül Jul 04 '15 at 12:57

4 Answers4

0

Try the below code.

string date1=reader["coloumn name"].tostring();
ryanyuyu
  • 6,366
  • 10
  • 48
  • 53
venkatesh
  • 3
  • 6
  • Thanks. Now if I check is null or empty using an if statement and else if, how many times can I repeat the else if? Basically I need to check about 10 strings for null response – Greg Adler Jul 04 '15 at 12:08
0

The fact you're getting an exception would be easier to solve if you told us what kind of exception you get. Possible errors are:

  1. Column is not DateTime, thus GetDateTime doesn't work at all
  2. firstMove is not a valid column name at all, so you can not access that column

Assuming the column really exists and is of type DateTime I tend to use the following:

DateTime? d = reader["firstMove"] as DateTime?;

This works pretty well. What you want would read

DateTime? d = reader["firstMove"] as DateTime?;
string dstring = d.HasValue ? d.Value.ToString() : "";

Or:

string date1 = !reader.IsDBNull(reader.GetOrdinal("firstMove")) ? reader.GetDateTime(reader.GetOrdinal("firstMove").ToString() : String.Empty;
Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
0

It would be better for you, to use DateTime (or a nullable DateTime - DateTime?) instead of string, to handle dates.

If you want to follow this approach, you can use an Extension Method:

public static class IDataReaderExtensions
{
    public static DateTime GetDateTimeOrMinValue(this IDataReader dr, int position)
    {
        if (dr.IsDBNull(position))
        {
            return DateTime.MinValue;
        }

        return dr.GetDateTime(position);
    }

    public static DateTime? GetDateTimeOrNull(this IDataReader dr, int position)
    {
        if (dr.IsDBNull(position))
        {
            return null;
        }

        return dr.GetDateTime(position);
    }
}

Usage:

DateTime dateA = reader.GetDateTimeOrMinValue(reader.GetOrdinal("firstMove"));

DateTime? dateB = reader.GetDateTimeOrNull(reader.GetOrdinal("firstMove"));

You can even improve the extension method, so it could receive the column name and find the ordinal position internally.

Hope this helps,

Pedro Martins
  • 854
  • 6
  • 9
-2

Use as below, by using or function to check whether null or not null.

if(string1 || string2 ||string3)
Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
venkatesh
  • 3
  • 6
  • Thanks for trying. That is a) not an answer and b) not valid C# code. – Thorsten Dittmar Jul 09 '15 at 10:30
  • This is not even valid syntax unless `string1`, `srting2` and `string3` are valid `bool` values. This "answer" is not helpful at all as it reads now. You could have invested the time it took to write your comment into improving your "answer" instead... – Thorsten Dittmar Jul 10 '15 at 11:27