none
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.

    //ADO.NET

    //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

Answers

  • 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.

    https://docs.microsoft.com/en-us/sql/database-engine/configure-windows/sql-server-express-localdb?view=sql-server-2017

    https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/introduction/creating-a-connection-string

    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