How to create multiple CSV file in ASP .Net

I want to create multiple CSV file for every drop down Selected value. Even-though while debugging i can notice csv is appending for different drop down value but not writing another CSV file. do not know why and where i am doing mistake.

When i remove using block, and for second time its throwing error :

The process cannot access the file 'File path' because it is being used by another process.

I believe need to store all files on server but again same issue
i have added code to solve error but no change;

if (File.Exists(filePath))
                        File.Delete(filePath);

Button code

 protected void btnGenerate_Click(object sender, EventArgs e)
      {
        List<int> stopWordArray = new List<int>();
        foreach (ListItem listItem in ddlDepot.Items)
          {
           if (listItem.Selected)
            {
              string FileType = ddlqoutatfileType.SelectedItem.Text;
              DateTime Qotation_Date = Convert.ToDateTime(txtdate.Text).AddDays(3);
              var Qotation_Date_modified = Qotation_Date.ToShortDateString();
              OracleCommand CmdB = new OracleCommand("Select DEPOT_CODE, PRODUCT_CODE, TO_CHAR(TO_DATE(UPLOAD_DATE,'DD/MM/YYYY'), 'DDMMYYY') as UPLOAD_DATE , TO_CHAR(TO_DATE(SALES_DATE,'DD/MM/YYYY'), 'DDMMYYYY') as SALES_DATE, ORDER_QTY, FILE_TYPE, ROUTE_CODE from SCM_SARAS_ORDERS_vw where File_Type = '" + FileType + "' and DEPOT_CODE =" + listItem.Value + " and SALES_DATE = TO_DATE('" + Qotation_Date_modified + "' , 'DD/MM/YYYY')", con);
              CmdB.CommandType = CommandType.Text;
              OracleDataAdapter daB = new OracleDataAdapter();
              DataTable dtB = new DataTable();
              daB.SelectCommand = CmdB;
              daB.Fill(dtB);
              string csv = string.Empty;
              foreach (DataRow row in dtB.Rows)
                {
                  foreach (DataColumn column in dtB.Columns)
                    {
                     csv += row[column.ColumnName].ToString().Replace(",", ";") + ',';
                     }
                   csv += "rn";
                 }
               string fileName = "myfile.csv";
               string filePath = Server.MapPath("~/file" + fileName);
               using (StreamWriter sw = new StreamWriter(filePath, false))
                 {
                    sw.Write(csv);
                  }
                }
              }
            }

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

I am not sure what do you mean about follow problem.
#but not writing another CSV file#

in your code, obviously, you wrote same file in the loop; why do not you change it to

string fileName = "myfile"+listItem.Value+".csv";//change filename?
string filePath = Server.MapPath("~/file" + fileName);
using (StreamWriter sw = new StreamWriter(filePath, false))
{
     sw.Write(csv);
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x