Handling Nulls from SQL Server

I am writing a C#, ASP.NET app, but my issue is mainly C# code related...my program is working as long as I don't have any nulls in my table. When I call select * from myTable and there are null fields, I get "The Input string was not in correct format". I have some fields that are string, some that are int and 1 that is DateTime. As far as I can tell, if the string fields are coming back null, my code is handling it fine, but if my int fields come back null it gives the error....if nothing is null, my code displays the data. Here's the section of code causing the problems...

Movie.MovieName = dr["MovieName"] == null ? "" : dr["MovieName"].ToString();
Movie.NumDVDs = dr["NumDVDs"] == null ? 0 : int.Parse(dr["NumDVDs"].ToString());
Movie.CaseNum = dr["CaseNum"] == null ? 0 : int.Parse(dr["CaseNum"].ToString());
Movie.DateAdded = dr["DateAdded"] == null ? DateTime.Now : Convert.ToDateTime(dr["DateAdded"].ToString());
Movie.LoanedTo = dr["LoanedTo"] == null ? "" : dr["LoanedTo"].ToString();

any help appreciated

Comments

  • : I am writing a C#, ASP.NET app, but my issue is mainly C# code related...my program is working as long as I don't have any nulls in my table. When I call select * from myTable and there are null fields, I get "The Input string was not in correct format". I have some fields that are string, some that are int and 1 that is DateTime. As far as I can tell, if the string fields are coming back null, my code is handling it fine, but if my int fields come back null it gives the error....if nothing is null, my code displays the data. Here's the section of code causing the problems...
    :
    : Movie.MovieName = dr["MovieName"] == null ? "" : dr["MovieName"].ToString();
    : Movie.NumDVDs = dr["NumDVDs"] == null ? 0 : int.Parse(dr["NumDVDs"].ToString());
    : Movie.CaseNum = dr["CaseNum"] == null ? 0 : int.Parse(dr["CaseNum"].ToString());
    : Movie.DateAdded = dr["DateAdded"] == null ? DateTime.Now : Convert.ToDateTime(dr["DateAdded"].ToString());
    : Movie.LoanedTo = dr["LoanedTo"] == null ? "" : dr["LoanedTo"].ToString();
    :
    : any help appreciated
    :

    I don't see any errors, so try to output some debugging info, like this:

    print(dr["NumDVDs"]==null)
    print(dr["NumDVDs"].ToString())

  • : : I am writing a C#, ASP.NET app, but my issue is mainly C# code related...my program is working as long as I don't have any nulls in my table. When I call select * from myTable and there are null fields, I get "The Input string was not in correct format". I have some fields that are string, some that are int and 1 that is DateTime. As far as I can tell, if the string fields are coming back null, my code is handling it fine, but if my int fields come back null it gives the error....if nothing is null, my code displays the data. Here's the section of code causing the problems...
    : :
    : : Movie.MovieName = dr["MovieName"] == null ? "" : dr["MovieName"].ToString();
    : : Movie.NumDVDs = dr["NumDVDs"] == null ? 0 : int.Parse(dr["NumDVDs"].ToString());
    : : Movie.CaseNum = dr["CaseNum"] == null ? 0 : int.Parse(dr["CaseNum"].ToString());
    : : Movie.DateAdded = dr["DateAdded"] == null ? DateTime.Now : Convert.ToDateTime(dr["DateAdded"].ToString());
    : : Movie.LoanedTo = dr["LoanedTo"] == null ? "" : dr["LoanedTo"].ToString();
    : :
    : : any help appreciated
    : :
    :
    : I don't see any errors, so try to output some debugging info, like this:
    :
    : print(dr["NumDVDs"]==null)
    : print(dr["NumDVDs"].ToString())
    :
    :
    I think it's crapping out trying to parse dr["NumDVDs"].ToString(), which means my check for null is failing.....does Sql Server return a different form of null than I'm checking for? If I'm right, how can I check for that?
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories