Click here to Skip to main content
14,384,969 members
Rate this:
Please Sign up or sign in to vote.
Hi, so I have a database (using the database provided by visual studio 2013) and I need to kill the database process so I can use it on another "button". The current error when i try to press this button (this button directly accesses the database) is below:

Unable to open the physical file "C:\Users\Slay3r\Documents\Visual Studio 2013\Projects\Project Thesis\Project Thesis\App_Data\ProjectThesis.mdf". Operating system error 32: "32(The process cannot access the file because it is being used by another process.)".
An attempt to attach an auto-named database for file C:\Users\Slay3r\Documents\Visual Studio 2013\Projects\Project Thesis\Project Thesis\App_Data\ProjectThesis.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.


I have the following code on an aspx

public void SaveToDatabase()
{
    string connectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ProjectThesis.mdf;Integrated Security=True;User Instance=True";
    using (SqlConnection conn = new SqlConnection(connectionString))
    {
        SqlCommand CmdSql = new SqlCommand("INSERT INTO BookDetails (ISBN,BookTitle,Price,WebLink,WebSource) VALUES (@ISBN,@BookTitle,@Price,@WebLink, @WebSource)", conn);
        conn.Open();
        CmdSql.Parameters.AddWithValue("@ISBN", ISBN);
        CmdSql.Parameters.AddWithValue("@BookTitle", Title);
        CmdSql.Parameters.AddWithValue("@Price", Prices.Price);
        CmdSql.Parameters.AddWithValue("@WebLink", Url);
        CmdSql.Parameters.AddWithValue("@WebSource", Source);
        CmdSql.ExecuteNonQuery();
        conn.Close();
    }
}


and

public DataSet GetDetails()
{

DataTable dt = new DataTable();

dt.Columns.Add("ISBN");
dt.Columns.Add("Book Title");
dt.Columns.Add("Price", typeof(double));
dt.Columns.Add("Web Link");
dt.Columns.Add("Web Source");

foreach (BookDetails detail in Collection)
{
DataRow dr = dt.NewRow();
dr[0] = detail.ISBN;
dr[1] = detail.Title;
dr[2] = detail.Prices.Price;
dr[3] = detail.Url;
dr[4] = detail.Source;
dt.Rows.Add(dr);
}

DataSet ds = new DataSet();
ds.Tables.Add(dt);
return ds;
}

On another aspx page i have a gridview that has the current code:

<h2 class="title"><a>Book Database Entry</a></h2>
    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" Height="100px" Width="500px">
    <Columns>
        <asp:BoundField DataField="DateCreated" HeaderText="DateCreated" SortExpression="DateCreated" />
        <asp:BoundField DataField="ISBN" HeaderText="ISBN" SortExpression="ISBN" />
        <asp:BoundField DataField="BookTitle" HeaderText="BookTitle" SortExpression="BookTitle" />
        <asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" />
          <asp:TemplateField HeaderText="WebLink">
               <ItemTemplate>
                   <asp:HyperLink ID="HyperLink1" Text='<%# Eval("WebLink")%>' runat="server" NavigateUrl='<%# Eval("WebLink") %>'></asp:HyperLink>
               </ItemTemplate>
           </asp:TemplateField>
        <asp:BoundField DataField="WebSource" HeaderText="WebSource" SortExpression="WebSource" />
    </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString2 %>" SelectCommand="SELECT [BookTitle], [Price], [WebLink], [WebSource], [DateCreated], [ISBN] FROM [BookDetails] ORDER BY [DateCreated] DESC"></asp:SqlDataSource>


In order to correct the error I need to kill the current database process right? how do i do that?

Thanks!
Posted
Updated 22-Feb-14 4:20am
v2
Comments
Krunal Rohit 22-Feb-14 9:33am
   
Go to Task Manager and restart all the services of SQL Server.
-KR
agent_kruger 22-Feb-14 22:50pm
   
sir, i am not expert in SQL but i think till closing down the services the database process would be over.
Krunal Rohit 22-Feb-14 23:29pm
   
As I said, Restart.!
-KR
agent_kruger 22-Feb-14 23:43pm
   
sir, but i think till "restarting" the services the database process would be over.
Krunal Rohit 22-Feb-14 23:54pm
   
Ya that means all other processes which using these SQL services might not have connected and then you wouldn't have this error.
And I'm also not an expert nor sir, just a student.. :) :)

-KR
agent_kruger 22-Feb-14 23:55pm
   
who said i am an expert? i am a student.. :) :)
Krunal Rohit 22-Feb-14 23:56pm
   
Cheers :)
Try out what I have said.
-KR
agent_kruger 23-Feb-14 0:02am
   
sir, actually this is not my question. :)
slayasty 22-Feb-14 9:38am
   
no no, you misunderstood. I need to this in the code itself. If i restart the web application and go directly to that second page all works fine. I just need to stop the process and basically restart it so I can use it on the other aspxpage
agent_kruger 22-Feb-14 22:52pm
   
can you tell us, why you want to stop the database process as the process is meant to be completed.
slayasty 23-Feb-14 9:39am
   
the problem is that I am trying to access another page that uses the database. I have another aspx page that finds data from a website and that data is stored in the database.
now the other aspx page is used to display all the data that is in the database. when i switch between them i get the error above.
ZurdoDev 22-Feb-14 17:19pm
   
What exactly is the problem? You don't stop db processes. This does not make sense.
agent_kruger 22-Feb-14 23:42pm
   
exactly
slayasty 23-Feb-14 9:39am
   
the problem is that I am trying to access another page that uses the database. I have another aspx page that finds data from a website and that data is stored in the database.
now the other aspx page is used to display all the data that is in the database. when i switch between them i get the error above.
ZurdoDev 23-Feb-14 12:45pm
   
Then something else is setup wrong. Databases are meant to be accessed by many connections at the same time.
slayasty 23-Feb-14 12:51pm
   
I see. Ill try to check what is conflicting in my setup. Thanks!

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100