Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Not allowed to change the 'ConnectionString' property. The connection's current state is connecting. Why I am getting this error?

Not allowed to change the 'ConnectionString' property. The connection's current state is connecting. Why I am getting this error?

Scheduled Pinned Locked Moved C#
databasesysadminhelpquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 11968669
    wrote on last edited by
    #1

    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."

    J 1 Reply Last reply
    0
    • U User 11968669

      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."

      J Offline
      J Offline
      John Torjo
      wrote on last edited by
      #2

      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!

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups