System.Data.SqlClient.SqlException was unhandled by user code RRS feed

  • Question

  • how can i solve this exception

    An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code

    Additional information: An attempt to attach an auto-named database for file C: \Users\Ripon\Documents\Visual Studio 2015\WebSites\WebSite5\App_Data\Database.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.


    //step1 String connectionstring = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C: \Users\Ripon\Documents\Visual Studio 2015\WebSites\WebSite5\App_Data\Database.mdf;Integrated Security=True"; SqlConnection conn = new SqlConnection(connectionstring); //step2 conn.Open(); //step 3 String Query = "insert [User] (Name) values (@name)"; SqlCommand cmd = new SqlCommand(Query, con); cmd.Parameters.AddWithValue("@name", name.Text); //step4 cmd.ExecuteNonQuery(); // SqlDataReader ds = cmd.ExecuteReader(); //step 5 /* while(ds.NextResult()) { ds.GetBoolean; }*/ //step 6 //step 7 con.Close();

    Monday, July 29, 2019 8:14 AM


  • Your connectionstring seems to be not formatted correctly for one thing. It should be 'C:\.............'. and you have a space between 'C: \……….'.  Maybe that's the problem.

    But on the other hand.....

    How did the MDF file used by Localdb windup in the location that it's located, becuase it should be not be there if it was auto generated.

    The link explains where the Localdb file is located when it is auto generated.

    However, since this is a Website, it is best to "Attach' the MDF file to the DB engine, stop using Localdb and change the connectionstring to use the server.

    Monday, July 29, 2019 9:16 AM