Not allowed to change the 'ConnectionString' property. The connection's current state is connecting. Why I am getting this error?
-
SqlConnection conn; public SqlConnection openconn() { conn = new SqlConnection(ConfigurationManager.ConnectionStrings["CostAllocationEntities2"].ConnectionString); try { if (conn.State == ConnectionState.Closed) { conn.Open(); return conn; } } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Failed to Connect with Database.", MessageBoxButtons.OK, MessageBoxIcon.Warning); return null; } return conn; } public void GetBackup(string strDBName) { if (string.IsNullOrEmpty(strDBName)) { MessageBox.Show("Server Name & Database can not be Blank"); return; } else { string destinationPath = ConfigurationManager.AppSettings.Get("BackupDestinationPath"); BackupDeviceItem deviceItem = new BackupDeviceItem(destinationPath, DeviceType.File); ServerConnection connection = new ServerConnection(ConfigurationManager.ConnectionStrings["CostAllocationEntities2"].ConnectionString); Server sqlServer = new Server(connection); Database db = sqlServer.Databases[strDBName]; Backup bkpDBFull = new Backup(); bkpDBFull.Action = BackupActionType.Database; bkpDBFull.Database = strDBName; bkpDBFull.Devices.AddDevice(String.Format("{0}{1}-{2}.bak", destinationPath, strDBName, DateTime.Now.ToString("yyyy-MM-dd")), DeviceType.File); bkpDBFull.BackupSetName = strDBName + " Backup"; bkpDBFull.BackupSetDescription = strDBName + "- Full Backup"; bkpDBFull.ExpirationDate = DateTime.Today.AddDays(10); bkpDBFull.Initialize = false; bkpDBFull.SqlBackup(sqlServer); MessageBox.Show("Backup Done"); } } On this line Database db = sqlServer.Databases[strDBName]; I get an error: Not allowed to change the 'ConnectionString' property. The connection's current state is connecting."
-
SqlConnection conn; public SqlConnection openconn() { conn = new SqlConnection(ConfigurationManager.ConnectionStrings["CostAllocationEntities2"].ConnectionString); try { if (conn.State == ConnectionState.Closed) { conn.Open(); return conn; } } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Failed to Connect with Database.", MessageBoxButtons.OK, MessageBoxIcon.Warning); return null; } return conn; } public void GetBackup(string strDBName) { if (string.IsNullOrEmpty(strDBName)) { MessageBox.Show("Server Name & Database can not be Blank"); return; } else { string destinationPath = ConfigurationManager.AppSettings.Get("BackupDestinationPath"); BackupDeviceItem deviceItem = new BackupDeviceItem(destinationPath, DeviceType.File); ServerConnection connection = new ServerConnection(ConfigurationManager.ConnectionStrings["CostAllocationEntities2"].ConnectionString); Server sqlServer = new Server(connection); Database db = sqlServer.Databases[strDBName]; Backup bkpDBFull = new Backup(); bkpDBFull.Action = BackupActionType.Database; bkpDBFull.Database = strDBName; bkpDBFull.Devices.AddDevice(String.Format("{0}{1}-{2}.bak", destinationPath, strDBName, DateTime.Now.ToString("yyyy-MM-dd")), DeviceType.File); bkpDBFull.BackupSetName = strDBName + " Backup"; bkpDBFull.BackupSetDescription = strDBName + "- Full Backup"; bkpDBFull.ExpirationDate = DateTime.Today.AddDays(10); bkpDBFull.Initialize = false; bkpDBFull.SqlBackup(sqlServer); MessageBox.Show("Backup Done"); } } On this line Database db = sqlServer.Databases[strDBName]; I get an error: Not allowed to change the 'ConnectionString' property. The connection's current state is connecting."
Check this out: https://msdn.microsoft.com/en-us/library/System.Data.SqlClient.SqlConnection%28v=vs.110%29.aspx[^] https://msdn.microsoft.com/en-us/library/System.Data.SqlClient.SqlConnection%28v=vs.110%29.aspx[^] I believe the connection is done asynchronously, thus you need to listen to the "StateChange" event. Also note that from your code, it seems you're not using 'db' at all - so you can actually remove that line. Best, John
-- LogWizard - Log Viewing can be a joy!