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!