locked
SqlConnection statement returns "not found in namespace" error? RRS feed

  • Question

  • Hi, 

    So I'm trying to establish an SqlConnection to my database in Visual Studio 17 but the SqlConnection (as mentioned in the title) is returning a "could not be found in namespace" error.

    I've written: 'using System.Data' and 'using System.Data.SqlClient' below 'using System' like so:

    using System;
    using System.Data;
    using System.Data.SqlClient;
     

    In addition I've tried writing out System.Data.SqlClient.SqlConnection on the actual line of code to no avail. It's not a referencing error because I've also linked the System.Data.dll file in (Add>Reference).

    What gives? This is super weird I haven't been able to find a solution online. I've even searched on google how to spell Connection just to make sure I'm not getting it wrong. 



    • Edited by james9oo0 Tuesday, February 20, 2018 2:58 PM
    • Moved by KareninstructorMVP Thursday, February 22, 2018 10:41 AM Moved from C# forum
    Tuesday, February 20, 2018 2:52 PM

Answers

  • Yes I did

    It's okay I just got it working! It turns out the 'New Project' template I used wasn't allowing the use of SQLConnection. 

    Thanks for everyone's time!

    Thursday, February 22, 2018 1:15 PM

All replies

  • delete the references from the project and put them back....
    Tuesday, February 20, 2018 3:45 PM
  • Hello,

    Did you have success with DA924x suggestion?


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, February 20, 2018 10:12 PM
  • Sorry for the wait,

    I just tried that, still nothing. Additionally, I just tried using the Framework (64x) version of data.dll and still hasn't worked.

    Update: I've also tried downloading the System.Data.dll file from another source to check if the original is corrupted, but I'm still not getting anything. 

    Update 2: Okay I'm really at a loss, I just tried making a new project still nothing. I'm going to post my code below just in case I've done something wrong, maybe you can pick it out? 

    using System;
    using System.Data;
    using System.Data.SqlClient;
    
    
    
    class DatabasePractice
    {
        public void DatabaseMain()
        {
    
        }
    
        public string Name_Cell()
        {
            SqlConnection conn = new SqlConnection
            ("Data Source=(PARSALAPTOP); Initial Catalogue=Practice; UserID=sa; Password");
    
        }
    }
    The code isn't even complex in the slightest, the SqlConnection is returning the error namespace not found. 


    • Edited by james9oo0 Wednesday, February 21, 2018 7:53 AM
    Wednesday, February 21, 2018 6:49 AM
  • Have you tried using the Visual studio assembly browser on the system.data.dll? Just to see if you can see the classes you expect to see in there?
    Wednesday, February 21, 2018 8:06 AM
  • How can I do that? 
    Wednesday, February 21, 2018 8:46 AM
  • I only have Visual Studio 2015, not 2017, so it may be different. But in Visual Studio:

    Expand the References folder in the Solution Explorer.

    Right-click on System.Data and click "View in Assembly Explorer"

    You should get into a new Assembly Explorer tab.

    From there you can drill down into the namespaces and see the classes. In the System.Data.SqlClient namespace you should find the SqlConnection class (you should even be able to double-click this to see a decompiled version of the source).


    • Edited by RJP1973 Wednesday, February 21, 2018 8:51 AM
    Wednesday, February 21, 2018 8:50 AM
  • Lmao it doesn't even have a references folder, it's called Assemblies, but either way I right clicked on the assembly and multiple different places and can't seem to find an Assembly Explorer or anything along those lines. 
    Wednesday, February 21, 2018 9:07 AM
  • Bah, just realised the Assembly Explorer comes from ReSharper (I think). Sorry.

    Do you have the object browser? That can also be used to drill down into assemblies and at least see the namespaces and classes in there.

    Wednesday, February 21, 2018 9:20 AM
  • Haha yeah no problem.

    So the only thing SQL related is SqlDBType which containes an enum base type? I'm still new to this so I don't really understand.

    What I can gather however is that SQLClient doesn't even exist? On top of that, I can't find SQLConnection anywhere though I'm not sure if it's a class or method.

    How is it that I'm not getting a namespace error for the SQLClient?

    Update: Oh snap, okay the actual System.Data assembly doesn't have anything inside of it. But the System.Data.Common does. However this shouldn't effect anything right? You're accessing the entirety of the System.Data assembly so this should include System.Data.Common.

    Either way the SQL connection is not in the System.Data.Common assembly neither.

    • Edited by james9oo0 Wednesday, February 21, 2018 9:52 AM
    Wednesday, February 21, 2018 9:43 AM
  • Just for comparison, this is what I see:

    Wednesday, February 21, 2018 12:11 PM
  • Bah, just realised the Assembly Explorer comes from ReSharper (I think). Sorry.

    Do you have the object browser? That can also be used to drill down into assemblies and at least see the namespaces and classes in there.

    Yes it is

    Reference tab in VS2017


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, February 21, 2018 12:32 PM
  • Looks good


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, February 21, 2018 12:36 PM
  • Have you tried to create a new project to see if the problem persist in the new project or is it simply the one project.

    Have you tried compiling for 32 bit ?


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, February 21, 2018 12:38 PM
  • @KarenPayne Yes, and yes

    @RJP973

    The images are not what I'm seeing.

    Apparently, I can't post images, so I'm just going to tell you. My System.Data doesn't include any classes, but System.Data.Common includes a System.Data class, System.Data.Common, and System.Data.SqlTypes class. Otherwise I have nothing else that's under the System.Data assembly.

    Okay seems like I'll just need to reinstall the .NET framework?

    Shouldn't me having gotten the dll file from another source made it work?

    UPDATE: Can't reinstall .NET framework because I have the Windows 10 Creators update which includes the latest version of the .NET framework as part of the OS

    UPDATE 2: Okay I've made things a lot worse, I uninstalled all the .NET things in control panel, and now I don't have default access to the system directory. lol should've waited for you guys to respond.

    UPDATE 3: Alright after running repair on Visual Studio Installer. I went back and tried referencing System.Data.dll again and it's still not working.

    UPDATE 4: I referenced all the System.Data dll files in the .NET folder and it still doesn't work.

    WHAT IS GOING ON.

    • Edited by james9oo0 Thursday, February 22, 2018 9:10 AM
    Thursday, February 22, 2018 6:27 AM
  • So you are say you used the browse button to add a reference as follows

    Traversed to C:\Windows\Microsoft.NET\Framework\v4.0.30319 and picked System.Data.dll ?

    Perhaps the best thought here is to move your question to the Visual Studio forum which I will do now as technically this is not a C# issue.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, February 22, 2018 10:41 AM
  • Yes I did

    It's okay I just got it working! It turns out the 'New Project' template I used wasn't allowing the use of SQLConnection. 

    Thanks for everyone's time!

    Thursday, February 22, 2018 1:15 PM
  • The first thing I do when I get your kind of message is 

    Project -> Project Properties -> Application and look what the default namespace is. 


    Success
    Cor

    Thursday, February 22, 2018 1:23 PM
  • Yes I did

    It's okay I just got it working! It turns out the 'New Project' template I used wasn't allowing the use of SQLConnection. 

    Thanks for everyone's time!

    Goes back to me asking for you to try a new project. Be careful how you select project as there are several options. If you see dependencies in the project folder it's not the one you want for classic desktop projects.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by james9oo0 Tuesday, February 27, 2018 6:24 AM
    • Unmarked as answer by james9oo0 Tuesday, February 27, 2018 6:24 AM
    Monday, February 26, 2018 2:03 AM
  • Hi james9oo0,

    Not get your response for several days, if you mean that this issue has been resolved, you could mark helpful replies as the answers, so it could help other community members who get the same issue, and I could close this case for you.

    Thanks,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, February 27, 2018 5:23 AM