Click here to Skip to main content
15,894,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The process cannot access the file 'C:\Users\God\Desktop\DataDump\1Excel.xls' because it is being used by another process.

C#
int count = 0;
           string connectionstring = "Server=(local);initial catalog=Test;Trusted_Connection=True";
           SqlConnection sqlConnection = new SqlConnection(connectionstring);
           SqlCommand cmd = new SqlCommand();
           SqlDataReader reader;
           DataSet ds = new DataSet();
           cmd.CommandText = "select * from Empdetails";
           cmd.CommandText += " where shifttype  = @par ";
           cmd.Parameters.Add("@par", SqlDbType.Int).Value = j;
           cmd.CommandType = CommandType.Text;
           cmd.Connection = sqlConnection;
           sqlConnection.Open();
           reader = cmd.ExecuteReader();
           if (reader.HasRows)
           {
               System.IO.StreamWriter sw_In = new System.IO.StreamWriter(@"C:\Users\God\Desktop\DataDump\" + j + "Excel.xls");
               while (reader.Read())
               {
                   if (count == 0)
                   {
                       for (int i = 0; i < reader.FieldCount; i++)
                       {
                           sw_In.AutoFlush = true;
                           sw_In.Write(reader.GetName(i) + "\t");
                       }
                       sw_In.Write("\n");
                       count = 1;
                   }
                   for (int i = 0; i < reader.FieldCount; i++)
                   {
                       sw_In.AutoFlush = true;
                       sw_In.Write(reader[i].ToString() + "\t");
                   }

                   MailMessage mis = new MailMessage();
                   SmtpClient smtpserver = new SmtpClient("smtp.gmail.com");
                   smtpserver.Credentials = new System.Net.NetworkCredential("narasiman1986@gmail.com", "narasiman123");
                   smtpserver.Host = "smtp.gmail.com";
                   smtpserver.Port = 587;
                   smtpserver.EnableSsl = true;
                   mis.From = new MailAddress("narasiman1986@gmail.com", "Report");
                   mis.IsBodyHtml = true;
                   System.Net.Mail.Attachment attachment;
                   attachment = new 
System.Net.Mail.Attachment(@"C:\Users\God\Desktop\DataDump\" + j + "Excel.xls");
                   mis.Attachments.Add(attachment);

                   mis.To.Add("narasiman1986@gmail.com");
                   mis.CC.Add(new MailAddress("narasiman1986@gmail.com"));
                   mis.Subject = "Data Dump Report";
                   smtpserver.Send(mis);

                   sw_In.Write("\n");
               }

           }
           sqlConnection.Close();
           reader.Close();

when i run the above code shows error as follows

The process cannot access the file 'C:\Users\God\Desktop\DataDump\1Excel.xls' because it is being used by another process.

the error shows in below line as follows

System.Net.Mail.Attachment(@"C:\Users\God\Desktop\DataDump\" + j + "Excel.xls");
i am sending the excel file to mail.

What I have tried:

i am sending excel file to mail. but i am getting error as follows

The process cannot access the file 'C:\Users\God\Desktop\DataDump\1Excel.xls' because it is being used by another process.
Posted
Updated 27-Aug-16 23:11pm
Comments
Richard MacCutchan 28-Aug-16 8:08am    
You cannot send an email attachment that comes from a file that has not been created properly. And you are still not creating Excel files. You should change the extension to .txt, since that is the type of file you are creating.

1 solution

There is nothing to do with your code here, the error message is pretty explicit, the problem is not in your code:
Quote:
The process cannot access the file 'C:\Users\God\Desktop\DataDump\1Excel.xls' because it is being used by another process.
Which word you don't understand in the error message ?
 
Share this answer
 

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