Click here to Skip to main content
15,888,102 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I get the error "An attempt to attach an auto-named database for file abc failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share"

private void btnsubmit_Click(object sender, EventArgs e)
        {
           
            String connectionString = "Data Source=USER\\SQLEXPRESS;Integrated Security=true; AttachDbFilename=abc;User Instance=true;";
            SqlConnection thisConnection = new SqlConnection(connectionString);
         
            

            thisConnection.Open();

            String thisQuery = "INSERT INTO abc (ID, Name, ContactNo, Email, Address) VALUES ('" + txtid.Text + "', '" + txtname.Text + "', '" + txtcontact.Text + "', '" + txtemail.Text + "', '" + txtaddress.Text + "')";
            SqlCommand thisCommand = new SqlCommand(thisQuery, thisConnection);
            SqlCommand cmd = new SqlCommand();

            thisCommand.ExecuteNonQuery();
            thisConnection.Close();

        }
Posted
Updated 2-Feb-12 1:47am
v4
Comments
Sridhar Patnayak 2-Feb-12 7:22am    
Repost, any way what is your error?
priyanka999 2-Feb-12 7:26am    
am usin windows authentication above sql connection is wrong i think
priyanka999 2-Feb-12 7:28am    
An attempt to attach an auto-named database for file abc failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

You haven't specified any error but i think you are getting error because of query

your ID column in DB is of integer type and you are sending it like string
Change it like this ==>
C#
String thisQuery = "INSERT INTO abc (ID, Name, ContactNo, Email, Address) VALUES (" + txtid.Text + ", '" + txtname.Text + "', '" + txtcontact.Text + "', '" + txtemail.Text + "', '" + txtaddress.Text + "')";

Or if your ID column is identity(ie autoincrement field) then no need to send it in query

C#
String thisQuery = "INSERT INTO abc ( Name, ContactNo, Email, Address) VALUES ('" + txtname.Text + "', '" + txtcontact.Text + "', '" + txtemail.Text + "', '" + txtaddress.Text + "')";


This is not a good practice to send parameters to query like this way as there'll be a greater chance of sql injection so yo should always use SQL Parameters to send to query
 
Share this answer
 
i think problem in your data base connection in code no error can you send the error message to me
 
Share this answer
 
Comments
priyanka999 2-Feb-12 7:31am    
An attempt to attach an auto-named database for file abc failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
keep the database file in deployment folder. and In you connection string just add the 'database=abc'. This prevents SQL server to create the auto-named database
 
Share this answer
 
Comments
priyanka999 2-Feb-12 7:45am    
deployment folder means
Syed Salman Raza Zaidi 2-Feb-12 7:45am    
where your exe file exists

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