Note that there are some explanatory texts on larger screens.

plurals
  1. PONullable DateTime conversion
    primarykey
    data
    text
    <blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/330471/c-sharp-why-cant-a-nullable-int-be-assigned-null-as-a-value">c# why cant a nullable int be assigned null as a value</a> </p> </blockquote> <p>Im trying to convert my reader[3] object which is datetime to be null if there is no lastPostDate for a Forum but it says Im missing a conversion. Error:</p> <blockquote> <p>Type of conditional expression cannot be determined because there is no implicit conversion between <code>&lt;null&gt;</code> and 'System.DateTime' </p> </blockquote> <pre><code>public class Forums { public List&lt;Forum&gt; GetForums() { using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["CMS"].ConnectionString)) { conn.Open(); SqlCommand cmd = new SqlCommand("sproc_Forums_GetForums", conn); cmd.CommandType = CommandType.StoredProcedure; SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.Default); List&lt;Forum&gt; forums = new List&lt;Forum&gt;(); while (reader.Read()) { var title = reader[6].ToString(); var threadCount = (int)reader[5]; var lastPostTitle = reader[4].ToString(); // below is where im having a problem Nullable&lt;DateTime&gt; lastPostDate = (reader[3] == DBNull.Value ? null : Convert.ToDateTime(reader[3])); var lastPostBy = reader[2].ToString(); var forumGroup = reader[1].ToString(); var description = reader[0].ToString(); Forum forum = new Forum(0, "",DateTime.Now, reader["Title"].ToString(),description, 0,false,"","",DateTime.Now,true, forumGroup, (int)threadCount, lastPostBy, lastPostDate, lastPostTitle); forums.Add(forum);/**/ } return forums; } } } </code></pre> <p>Below is my class object for Forum with a Nullable lastPostDate</p> <pre><code> public class Forum { public Forum(int forumID, string addedBy, DateTime addedDate, string title, string description, int parentID, bool moderated, string imageUrl, string updatedBy, DateTime? updatedDate, bool active, string forumGroup, int threadCount, string lastPostBy, Nullable&lt;DateTime&gt; lastPostDate, string lastPostTitle) { this.ForumID = forumID; this.AddedBy = addedBy; this.AddedDate = addedDate; this.Title = title; this.Description = description; this.ParentID = parentID; this.Moderated = moderated; this.ImageUrl = imageUrl; this.UpdatedBy = updatedBy; this.UpdatedDate = updatedDate; this.Active = active; this.ForumGroup = forumGroup; this.ThreadCount = threadCount; this.LastPostBy = lastPostBy; this.LastPostDate = lastPostDate; this.LastPostTitle = lastPostTitle; } private int _forumID; public int ForumID { get { return _forumID; } set { _forumID = value; } } private string _addedBy; public string AddedBy { get { return _addedBy; } set { _addedBy = value; } } private DateTime _addedDate = DateTime.Now; public DateTime AddedDate { get { return _addedDate; } set { _addedDate = value; } } private string _title = ""; public string Title { get { return _title; } set { _title = value; } } private string _description = ""; public string Description { get { return _description; } set { _description = value; } } private int _parentID = 0; public int ParentID { get { return _parentID; } set { _parentID = value; } } private bool _moderated = false; public bool Moderated { get { return _moderated; } set { _moderated = value; } } private string _imageUrl = ""; public string ImageUrl { get { return _imageUrl; } set { _imageUrl = value; } } private string _updatedBy = ""; public string UpdatedBy { get { return _updatedBy; } set { _updatedBy = value; } } private DateTime? _updatedDate = null; public DateTime? UpdatedDate { get { return _updatedDate; } set { _updatedDate = value; } } private bool _active = false; public bool Active { get { return _active; } set { _active = value; } } private string _forumGroup = ""; public string ForumGroup { get { return _forumGroup; } set { _forumGroup = value; } } private int _threadCount = 0; public int ThreadCount { get { return _threadCount; } set { _threadCount = value; } } private string _lastPostBy = ""; public string LastPostBy { get { return _lastPostBy; } set { _lastPostBy = value; } } private Nullable&lt;DateTime&gt; _lastPosteDate = null; public Nullable&lt;DateTime&gt; LastPostDate { get { return _lastPosteDate; } set { _lastPosteDate = value; } } private string _lastPostTitle = ""; public string LastPostTitle { get { return _lastPostTitle; } set { _lastPostTitle = value; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload