1

Just changed over to a SQL Server database and am trying to access my connectionstring set in my web.config. I am getting the following error:

The ConnectionString property has not been initialized.

Web.Config code:

<configuration>
  <configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <section name="EditorGenerator.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>    
  </configSections>
  <connectionStrings>
    <add name="loganWebConn" connectionString="Data Source=PMCUSSRV05;Integrated Security=true;Initial Catalog= loganWeb;User Id=logan_sys;Password=appPwd;" providerName="System.Data.SqlClient" />
  </connectionStrings>

C# code:

        SqlConnection con = new System.Data.SqlClient.SqlConnection();
        con.ConnectionString = ConfigurationManager.ConnectionStrings["loganWebConn"].ConnectionString;
        con.Open();
        try
        {
            string sql = "select j.*, u.fullname as plannerName from logan_jobs j left join on logan_user u on u.userID = j.plannerID ";
            sql += "where 0=0 ";
            sql += "and plannerID =" + planner + " ";
            sql += "and " + dept + " in (select deptID from logan_prodData p where p.jobID = j.jobID and curEstEndDate >= " + fromDate + " and curEstEndDate <= " + toDate + " ) ";
            sql += "order by jobNumber";

            SqlCommand cmd = new SqlCommand(sql, con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);

What am I doing wrong? I know this is really basic stuff here...

KMBonin
  • 111
  • 4
  • 12
  • 4
    Why bother with 0 = 0? It provides no benefit at all. The bigger concern though is the way you are creating your queries. You need to read about, understand and start using parameterized queries. This code is wide open to sql injection. – Sean Lange Oct 12 '16 at 20:01
  • 1
    Oh you also have some logical issues because you are not parameterizing your queries. You did not use single quotes around your dates so what will happen is the math will execute as either division or subtraction depending on the format of your strings. Those new integer values will then be implicitly converted to a date and it will NOT do what you think it is going to do. Last but not least, you should either just continue the same string or start using the StringBuilder class. – Sean Lange Oct 12 '16 at 20:04
  • 1
    So when you debug your code, what value does `con` have for the connection string after loading from the config file? – DeanOC Oct 12 '16 at 20:08
  • You have to learn how to use parameterized queries. Please review [Best Practices - Executing Sql Statements](http://stackoverflow.com/documentation/.net/3589/ado-net/14261/best-practices-executing-sql-statements). It will guide you in how to use parameters, why to use `using` statements to close connections, as well as other pointers. – Igor Oct 12 '16 at 20:18
  • Possible duplicate of [How to fix "The ConnectionString property has not been initialized"](http://stackoverflow.com/questions/1007786/how-to-fix-the-connectionstring-property-has-not-been-initialized) – Igor Oct 12 '16 at 20:24

2 Answers2

0

I believe that your problem is that you have specified Integrated Security=true, but you have also supplied a username and password.

If you really want to use Integrated Security, remove the username/password

It would also be worthwhile removing the space before the catalog name loganWeb.

This would make your connection string

<add name="loganWebConn" connectionString="Data Source=PMCUSSRV05;Initial Catalog=loganWeb;Integrated Security=true;" providerName="System.Data.SqlClient" />
DeanOC
  • 7,142
  • 6
  • 42
  • 56
  • Thanks for your reply. I removed the integrated security setting and kept the un/pw, but I am still throwing the same error: – KMBonin Oct 12 '16 at 20:46
  • Have you debugged your code and checked that the connection settings of the con object are what you expect? – DeanOC Oct 12 '16 at 20:55
0

Job Benefits

   <add key="SQLDatabaseConnectionString" value="Data Source=metin; Initial Catalog=firlama; User Id=12345; Password="/>
Metin Atalay
  • 1,375
  • 18
  • 28