Instead of generating the connection string in cs, it would be a best practice to use connection string in web.config.
So, when you publish to different server, just change the web.config.
http://www.connectionstrings.com/Articles/Show/store-connection-string-in-web-config[
^]
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringNameFromWebConfig"].ConnectionString);
Hope this helps.
cheers