Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6591803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:28:21+00:00 2026-05-25T17:28:21+00:00

I am having trouble, I am trying to get updates from a messages table

  • 0

I am having trouble, I am trying to get updates from a messages table for my web chat. To do this I store the last datetime in a session and try to get messages sent between last datetime and now.

However, it is missing some messages and I cannot figure out why. Is there something wrong with my mysql datetime comparison?

If I refresh the page it resets the session variable and all messages display. In javascript I call the web method every 2 + callback seconds.

private string LastNow
{
    get
    {
        return Session["last_now"] as string ?? 
                   (string)(Session["last_now"] = string.Empty);
    }

    set
    {
        Session["last_now"] = value;
    }
}

[WebMethod(EnableSession=true)]
public object GetMessages(int id, int roomId)
{
    string lastDt = LastNow;
    /* EDIT */
    string now = MySqlHelper.ExecuteScalar(
        connstr, "SELECT CAST(NOW() AS CHAR);") as string;

    // if the session variable is null or empty, 
    // get all messages up to now, else most recent
    string dtclause = 
        !string.IsNullOrWhiteSpace(lastDt) ?
        string.Format(" AND sent_dt > '{0}' AND sent_dt <= '{1}'", lastDt, now) :
        string.Format(" AND sent_dt <= '{0}'", now);

    string sql =
        string.Format(
        "SELECT user_id, type, message FROM Chat.Message WHERE room_id={0}{1};",
        roomId, dtclause);

    object ret = null;

    try // with a finally, always store last update time
    {
        using (DataSet msgset = 
               MySqlHelper.ExecuteDataset(Common.SupportDatabase, sql))
        {
            DataTable msgtable = msgset.Tables[0];

            if (msgtable != null)
            {
                DataRow[] rows = msgtable.Select();

                if (rows != null && rows.Length > 0)
                {
                    ret = from msg in rows
                            select new
                            {
                                id = msg["user_id"],
                                type = msg["type"],
                                message = msg["message"]
                            };
                }
            }
        }
    }
    finally
    {
        LastNow = now;                
    }


    return ret;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T17:28:22+00:00Added an answer on May 25, 2026 at 5:28 pm

    There shouldn’t be any reason to do the date parsing in your code. MySQL will already output the now() function in yyyy-mm-dd hh:mm:ss format, so just store the raw string that MySQL returns. DateTime parsing will potentially induce things like timezone and daylight savings conversions, which WILL screw up the timstamps. Instead, just suck out the timestamp, and stuff it back in, unconverted.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having trouble trying to get iterators for inner sub-containers. Basically imagine this
I am having real trouble trying to get decent answers to my questions from
I'm having a bit of trouble trying to get class variables to work in
I`m having trouble trying to optimize this query with OVER (PARTITION BY ...) because
I'm trying to get an email sent using ASP classic, and am having trouble
I'm trying to use a session-per-request pattern and I'm having trouble with getting a
I was having some trouble running some Ruby scripts. While trying to get these
I'm having trouble seeing tables from one particular SQL Server 2008 database while trying
I found this snippet of code I am having trouble trying to find a
I'm having trouble trying to optimize the following query for sql server 2005. Does

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.