Return nullable datetime from scalar, stored procedure

Posted by molgan on Stack Overflow See other posts from Stack Overflow or by molgan
Published on 2010-04-06T06:54:30Z Indexed on 2010/04/06 7:03 UTC
Read the original article Hit count: 234

Filed under:
|

Hello

I have a function that returns a date from a stored procedure, and it all works great til the value is NULL, how can I fix this so it works with null aswell?

    public DateTime? GetSomteDate(int SomeID)
    {

        DateTime? LimitDate= null;

        if (_entities.Connection.State == System.Data.ConnectionState.Closed)
            _entities.Connection.Open();

        using (EntityCommand c = new EntityCommand("MyEntities.GetSomeDate", (EntityConnection)this._entities.Connection))
        {
            c.CommandType = System.Data.CommandType.StoredProcedure;


            EntityParameter paramSomeID = new EntityParameter("SomeID", System.Data.DbType.Int32);
            paramSomeID.Direction = System.Data.ParameterDirection.Input;
            paramSomeID.Value = SomeID;
            c.Parameters.Add(paramSomeID);

            var x = c.ExecuteScalar();

            if (x != null)
                LimitDate = (DateTime)x;

            return LimitDate.Value;

        };
    }

© Stack Overflow or respective owner

Related posts about entity-framework

Related posts about c#