简体   繁体   中英

Date is 01/01/0001 and not the date that is in the database?

I have this line in my sql server 2008 r2 database, but I don't understand:

2012-12-06 11:00:36.703 and is of type DATETIME

When I display it in my view it is:

Mon, Jan 1, 0001 

Here my code:

var allNews = ZincService.NewsService.GetNewsPostsForId(id);

List<Zinc.Web.Areas.News.ViewModels.Home.NewsPostsViewModel> newsItems = new List<NewsPostsViewModel>();

foreach (var newsItem in allNews)
{ 
  NewsPostsViewModel newsItemViewModel = new NewsPostsViewModel();
  newsItemViewModel.CommentDate = String.Format("{0:ddd, MMM d, yyyy}", newsItem.CommentDate);
}

public class NewsPostsViewModel
{
  public Entities.News.News MainNews { get; set; }
  public virtual string News { get; set; }
  public virtual int NewsId { get; set; }
  public virtual string CommentDate { get; set; }
}

  public List<DataModels.News.NewsPostsDataModel> GetNewsPostsForId(int id)
{
  using (SqlConnection conn = new SqlConnection(ZincModelContainer.CONNECTIONSTRING))
  {
    using (SqlCommand cmd = conn.CreateCommand())
    {
      conn.Open();
      cmd.CommandType = System.Data.CommandType.StoredProcedure;
      cmd.CommandText = "[News].[GetNewsPostsForId]";

      SqlParameter param = new SqlParameter("@Id", System.Data.SqlDbType.Int);
      param.Value = id;
      cmd.Parameters.Add(param);

      List<DataModels.News.NewsPostsDataModel> news = new List<DataModels.News.NewsPostsDataModel>();

      using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
      {
        DataTable dt = new DataTable();
        adapter.Fill(dt);

        foreach (DataRow row in dt.Rows)
        {
          string message = Convert.ToString(row["NewsDescription"]);
          news.Add(new DataModels.News.NewsPostsDataModel { NewsPostId = id ,Message = message});
        }
      }

      return news;
    }
  }
}

public class NewsPostsDataModel
{
  public virtual int NewsPostId { get; set; }
  public virtual string Message { get; set; }
  public virtual DateTime CommentDate { get; set; }
}

Can some one help me, please?

This is the problem:

news.Add(new DataModels.News.NewsPostsDataModel { NewsPostId = id ,Message = message});

You're not populating CommentDate here, so it's got the default value of DateTime.MinValue .

Of course we now can't tell whether your stored procedure returns the value at all, but assuming it does, you should presumably be fetching it from the DataRow and putting it in your model...

Before you just fix the code, you should take a step back and think about your diagnostic process. Did you try debugging into this? Adding logging? You should have been able to see very quickly that you didn't have a CommentDate in the model, and then tried to work out why you didn't have a CommentDate , which should have led you back to the code creating the instance of the model. If you can improve your diagnostic processes, you can speed you your future development significantly by not having to ask as many questions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM