I’m trying to insert values in the table but when I click on the SignUp button it gives me error
An attempt to attach an auto-named database for file Database.mdf
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 Database.mdf failed. A database
with the same name exists, or specified file cannot be opened, or it
is located on UNC share.Source Error:
Line 22: string con =ConfigurationManager.ConnectionStrings[“connection”].ConnectionString;
Line 23: SqlConnection conn = new SqlConnection(con);
Line 24:
conn.Open(); //error line
Line 25: if (selectques.SelectedItem.Text ==
“Write your own question?”)
Line 26: {
My button event
protected void signup_Click(object sender, EventArgs e)
{
string con = ConfigurationManager.ConnectionStrings["connection"].ConnectionString;
SqlConnection conn = new SqlConnection(con);
conn.Open();
if (selectques.SelectedItem.Text == "Write your own question?")
{
SqlCommand cmd = new SqlCommand("insert into registration values('" + username + "','" + passwrd + "','" + emailadd + "','" + alterquestion + "','" + securityanswer + "'", conn);
cmd.ExecuteNonQuery();
conn.Close();
}
else
{
SqlCommand cmd = new SqlCommand("insert into registration values('" + username + "','" + passwrd + "','" + emailadd + "','" + selectques + "','" + securityanswer + "'", conn);
cmd.ExecuteNonQuery();
conn.Close();
}
And my web.config connectionstring
<configuration>
<appSettings/>
<connectionStrings>
<add name="connection"
connectionString="server=.\sqlexpress; AttachdbFilename=Database.mdf; integrated security=true; user instance=true"/>
</connectionStrings>
This part of your connection string is wrong:
AttachdbFilenameneeds a full path to the MDF file, not just its name.Source:
Connecting to SQL Server Express User Instances (ADO.NET)
Quote from the link: