declare connection string in app.config in WPF

Last post 05-17-2007 1:14 AM by sureshsoft. 1 replies.

Sort Posts:

  • declare connection string in app.config in WPF

    05-05-2007, 9:40 PM
    • Member
      51 point Member
    • kpxkpx
    • Member since 09-01-2005, 10:26 PM
    • Posts 19

    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!

    Harry Sun
  • Re: declare connection string in app.config in WPF

    05-17-2007, 1:14 AM
    • Member
      2 point Member
    • sureshsoft
    • Member since 05-16-2007, 10:45 AM
    • Chennai
    • Posts 1

     Hi.. U just do it..

    1. open solution explorer-->Click Project Properties--> Click Security tab-->Tick 'Enable ClickOne Security settings'.
    2. Ensure you have selected 'This is a full trust application', or appropriate security settings.
    3. Recompile and run.
                 Luck...By Suresh. P Yes
    Suresh. P
    Filed under:
Page 1 of 1 (2 items)