Format of the initialization string does not conform to to specification starting at index 0

user1800361 picture user1800361 · Apr 25, 2013 · Viewed 55k times · Source

I am using Microsoft Enterprise Lip I I have this method to Insert resource in the website I get this error down i don't think it is permission problem and really i don't know how to solve it.by the way I test the connectionStrings and it work fine

  <connectionStrings>
    <add name="SiteSqlServer"
      connectionString="Data Source=.;Initial Catalog=databaseName;User ID=sa;Password=***"/>
  </connectionStrings>

 public static int Insert(Resoursce r)
        {
            Database objDB = new SqlDatabase("SiteSqlServer");
            int val = 0;
            using (DbCommand cmd = objDB.GetStoredProcCommand("InsertResoursce"))
            {
                //  OutParameter
                objDB.AddOutParameter(cmd,"@OutResoursceID",DbType.Int32,int.MaxValue);

                //  iNParameter
                objDB.AddInParameter(cmd, "@ModuleId", DbType.Int32, r.ModuleId);
                objDB.AddInParameter(cmd, "@Summary", DbType.StringFixedLength, r.Summary);
                objDB.AddInParameter(cmd, "@PageId", DbType.StringFixedLength, r.PageID);
                objDB.AddInParameter(cmd, "@TypeId", DbType.Int32, r.TypeID);
                objDB.AddInParameter(cmd, "@UserID", DbType.Guid, r.UserID);
                objDB.AddInParameter(cmd, "@Enabled", DbType.Boolean, r.Enabled);
                objDB.AddInParameter(cmd, "@SafetyAlert", DbType.Boolean, r.SafetyAlert);
                objDB.AddInParameter(cmd, "@SaftyAlertText", DbType.StringFixedLength, r.SafetyAlertText);


                try
                {
                     val = objDB.ExecuteNonQuery(cmd);
                     if (val == 1)
                     {
                         return Convert.ToInt32(objDB.GetParameterValue(cmd, "@OutResoursceID"));
                     }
                     else
                     {
                         return -1;
                     }
                }
                catch (Exception ex)
                {


                    throw ex;
                } 

            }

System.ArgumentException was caught HResult=-2147024809
Message=Format of the initialization string does not conform to specification starting at index 0. Source=System.Data StackTrace: at System.Data.Common.DbConnectionOptions.GetKeyValuePair(String connectionString, Int32 currentPosition, StringBuilder buffer, Boolean useOdbcRules, String& keyname, String& keyvalue) at System.Data.Common.DbConnectionOptions.ParseInternal(Hashtable parsetable, String connectionString, Boolean buildChain, Hashtable synonyms, Boolean firstKey) at System.Data.Common.DbConnectionOptions..ctor(String connectionString, Hashtable synonyms, Boolean useOdbcRules) at System.Data.SqlClient.SqlConnectionString..ctor(String connectionString) at System.Data.SqlClient.SqlConnectionFactory.CreateConnectionOptions(String connectionString, DbConnectionOptions previous) at System.Data.ProviderBase.DbConnectionFactory.GetConnectionPoolGroup(DbConnectionPoolKey key, DbConnectionPoolGroupOptions poolOptions, DbConnectionOptions& userConnectionOptions) at System.Data.SqlClient.SqlConnection.ConnectionString_Set(DbConnectionPoolKey key) at System.Data.SqlClient.SqlConnection.set_ConnectionString(String value) at Microsoft.Practices.EnterpriseLibrary.Data.Database.CreateConnection() at Microsoft.Practices.EnterpriseLibrary.Data.Database.GetNewOpenConnection() at Microsoft.Practices.EnterpriseLibrary.Data.Database.GetWrappedConnection() at Microsoft.Practices.EnterpriseLibrary.Data.Database.GetOpenConnection() at Microsoft.Practices.EnterpriseLibrary.Data.Database.ExecuteNonQuery(DbCommand command) at Christoc.Modules.ResourceModule.App_Code.BOL.Resoursce.Insert(Resoursce r) in c:\inetpub\wwwroot\ideaPark\DesktopModules\ResourceModule\App_Code\BOL\Resoursce.cs:line 54 InnerException:

Answer

Fenton picture Fenton · Apr 25, 2013

This usually means your connection string isn't any good. If you look at the stack trace, you'll notice that this is failing when trying to interpret your connection string.

Check your connection string to make sure it is correct - or post it here for help (but without any sensitive information such as passwords ;) )

UPDATE

According to the SqlDatabase documentation the SqlDatabase class takes a connection string, not a key to the connection string configuration.

So

new SqlDatabase("SiteSqlServer");

Should be

var connection = ConfigurationManager.ConnectionStrings["SiteSqlServer"];

Database objDB = new SqlDatabase(connection.ConnectionString);

(I have omitted any defensive code here for brevity)