0

I am creating a desktop application in which I have to query data in SQL Server by providing date and time. Interval is in seconds and every second I run the query again, with the next date.

In this scenario I am getting data from SQL Server and saving date column value on a label, after processing that data, I am running that query again with the date stored on that label and again..

But there's a problem that my SQL Server datetime column is showing as

2016-12-01 18:36:32.000

and when I access that column in my Winform label, it shows as

12/1/2016 6:36 PM

As I have mentioned that I have to consider seconds also, like in SQL Server my query is working fine because I can change minutes easily but when I try to load that same field on label, it is not showing as it is showing in SQL Server.

All I want is to get that column value the same as it is showing in SQL Server.

This is the code I am using to display datetime value on label.

I have selected the column from a simple SQL query like

SELECT [column name]
FROM [table name]

if (dataset.Tables[0].Rows.Count>0)
{
    lblDate.Text = dataset.Tables[0].Rows[0]["CHECKTIME"].ToString();
    dgv.DataSource = dataset.Tables[0].DefaultView;
}

Screenshots of error

17
  • 3
    Is this really all the C# DateTime is storing? or is it all you're displaying? Commented Dec 12, 2016 at 9:32
  • i am just displaying data from database Commented Dec 12, 2016 at 9:34
  • i just want retrieved datetime same as it displays in sql server Commented Dec 12, 2016 at 9:34
  • 1
    But I suspect you're displaying it using the default formatting, which in your culture probably doesn't show seconds. This is a "how do I specify a custom format" question really, but we don't know how you're displaying the value at the moment, as you haven't shown any code. Commented Dec 12, 2016 at 9:34
  • 1
    Well what if you were to display myDate.ToString("yyyy-MM-dd hh:mm:ss.fff"); - following your edit, you would specifically use lblDate.Text = dataset.Tables[0].Rows[0]["CHECKTIME"].ToString("yyyy-MM-dd hh:mm:ss.fff"); Commented Dec 12, 2016 at 9:36

1 Answer 1

1

You must strictly separate

  • the value of a date-time

and

  • the textual representation

What you see is not the actual value. Especially with date-time values this might get quite tricky: How this is translated into readable text is depending on many factors: System's culture, language, settings...

When you read a date-time from SQL-Server column (DATETIME) into C# variable (DateTime) there exists no readable text. But when you set the lable's Text-property, you probably used .ToString(). At this moment the default settings are used.

However you set the value to the lable's text, you must pass in culture/format info to get this in the way you need it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.