Invalid character in a Base-64 string for connecti

2019-03-04 22:39发布

问题:

I have a Sql Server 2008 R2 connection string that looks like

  <add name="DBConnectionString"
providerName="System.Data.SqlClient"
connectionString="Data Source=AYZ;Initial Catalog=AYZ;User ID=AYZ;Password=AYZ;"/>

When trying to create a new instance of SQLDatabase using EnterpriseLibrary.Data I get the above error.

connString =ConnectionStrings.ConnectionStrings["DBConnectionString"];
Database objDB = new SqlDatabase(connString);

connString has the following

Data Source=AYZ;Initial Catalog=AYZ;User ID=AYZ;Password=AYZ;

The detailed exception is as follows :

System.FormatException was unhandled by user code
  Message=Invalid character in a Base-64 string.
  Source=mscorlib
  StackTrace:
       at System.Convert.FromBase64String(String s)
       at Microsoft.Practices.EnterpriseLibrary.Data.Database.DecryptConnectionString(String strEncyptConStr)
       at Microsoft.Practices.EnterpriseLibrary.Data.Database..ctor(String connectionString, DbProviderFactory dbProviderFactory)
       at Microsoft.Practices.EnterpriseLibrary.Data.Sql.SqlDatabase..ctor(String connectionString)
       at ABC.GRC.DAL.CommonDAL..ctor() in D:\ABC\Projects\Right Price\ABC.GRC\ABC.GRC\ABC.GRC.DAL\CommonDAL.cs:line 31
       at ABC.GRC.BLL.CommonBLL..ctor() in D:\ABC\Projects\Right Price\Syntel.GRC\Syntel.GRC\Syntel.GRC.BLL\CommonBLL.cs:line 14
       at Global.Application_Error(Object sender, EventArgs e) in d:\ABC\Projects\Right Price\ABC.GRC\ABC.GRC\ABC.GRC.WEB\App_Code\Global.asax.cs:line 60
       at System.EventHandler.Invoke(Object sender, EventArgs e)
       at System.Web.HttpApplication.RaiseOnError()
  InnerException: 

回答1:

I think there is the problem

connString =ConnectionStrings.ConnectionStrings["ConnectionString"];

It should be

connString =ConnectionStrings.ConnectionStrings["DBConnectionString"];

Since you have key DBConnectionString in you configuration file not ConnectionString.

Useful link:

  1. Read connection string from web.config