title: How to backup the database from the server to the client computer using asp.net page
body: <p>I have used code C # to write one page of data to back up the .bak file from the server to the client machine. when I tested on my computer is very good. which when connected through another computer by using connectionString web.cofig it reappears errors like this. This one was like before or workaround, please help.</p>
  
  <p><a href="https://i.stack.imgur.com/Ad6pP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Ad6pP.png" alt="enter image description here"></a></p>
  
  <p>private string _ConnectionString = ConfigurationManager.ConnectionStrings["connectionString"].ToString();</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {
      if (!Page.IsPostBack)
      {
          FillDatabases();
          //ReadBackupFiles();
      }
  }
  
  private void FillDatabases()
  {
      try
      {
          SqlConnection sqlConnection = new SqlConnection();
          sqlConnection.ConnectionString = _ConnectionString;
          sqlConnection.Open();
          string sqlQuery = "SELECT * FROM sys.databases";
          SqlCommand sqlCommand = new SqlCommand(sqlQuery, sqlConnection);
          sqlCommand.CommandType = CommandType.Text;
          SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
          DataSet dataSet = new DataSet();
          sqlDataAdapter.Fill(dataSet);
          ddlDatabases.DataSource = dataSet.Tables[0];
          ddlDatabases.DataTextField = "name";
          ddlDatabases.DataValueField = "database_id";
          ddlDatabases.DataBind();
      }
      catch (SqlException sqlException)
      {
          lblMessage.Text = sqlException.Message.ToString();
      }
      catch (Exception exception)
      {
          lblMessage.Text = exception.Message.ToString();
      }
  }
  
  protected void btnBackup_Click(object sender, EventArgs e)
  {
      try
      {
          string _DatabaseName = ddlDatabases.SelectedItem.Text.ToString();
          string _BackupName = _DatabaseName + "_" + DateTime.Now.Day.ToString() + "_" + DateTime.Now.Month.ToString() + "_" + DateTime.Now.Year.ToString() + ".bak";
          SqlConnection sqlConnection = new SqlConnection();
          sqlConnection.ConnectionString = _ConnectionString;
          sqlConnection.Open();
          string sqlQuery = "BACKUP DATABASE " + _DatabaseName + " TO DISK = 'D:\\SQLServerBackups\\" + _BackupName + "' WITH FORMAT, MEDIANAME = 'Z_SQLServerBackups', NAME = '" + _BackupName + "';";
          SqlCommand sqlCommand = new SqlCommand(sqlQuery, sqlConnection);
          sqlCommand.CommandType = CommandType.Text;
          int iRows = sqlCommand.ExecuteNonQuery();
          sqlConnection.Close();
          lblMessage.Text = "The " + _DatabaseName + " database Backup with the name " + _BackupName + " successfully...";
          ReadBackupFiles();
      }
      catch (SqlException sqlException)
      {
          lblMessage.Text = sqlException.Message.ToString();
      }
      catch (Exception exception)
      {
          lblMessage.Text = exception.Message.ToString();
      }
  }
  </code></pre>