Connection string for localhost debugging / C# in code behind.

Discussion in 'Site Programming, Development and Design' started by Charlie, May 16, 2010.

  1. This post is of possible use to those less experienced people working with C#
    who are unable to debug on localhost because their connection string from their web config file will not work in a code behind page.

    After many many hours of googling and trying to solve this problem and subsequently having to debug online for the best part of 6 weeks I discovered that a typical connection string in the web config file - like this:

    SqlConnection conn = new SqlConnection("server=(local)\SQLEXPRESS;AttachDbFilename=|DataDirectory|\MainDB1.mdf;Integrated Security=True;User Instance=True");

    . . . does not work because the C# code behind page will think that the back-slash means a new line.

    For the above connection string to work in localhost it should read:

    SqlConnection conn = new SqlConnection("server=(local)\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\MainDB1.mdf;Integrated Security=True;User Instance=True");

    Hope that helps someone!
  2. Ray


    Tanx for the post.

Share This Page