locked
Why can’t any references to my SQL database be found? RRS feed

  • Question

  • User-696641838 posted

    Errors occur everywhere there is an attempt to access a database table, such as with “TextTable texttable = db.TextTables.SingleOrDefault(p => p.TexID == TextID);” which state: Error 16 'System.Data.Linq.Table<TransLangTable>' does not contain a definition for 'SingleOrDefault'.  Errors also occur whenever there is an attempt to access the database directly such as with “SqlConnection conn = null;” which state: Error 52 The type or namespace name 'SqlConnection' could not be found.

     

    Obviously, I included a “using System.Data.SqlClient;” statement in the code section along with many others.  There is also a statement of: “<add name="ENCdbConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ENCdb.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/> within the connectionStrings portion of the web.config file.  In addition, there is, “string ENCconn = ConfigurationManager.ConnectionStrings["ENCdbConnectionString"].ToString();” at the start of the script portion of the code.

     

    I believe that the problem is due to the fact that I only copied the database and DataClasses from another website into my current website instead of creating them directly.  For some reason, the Debug function cannot relate to any of my code that relates to the database.  I even attempted to copy the code into a new web page file within the other website where all other web pages work fine with the database.  However, I obtained the exact same results with this file when I attempted to run it.  What am I doing wrong?  Please help.

     

    Maurice

    Wednesday, October 7, 2015 8:16 PM

Answers

  • User614698185 posted

    Based on your code, you modified like below:

    In Web.config file:

    <add name="ENCdbConnectionString" connectionString="Data Source==.\SQLEXPRESS; AttachDbFilename=|DataDirectory|\ENCdb.mdf;
    Initial Catalog=ENCdb;Integrated Security=True" providerName="System.Data.SqlClient" />

    On .aspx.cs page:

    string ENCconn = ConfigurationManager.ConnectionStrings["ENCdbConnectionString"].ConnectionString;
    SqlConnection con = new SqlConnection(ENCconn);

    Best Regards,

    Candice Zhou

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 7, 2015 11:28 PM
  • User614698185 posted

    Hi Maurice Osborn,

    My Visual Studio version works fine with all other websites that interface with an SQL database. 

    Based on your description, it is hard to say the root cause of your issue, but I suggest you creating new project and restoring your original project. If your environment is normal, the reference issue should not be existing.

    Best Regards,

    Candice Zhou

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, October 10, 2015 11:29 AM

All replies

  • User614698185 posted

    Based on your code, you modified like below:

    In Web.config file:

    <add name="ENCdbConnectionString" connectionString="Data Source==.\SQLEXPRESS; AttachDbFilename=|DataDirectory|\ENCdb.mdf;
    Initial Catalog=ENCdb;Integrated Security=True" providerName="System.Data.SqlClient" />

    On .aspx.cs page:

    string ENCconn = ConfigurationManager.ConnectionStrings["ENCdbConnectionString"].ConnectionString;
    SqlConnection con = new SqlConnection(ENCconn);

    Best Regards,

    Candice Zhou

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 7, 2015 11:28 PM
  • User-696641838 posted

    Thank you for your response.  The replacement of your new code in the web.config file appears to work just fine, but the replacement of your code in my aspx.cs page is causing an error that states: Error 17 The type or namespace name 'SqlConnection' could not be found.  Strangely, when I tried to correct this error by removing the portion: “SqlConnection con = new SqlConnection(ENCconn);” it caused the errors to reappear everywhere that there was an attempt to access a database table.  This is just what occurred previously without your code.  Obviously, this appears to be the key to my problem.  Please help.

     Maurice

    Thursday, October 8, 2015 3:20 PM
  • User614698185 posted

    Hi Maurice Osborn,

    when I tried to correct this error by removing the portion: “SqlConnection con = new SqlConnection(ENCconn);” it caused the errors to reappear everywhere

    I think you could use Ctrl+F, and then choose Entire Solution to search SqlConnection. You should make sure all the classes which have SqlConnection have added reference: “using System.Data.SqlClient;” .

    If all the classes have added reference, you should check out your Visual Studio version and environment.

    Best Regards,

    Candice Zhou

    Friday, October 9, 2015 8:11 AM
  • User-696641838 posted

    I performed the search in the Entire Solution for SqlConnection, which found everywhere that this was used within my code for the web page.  I’m not sure what you mean by “You should make sure all the classes which have SqlConnection have added reference: “using System.Data.SqlClient;”.  The “using System.Data.SqlClient;” statement is certainly included at the top of every web page code module.   My Visual Studio version works fine with all other websites that interface with an SQL database.  Am I missing anything else?

     Maurice

    Friday, October 9, 2015 1:39 PM
  • User614698185 posted

    Hi Maurice Osborn,

    My Visual Studio version works fine with all other websites that interface with an SQL database. 

    Based on your description, it is hard to say the root cause of your issue, but I suggest you creating new project and restoring your original project. If your environment is normal, the reference issue should not be existing.

    Best Regards,

    Candice Zhou

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, October 10, 2015 11:29 AM
  • User-696641838 posted

    To all of those who have provided so much help to me,

     

    I apologize.  I have just discovered that I have been specifying “using System.Data; using System.Data.SqlClient; and using System.Linq; instead of including the following import statements to enable a connection with my database. 

     

    <%@ Import Namespace="System.Linq" %>

    <%@ Import Namespace="System.Data" %>

    <%@ Import Namespace="System.Data.SqlClient" %>

     

    All is working well now.  I am sorry for troubling you with my problem when the solution was so simple.  But, I appreciate your efforts.

     

    Maurice

    Thursday, October 22, 2015 4:45 PM