Click here to Skip to main content
15,895,283 members
Please Sign up or sign in to vote.
1.11/5 (2 votes)
See more:
C#
public void ExportCSV_Driver()
        {
            string constr = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand("SELECT DriverName,Address,ContactNo,DLNo,CONVERT(char(10), DLExpiryDate,126) As DLExpiryDate ,Status FROM [dbo].[DriverRegistration]"))
                {
                    using (SqlDataAdapter sda = new SqlDataAdapter())
                    {
                        cmd.Connection = con;
                        sda.SelectCommand = cmd;
                        using (DataTable dt = new DataTable())
                        {
                            sda.Fill(dt);

                            //Build the CSV file data as a Comma separated string.
                            string csv = string.Empty;

                            foreach (DataColumn column in dt.Columns)
                            {
                                //Add the Header row for CSV file.
                                csv += column.ColumnName + ',';
                            }

                            //Add new line.
                            csv += "\r\n";

                            foreach (DataRow row in dt.Rows)
                            {
                                foreach (DataColumn column in dt.Columns)
                                {
                                    //Add the Data rows.
                                    csv += row[column.ColumnName].ToString().Replace(",", ";") + ',';
                                }

                                //Add new line.
                                csv += "\r\n";
                            }

                            //Download the CSV file.
                            Response.Clear();
                            Response.Buffer = true;
                            Response.AddHeader("content-disposition", "attachment;filename=Driver.csv");
                            Response.Charset = "";
                            Response.ContentType = "application/text";
                            Response.Output.Write(csv);
                            Response.Flush();
                            Response.End();
                        }
                    }
                }
            }
        }


What I have tried:

above is my code and i want to know how to download CSV File With Format in YYYY-MM_DD
because when it is download it takes default dd-mm-yyy

so please help me urgent
Posted
Updated 17-Sep-18 21:39pm
v2

First off, don't use a string - use a StringBuilder, it's a lot more efficient when dealing with lots of data.

Second, your code is doing exactly what you want - the CONVERT(...,126) part of the SELECT query ensures that, and if you look at the actual CSV data you are downloading to your client, you will see that the dates are in the form yyyy-MM-dd
What's happening after that is down to whatever the client is using to display the data: it's reading the CSV, converting the date string to a Date value (which is exactly what it should do) and displaying it in the users preferred date format.

You need to look at what the user is doing, not at that code.
 
Share this answer
 
Comments
Member 12183079 18-Sep-18 6:27am    
so please give me code how to do
Member 12183079 18-Sep-18 6:27am    
write a code for lock
OriginalGriff 18-Sep-18 6:48am    
Read what I said: it's what the user is doing with the data, not any code I could give you.
[no name] 18-Sep-18 9:25am    
u may try my code,this code is completely right.
You may try this..
u will get ur desired output
C#
/*How to download CSV file with default set YYYY-MM-DD in C#*/

/*use this code to get date in YYYY-MM-DD From Sql*/
REPLACE(CONVERT(CHAR(11),Date, 106),' ','-') AS Date


/* use this function to Export/Download CSV File On button Click*/
  private void ExportExcelFile()
    {
       
            DataSet dsExcel = new DataSet();
            dsExcel.Tables.Clear();

            DataTable dtExcelReport = new DataTable();

            dtExcelReport.TableName = "Report";
            dtExcelReport.Columns.Add("Code");
            dtExcelReport.Columns.Add("Name");
           
            
            dtExcelReport.Columns.Add("Date");
          
                
                
                    aPL.Id= Id.Text;
                   
                    DataTable dt = aBL.Detail(aPL);
                    foreach (DataRow dr in dt.Rows)
                    {
                        DataRow drExcelReport = dtExcelReport.NewRow();
                        drExcelReport["Code"] = dr["Code"].ToString();
                        drExcelReport["Name"] = dr["Name"].ToString();
                      
                        drExcelReport["Date"] = dr["Date"].ToString();
                       
                        dtExcelReport.Rows.Add(drExcelReport);
                    }
                    dsExcel.Tables.Add(dtExcelReport);
                    ExcelHelper.ToExcel(dtExcelReport, "" + DateTime.Now.ToString("dd-MMMM-yyyy") + "_Report.xls", Page.Response);
                
            
        
    }

/*For Detail use this code In layer base*/
public DataTable Detail(A_PAL pl)
    {
        A_DAL obj = new A_DAL();
        return obj.Detail(pl);
    }
public DataTable Detail(A_PAL pl)
    {
        con.cmd.CommandText = "SP_ProcedureName";
        con.cmd.CommandType = CommandType.StoredProcedure;
        con.cmd.Parameters.AddWithValue("@Id", pl.Id);
        SqlDataReader sdr;
        DataTable dt = new DataTable();
        con.Open();
        sdr = con.cmd.ExecuteReader();
        dt.Load(sdr);
        con.Close();
        return dt;
    }

/*Procedure On Sql To get data*/
CREATE PROCEDURE [dbo].[SP_ProcedureName]
(
@Id bigint=null
)
AS
BEGIN
SELECT  Code,Name ,REPLACE(CONVERT(CHAR(11),Date, 106),' ','-') AS Date
FROM tbl_Date
WHERE Id=@Id
END
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900