Dear all,
Here is my question. I am trying to build up a object data source that will to be bound to xaml object in WPF. After I declare a connectionString in app.config of object data source project, the connectionString can not be found and show the error below:
"Object reference not set to an instance of an object."
app.config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="Connection" connectionString="Data Source=***;Initial Catalog=***;User ID=***;Password=***" providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>
cs code:
string _connectionString =ConfigurationManager.ConnectionStrings["Connection"].ConnectionString;
Can any one tell me what I did wrong? Many thanks.
______________________________________________________________________________________
After that, I though that it may be work if use connection string in code, but I got new error as below:
"Request for the permission of type 'System.Data.SqlClient.SqlClientPermission, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed."
CS:
string _connectionString="Data Source=***;Initial Catalog=***;User ID=***;Password=***" ;
SqlClientPermission permission = new SqlClientPermission(PermissionState.None);
permission.Add(_connectionString, "", KeyRestrictionBehavior.AllowOnly);
permission.PermitOnly();
string sqlCmd = "select * from OBJ_WORK_ITEMS where ITEM_ID=38";
SqlConnection conn = new SqlConnection(_connectionString);
SqlDataAdapter da = new SqlDataAdapter(sqlCmd, conn);
DataSet ds = new DataSet();
try
{
conn.Open();
da.Fill(ds, "OBJ_WORK_ITEMS");
}
catch (Exception ee)
{
throw new Exception("Something wrong in da.fill()" + ee);
}
finally
{
conn.Close();
}
You can see I tried to modify SqlClientPermission to make it work, but no luck. Please help me!