using (MySqlCommand cmd = new MySqlCommand("SELECT  EmpId, Emp_Name, Salary , Start_date , End_date , CASE WHEN (Employees.End_Date is null)  THEN DATEDIFF(DATE_ADD(Start_Date, INTERVAL 30 DAY), Start_Date) * Salary/30 ELSE DATEDIFF(End_Date, Start_Date) * Salary/30  END AS Total_Salary From Employees;"))
        {
            using (MySqlDataAdapter sda = new MySqlDataAdapter())
            {
                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=SqlExport.csv");
                    Response.Charset = "";
                    Response.ContentType = "application/text";
                    Response.Output.Write(csv);
                    Response.Flush();
                    Response.End();
                    return RedirectToAction("GetAllEmpDetails");
                }
            }
        }

}

Recommended Answers

All 3 Replies

Code dumps without comments or questions are usually unanswered. Add details, questions and where you are having issues.

One observation is that I don't know the size of your database but the way that the variable or array csv is handled (or rather not) it looks like your array could grow without end and be a memory hog. You didn't reveal much at all in your post about the problem so leaving this for others to guess results in bad guesses.

Before you reply, read https://www.daniweb.com/programming/threads/435023/read-this-before-posting-a-question

Sorry My Mistake i forgot to tell about the issue. I am attaching the issue image which occuring while export of CSV.

http://imgur.com/a/LyP39

Please check this image and please help me out.

Looked at the image and yes, that looks like what a spreadsheet could show from a CSV file so you did get CSV out but left me to guess what the problems are. The row with the titles is off but that's cosmetic and should be well inside your problem solving skills.

As above, don't make others guess or they will guess badly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.