title: SQL Server Management Objects (SMO) - Login failed for user.Error on production server
body: <p>I have written a code which take backup of database (Generate Sql script) on button click, This code is <strong>working fine on localhost</strong>, However when i deploy this to production i get the following error.</p>
  
  <blockquote>
    <p>Login failed for user 'XXXXXX\IWPD_1257(XXXXX)'</p>
  </blockquote>
  
  <p>A part of Stack Trace is</p>
  
  <pre><code>[ConnectionFailureException: Failed to connect to server ..]
     Microsoft.SqlServer.Management.Smo.DatabaseCollection.get_Item(String name) +370
     LeadWeb.Backup.scriptTables() +232
     LeadWeb.Backup.btnsubmit_OnClick(Object sender, EventArgs e) +5
     System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9628114
     System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +103
     System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
     System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
     System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
     System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724
  </code></pre>
  
  <p>Here is my code</p>
  
  <pre><code>public void scriptTables()
          {
              try
              {
                  string folder = HttpContext.Current.Server.MapPath("~/Excels/data.sql");// I have this file 
                  System.IO.File.WriteAllText(folder, string.Empty);
                  Server myServer = new Server(ConfigurationManager.AppSettings["server"]);
                  Database CMSDB = myServer.Databases[ConfigurationManager.AppSettings["Database"]];
  
                  Scripter script = new Scripter(myServer);
                  ScriptingOptions so = new ScriptingOptions();
                  so.AnsiPadding = true;
                  so.IncludeHeaders = true;
                  so.Default = true;
                  so.DriForeignKeys = true;
                  so.DriPrimaryKey = true;
                  so.DriUniqueKeys = true;
                  so.ScriptData = true;
                  so.ScriptSchema = true;
                  so.ScriptDrops = false;
  
  
                  StringBuilder ss = new StringBuilder();
  
                  foreach (Table table in CMSDB.Tables)
                  {
                      string tables = table.Name;
                      string filename = folder;
  
                      so.FileName = filename;
                      so.AppendToFile = true;
                      CMSDB.Tables[tables].EnumScript(so);
                  }
                  HttpContext.Current.Response.Clear();
                  HttpContext.Current.Response.ClearHeaders();
                  HttpContext.Current.Response.ClearContent();
                  HttpContext.Current.Response.AddHeader("Content-Disposition",
                      "attachment; filename=" + "LeadBackup" + DateTime.Now.Date.ToString("d") + ".sql");
                  HttpContext.Current.Response.AddHeader("Content-Length", folder.Length.ToString());
                  HttpContext.Current.Response.ContentType = "text/plain";
                  HttpContext.Current.Response.Flush();
                  HttpContext.Current.Response.TransmitFile(folder);
                  HttpContext.Current.Response.End();
  
              }
              catch (Exception e)
              {
  
                  Response.Write(e.Message);
              }
  </code></pre>
  
  <p>Webconfig values</p>
  
  <p><code>&lt;add key="Server" value="." /&gt; // changes according to server
   &lt;add key="Database" value="Lead" /&gt; // changes according to Database</code></p>
  
  <p>Any help or guidance.?</p>