Reputation: 63
protected void Button1_Click(object sender, EventArgs e)
{
if (TextName.Text != "" && TextPass.Text != "" && TextRePass.Text != "" && TextAddr.Text != "" && TextPhn.Text != "")
{
SqlConnection i = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\Study materials\Mobile Shop\App_Data\;Integrated Security=True;User Instance=True");
i.Open();
string q = "insert into Register values ('" + TextName.Text + "','" + TextRePass.Text + "','" + TextAddr.Text + "','" + TextPhn.Text + "','" + Label1.Text + "','" + Label2.Text + "')";
SqlCommand cmd = new SqlCommand(q, i);
cmd.ExecuteNonQuery();
Label3.Text = "DETAILS ENTERED SUCCESSFULLY IN THE DATABASE!!";
}
else
{
Label4.Text = "ALL FIELDS ARE MANDATORY";
}
}
I having a error here:
i.Open();
Error is:
An attempt to attach an auto-named database for file E:\Study materials\Mobile Shop\App_Data\ failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
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.Data.SqlClient.SqlException: An attempt to attach an auto-named database for file E:\Study materials\Mobile Shop\App_Data\ failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
Upvotes: 1
Views: 3024
Reputation: 17380
You need a database name in the Connection String:
SqlConnection i = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\Study materials\Mobile Shop\App_Data\<DB Name Here>;Integrated Security=True;User Instance=True");
^^^^^^^^^^^^
Upvotes: 1