Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

SQL
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

C#
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:

XML
<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 3: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

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