I've been having a Server Error in '/' Application when im running my website. I have already tried troubleshooting the problem but i still can't figure out where the error is coming from. It states that the process cannot access the file because it is being used by another process. Before this happens, I created a code where if the admin wants to create a new brand, it will automatically write new piece of code on files that i target it to. And also if the admin wants to delete the brand, it will remove all the new code that i wrote previously.
Everything worked fine yesterday but visual studio is prompting me that there are changes made on files outside of IDE and it kept on checking for consistent lines on my code. I just clicked yes to all to update the code. But i want it to automatically reload the files so i went to Tools-Options-Documents-then i checked Reload modified files unless there are unsaved changes. After that, i can still run my website and create new brands but whenever i try to remove the brand, it is giving me this server error in '/' application.
Here is the full description of the error:
Server Error in '/' Application.
The process cannot access the file 'C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\App_Code\ConnectionClassGuitarItems.cs' because it is being used by another process.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.IO.IOException: The process cannot access the file 'C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\App_Code\ConnectionClassGuitarItems.cs' because it is being used by another process.
Source Error:
Line 184: }
Line 185:
Line 186: System.IO.File.WriteAllText(@"C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\App_Code\ConnectionClassGuitarItems.cs", result);
Line 187: }
Line 188: }
Source File: c:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\Pages\OverviewGuitarData.aspx.cs Line: 186
Stack Trace:
[IOException: The process cannot access the file ' C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\App_Code\ConnectionClassGuitarItems.cs' because it is being used by another process.]
System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +216
System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) +1326
System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) +66
System.IO.StreamWriter.CreateFile(String path, Boolean append, Boolean checkHost) +73
System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize, Boolean checkHost) +73
System.IO.File.InternalWriteAllText(String path, String contents, Encoding encoding, Boolean checkHost) +64
System.IO.File.WriteAllText(String path, String contents) +43
Pages_OverviewData.RemoveConnectionClassGuitarItems(String name) in c:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\Pages\OverviewGuitarData.aspx.cs:186
Pages_OverviewData.GuitarBrandsGridViewBtn_Click(Object sender, EventArgs e) in c:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\Pages\OverviewGuitarData.aspx.cs:73
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9696694
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +204
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +12
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +15
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1639
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.6.1055.0
What I have tried:
protected void GuitarBrandsGridViewBtn_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
GridViewRow gridrow = btn.NamingContainer as GridViewRow;
int id = Convert.ToInt32(GuitarBrandsGridView.DataKeys[gridrow.RowIndex].Value.ToString());
string name = GuitarBrandsGridView.Rows[gridrow.RowIndex].Cells[4].Text;
con1.Open();
cmd1.CommandText = "DELETE FROM [guitarBrands] WHERE id=" + id;
cmd1.Connection = con1;
int a = cmd1.ExecuteNonQuery();
con1.Close();
if (a > 0)
{
bindgridviewguitarbrands();
}
RemoveAddGuitarClass(name);
RemoveOverviewGuitarDataCode(name);
RemoveConnectionClassGuitarItems(name);
RemoveOverviewGuitarDataASPX(name);
System.IO.File.Delete(@"C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\Pages\GuitarItems" + id + ".aspx");
System.IO.File.Delete(@"C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\Pages\GuitarItems" + id + ".aspx.cs");
}
protected void RemoveAddGuitarClass(string name)
{
int counter = 0;
string line;
System.IO.StreamReader file1 = new System.IO.StreamReader(@"C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\App_Code\AddGuitarClass.cs");
while ((line = file1.ReadLine()) != null)
{
if (line.Contains(" case \"" + name + "\":"))
{
break;
}
counter += 1;
}
file1.Close();
RemoveAddGuitarClassCont(counter);
}
protected void RemoveAddGuitarClassCont(int counter)
{
int removeAt = counter;
removeAt -= 1;
int linesToRemove = 20;
string s = System.IO.File.ReadAllText(@"C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\App_Code\AddGuitarClass.cs");
List<string> arr = s.Split("\n".ToCharArray()).ToList();
string result = "";
for (int i = 0; i < linesToRemove; i++)
{
arr.RemoveAt(removeAt);
result = "";
foreach (string str in arr)
{
result += str + "\n";
}
System.IO.File.WriteAllText(@"C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\App_Code\AddGuitarClass.cs", result);
}
}
protected void RemoveOverviewGuitarDataCode(string name)
{
int counter = 0;
string line;
System.IO.StreamReader file2 = new System.IO.StreamReader(@"C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\Pages\OverviewGuitarData.aspx.cs");
while ((line = file2.ReadLine()) != null)
{
if (line.Contains(" //Start of Gridview Code for " + name + " Guitars"))
{
break;
}
counter += 1;
}
file2.Close();
RemoveOverviewGuitarDataCodeCont(counter);
}
protected void RemoveOverviewGuitarDataCodeCont(int counter)
{
int removeAt = counter;
removeAt -= 1;
int linesToRemove = 41;
string s = System.IO.File.ReadAllText(@"C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\Pages\OverviewGuitarData.aspx.cs");
List<string> arr = s.Split("\n".ToCharArray()).ToList();
string result = "";
for (int i = 0; i < linesToRemove; i++)
{
arr.RemoveAt(removeAt);
result = "";
foreach (string str in arr)
{
result += str + "\n";
}
System.IO.File.WriteAllText(@"C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\Pages\OverviewGuitarData.aspx.cs", result);
}
}
protected void RemoveConnectionClassGuitarItems(string name)
{
int counter = 0;
string line;
System.IO.StreamReader file3 = new System.IO.StreamReader(@"C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\App_Code\ConnectionClassGuitarItems.cs");
while ((line = file3.ReadLine()) != null)
{
if (line.Contains(" public static ArrayList GetItems" + name + "(string itemCategory)"))
{
break;
}
counter += 1;
}
file3.Close();
RemoveConnectionClassGuitarItemsCont(counter);
}
protected void RemoveConnectionClassGuitarItemsCont(int counter)
{
int removeAt = counter;
removeAt -= 2;
int linesToRemove = 44;
string s = System.IO.File.ReadAllText(@"C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\App_Code\ConnectionClassGuitarItems.cs");
List<string> arr = s.Split("\n".ToCharArray()).ToList();
string result = "";
for (int i = 0; i < linesToRemove; i++)
{
arr.RemoveAt(removeAt);
result = "";
foreach (string str in arr)
{
result += str + "\n";
}
System.IO.File.WriteAllText(@"C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\App_Code\ConnectionClassGuitarItems.cs", result);
}
}
protected void RemoveOverviewGuitarDataASPX(string name)
{
int counter = 0;
string line;
System.IO.StreamReader file4 = new System.IO.StreamReader(@"C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\Pages\OverviewGuitarData.aspx");
while ((line = file4.ReadLine()) != null)
{
if (line.Contains(" <h3>" + name + " Guitar Items Data</h3>"))
{
break;
}
counter += 1;
}
file4.Close();
RemoveOverviewGuitarDataASPXCont(counter);
}
protected void RemoveOverviewGuitarDataASPXCont(int counter)
{
int removeAt = counter;
removeAt -= 2;
int linesToRemove = 44;
string s = System.IO.File.ReadAllText(@"C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\App_Code\ConnectionClassGuitarItems.cs");
List<string> arr = s.Split("\n".ToCharArray()).ToList();
string result = "";
for (int i = 0; i < linesToRemove; i++)
{
arr.RemoveAt(removeAt);
result = "";
foreach (string str in arr)
{
result += str + "\n";
}
System.IO.File.WriteAllText(@"C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\App_Code\ConnectionClassGuitarItems.cs", result);
}
}