Change Datatable Column Date Format

angel_neo

I'm working with a txt files, I must insert that data in mysql database. So I get that info in a Datatable, then I can get that data in my gridview and I must insert that data in database. I can do that, but I have problems with date columns.

For example, When I try to insert, If I have this date in datagridview: 12/01/2018 no problem, but if I have 17/01/2018 not insert, I have manually changed and I've probed it.

this is read txt:

string LogAutoriz_fileName = Server.MapPath("~/Files/") + User.Identity.Name.ToString() + "_" + Path.GetFileName(fup_LogAutoriz_File.PostedFile.FileName);
    fup_LogAutoriz_File.SaveAs(LogAutoriz_fileName);

    DataTable dt = new DataTable();

    List<string[]> list = new List<string[]>();
    int maxItem = 0;
    using (System.IO.TextReader tr = File.OpenText(LogAutoriz_fileName))
    {
        string line;
        while ((line = tr.ReadLine()) != null)
        {
            string[] items = line.Trim().Split('    ');
            if (maxItem <= items.Count())
            {
                maxItem = items.Count();
            }
            list.Add(items);
        }


        //Crear las columnas del DataTable de Datos
        dt.Columns.Add("Comprobante", typeof(string));
        dt.Columns.Add("Serie_Comprobante", typeof(string));
        dt.Columns.Add("Ruc_Emisor", typeof(string));
        dt.Columns.Add("RazonSoc_Emisor", typeof(string));
        dt.Columns.Add("Fecha_Emision", typeof(string));
        dt.Columns.Add("Fecha_Autoriz", typeof(string));
        dt.Columns.Add("Tipo_Emision", typeof(string));
        dt.Columns.Add("Ident_Receptor", typeof(string));
        dt.Columns.Add("Basura", typeof(string));
        dt.Columns.Add("Clave_Acceso", typeof(string));
        dt.Columns.Add("Numero_Autor", typeof(string));
        dt.Columns.Add("Importe_Total", typeof(string));



        foreach (var items in list)
        {
            dt.Rows.Add(items);
        }


        //cargar al grid
        this.gvwAutorizaciones.DataSource = dt;
        this.gvwAutorizaciones.DataBind();

And this is for insert every gridview record in data base:

try
{
    using (MySqlConnection sqlCon = new MySqlConnection(conn))
    {
        using (MySqlCommand cmd = new MySqlCommand())
        {
            cmd.CommandText = "Insert into autorizaciones " +
                "(comprobante, Serie_Comprobante, Ruc_Emisor, RazonSoc_Emisor, " +
                "Fecha_Emision, Fecha_Autorizacion, " +
                "Tipo_Emision, Ident_Receptor, Clave_Acceso, Numero_Autorizacion) " +
                "values(" +
                "'" + comprob + "', " +
                "'" + serComprob + "', " +
                "'" + rucEmisor + "', " +
                "'" + razSocEmi + "', " +
                "'" + FecEmisor + "', " +
                "'" + FecAutoriz + "', " +
                "'" + tipoEmision + "', " +
                "'" + identRecep + "', " +
                "'" + claveAcceso + "', " +
                "'" + numAutoriz + "')";
            cmd.Connection = sqlCon;
            sqlCon.Open();
            cmd.ExecuteNonQuery();
            sqlCon.Close();
        }
    }
}

catch (MySqlException ex)
{
}

I call this in a foreach and I can insert records, but for dates, I have that problem.

Is there any way to format the column datatable, for example "yyyy-mm-dd"?

If please anyone can help me,

thanks a lot best regards

Oakar

I suppose, you should change the type of your column because of it is a type of string.

DateTime newDate= DateTime.Parse(stringDate);---> You can use this to convert string to date,

or

string s = "13/5/2014 12:00:00 AM";    
var date = DateTime.ParseExact(s, "dd/M/yyyy hh:mm:ss tt",
                                   CultureInfo.InvariantCulture); 

You can use above code block to change the format of the date time.

Also you can determine your data table column type before you create it like,

DataColumn colDateTime = new DataColumn("DateTimeCol");   
colDateTime.DataType = System.Type.GetType("System.DateTime");

All I mean, I think you have type problem so I hope this helps.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to change date format for the date column in datatable?

From Dev

convert a datatable column date format

From Dev

Change the format of the date column in Thunderbird

From Dev

Change date format of dataframe date column

From Dev

sqlite change date format of every cell in column

From Dev

Change the format of the date which is a column of a list

From Dev

Is it possible to change format of date values in a MySQL column?

From Dev

Change the format of the date which is a column of a list

From Dev

DataTables Plugin: How to format a date column in DataTable plugin?

From Dev

DataTables Plugin: How to format a date column in DataTable plugin?

From Dev

datatable, how to display table sorted by a column in this date format?

From Dev

Format DataTable Column for Percent

From Dev

change date format while displaying date column from database Yii

From Dev

Sort by date the column in datatable

From Dev

How do I change the date format in a dataframe column.

From Dev

change columns date column format in ruby sqlite3

From Dev

How to Change Format column in Datagridview to date type for this value

From Dev

How to Change Format column in Datagridview to date type for this value

From Dev

Excel Pivot table: Change the Number format of Column label(Date) to "dddd"

From Dev

Datatable Date format is not working correctly

From Dev

format column (value) of datatable dynamically

From Dev

format column (value) of datatable dynamically

From Dev

Change date and time format

From Dev

Change in Date Format

From Dev

Android Change Date Format

From Dev

change date format

From Dev

Change the date format in phpmyadmin

From Dev

Change the Format of the Date in VBA

From Dev

Change date format in OpenERP

Related Related

  1. 1

    How to change date format for the date column in datatable?

  2. 2

    convert a datatable column date format

  3. 3

    Change the format of the date column in Thunderbird

  4. 4

    Change date format of dataframe date column

  5. 5

    sqlite change date format of every cell in column

  6. 6

    Change the format of the date which is a column of a list

  7. 7

    Is it possible to change format of date values in a MySQL column?

  8. 8

    Change the format of the date which is a column of a list

  9. 9

    DataTables Plugin: How to format a date column in DataTable plugin?

  10. 10

    DataTables Plugin: How to format a date column in DataTable plugin?

  11. 11

    datatable, how to display table sorted by a column in this date format?

  12. 12

    Format DataTable Column for Percent

  13. 13

    change date format while displaying date column from database Yii

  14. 14

    Sort by date the column in datatable

  15. 15

    How do I change the date format in a dataframe column.

  16. 16

    change columns date column format in ruby sqlite3

  17. 17

    How to Change Format column in Datagridview to date type for this value

  18. 18

    How to Change Format column in Datagridview to date type for this value

  19. 19

    Excel Pivot table: Change the Number format of Column label(Date) to "dddd"

  20. 20

    Datatable Date format is not working correctly

  21. 21

    format column (value) of datatable dynamically

  22. 22

    format column (value) of datatable dynamically

  23. 23

    Change date and time format

  24. 24

    Change in Date Format

  25. 25

    Android Change Date Format

  26. 26

    change date format

  27. 27

    Change the date format in phpmyadmin

  28. 28

    Change the Format of the Date in VBA

  29. 29

    Change date format in OpenERP

HotTag

Archive