I know it may seem a duplicate question but previous questions could not solve my problem.
I used this code to restore the backup that I have created.
public string conrestore = "Data Source=.;Integrated Security=True";
private void btn_restore_Click(object sender, EventArgs e)
{
SqlConnection.ClearAllPools();
using (SqlConnection con = new SqlConnection(conrestore))
{
ServerConnection srvconn = new ServerConnection(con);
Server srvr = new Server(srvconn);
if (srvr != null)
{
try
{
Restore restoredb = new Restore();
restoredb.Action = RestoreActionType.Database;
restoredb.Database = "3LStoreDB";
OpenFileDialog openfd = new OpenFileDialog();
openfd.Filter = "Backup File (*.Bak)|*.Bak";
Nullable<bool> result = Convert.ToBoolean(openfd.ShowDialog());
if (result == true)
{
BackupDeviceItem bkDevice = new BackupDeviceItem(openfd.FileName, DeviceType.File);
restoredb.Devices.Add(bkDevice);
restoredb.ReplaceDatabase = true;
restoredb.SqlRestore(srvr);
MessageBox.Show("Restored successfully.");
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
It works properly till the line
restoredb.SqlRestore(srvr);
And it shows error Restore failed for server 'Server Name'
I don't know what's wrong with this code and I would really appreciate any help.
Well it's amazing and I don't know why this happened. But I Logged Off windows and again Logged in and now it works properly.