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

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:inetpubwwwrootideaParkDesktopModulesResourceModuleApp_CodeBOLResoursce.cs:line
54 InnerException:

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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)

Method 2

SqlConnection con = new SqlConnection(@”Data Source = (LocalDB)MSSQLLocalDB; AttachDbFilename=C:UsersKAMRAN RIYAZDesktopWeb Tech Lab PracticeASP.NETP21SQLoperationsP21SQLoperationsstu.mdf”);

Above connection string worked for me.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x