-1

I stay with that error when I'm trying to format a date in my code:

Cmd.CommandText = @"
    DECLARE @command varchar(5000);
    DECLARE @RestoreList TABLE(DB_name VARCHAR(100), RS_name VARCHAR(100), RS_DateFinExercice DATE, RS_IsClosed VARCHAR(50));
    SELECT @command = 'IF ''?'' IN (SELECT name FROM sys.databases WHERE HAS_DBACCESS(name) = 1 AND CASE WHEN state_desc = ''ONLINE'' THEN OBJECT_ID( QUOTENAME( name ) + ''.[dbo].[P_DOSSIER]'',''U'' ) END IS NOT NULL) BEGIN USE [?] SELECT DB_name = CAST(DB_NAME() AS VARCHAR(100)), RS_name = CAST(a.D_RaisonSoc AS VARCHAR(100)), RS_DateFinExercice = CAST((SELECT Max(v) FROM (VALUES (a.[D_FinExo01]), (a.[D_FinExo02]), (a.[D_FinExo03]),(a.[D_FinExo04]),(a.[D_FinExo05])) AS value(v)) AS DATE), RS_IsClosed = CAST((SELECT CASE WHEN (SUM (CASE WHEN JM_Cloture !=2 THEN 1 ELSE 0 END)>0) THEN '''' ELSE ''arc'' END FROM F_JMOUV) AS VARCHAR(50)) FROM [dbo].[P_DOSSIER] a INNER JOIN F_JMOUV b ON DB_name() = DB_NAME() GROUP BY D_RaisonSoc, D_FinExo01, D_FinExo02, D_FinExo03, D_FinExo04, D_FinExo05 HAVING COUNT(*) > 1 END'
    INSERT INTO @RestoreList EXEC sp_MSforeachdb @command;
    SELECT * FROM @RestoreList ORDER BY DB_name;";

SqlDataReader dr = Cmd.ExecuteReader();

List<DBtoRestore> dgUIDcollection = new List<DBtoRestore>();

if (dr.HasRows)
{
    while (dr.Read())
    {
        DBtoRestore currentdgUID = new DBtoRestore
            {
                CUID_dbname = dr["DB_name"].ToString(),
                CUID_RaisonSoc = dr["RS_name"].ToString(),
                CUID_DateFinExercice = dr["RS_DateFinExercice"].ToString(),
                CUID_IsClosed = dr["RS_IsClosed"].ToString()
            };

        dgUIDcollection.Add(currentdgUID);
    }
}

dgDBtoRestore.ItemsSource = dgUIDcollection;
Cnx.Close();

The problem is on this line of code:

CUID_DateFinExercice = dr["RS_DateFinExercice"].ToString()

For now, my datagrid report date like 01/01/2020 00:00:00. In SQL, I have 01-01-2020 style.

I want to have the same style in my datagrid.

I have try something like ToString("dd-MM-yyyy") but it's in that context I've received the error.

Any idea to help me?

marc_s
  • 704,970
  • 168
  • 1,303
  • 1,425
  • Which dbms are you using? (Perhaps MS SQL Server???) – jarlh Sep 07 '21 at 18:50
  • Yes ms.sql server. – Fabrice Bertrand Sep 07 '21 at 18:53
  • @FabriceBertrand you should mention the type of that field in this table. – snr Sep 07 '21 at 18:54
  • Victory, i have find a solution : CUID_DateFinExercice = ((DateTime)dr["RS_DateFinExercice"]).ToString("dd-MM-yyyy"), – Fabrice Bertrand Sep 07 '21 at 18:54
  • 3
    Use [SqlDataReader.GetDateTime](https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqldatareader.getdatetime?view=dotnet-plat-ext-5.0#System_Data_SqlClient_SqlDataReader_GetDateTime_System_Int32_) method. – Alexander Petrov Sep 07 '21 at 19:05
  • [How to format DateTime columns in DataGridView](https://stackoverflow.com/questions/4033113/how-to-format-datetime-columns-in-datagridview) – John Wu Sep 07 '21 at 19:20
  • The problem with `GetDateTime` is that it requires an ordinal parameter which makes it fragile. Converting `dr["RS_DateFinExercice"]` is better in that sense. – mm8 Sep 07 '21 at 19:20

2 Answers2

0

Convert to a DateTime and then call ToString on it:

Convert.ToDateTime(dr["RS_DateFinExercice"]).ToString("dd-MM-yyyy")
mm8
  • 150,971
  • 10
  • 50
  • 74
  • what is the difference btw ,except being syntatic sugar , Convert.ToDateTime and DateTime cast? – snr Sep 09 '21 at 05:42
-2

Solution :

CUID_DateFinExercice = ((DateTime)dr["RS_DateFinExercice"]).ToString("dd-MM-yyyy"),
Chris
  • 112,704
  • 77
  • 249
  • 231