Results 1 to 8 of 8

Thread: Errors with Imports, .Net 2019 (Just getting started)

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2021
    Posts
    10

    Errors with Imports, .Net 2019 (Just getting started)

    I am trying to get coding in .net. I used to use VB 6, which was much simpler, and have mostly coded in VBA (MS Access) for the past 10 years.

    I'm having trouble with this code set:

    Imports System.Data
    Imports System.Data.SqlClient
    Imports System.Data.SqlTypes
    Imports System.Data.Sql

    Public Class Form1
    Private Sub btnCustomer_Click(sender As Object, e As EventArgs) Handles btnCustomer.Click
    Dim myConn As SqlConnection
    'Dim myCmd As SqlCommand
    'Dim myReader As SqlDataReader
    'Dim results As String
    End Sub
    End Class

    I'm getting the following errors. It's like it's not seeing my Imports, even though they're right above the Form declaration. If I try to put it inside the form, it tells me it can't be there.

    >------ Build started: Project: EnterpriseLink, Configuration: Debug Any CPU ------
    1>C:\Util\Projects\EnterpriseLink\Form1.vb(7,23): error BC30002: Type 'SqlConnection' is not defined.
    1>C:\Util\Projects\EnterpriseLink\Form1.vb(7,13): warning BC42024: Unused local variable: 'myConn'.
    1>Done building project "EnterpriseLink.vbproj" -- FAILED.
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

  2. #2
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,474

    Re: Errors with Imports, .Net 2019 (Just getting started)

    Quote Originally Posted by Rando1000 View Post
    I am trying to get coding in .net. I used to use VB 6, which was much simpler, and have mostly coded in VBA (MS Access) for the past 10 years.

    I'm having trouble with this code set:

    Imports System.Data
    Imports System.Data.SqlClient
    Imports System.Data.SqlTypes
    Imports System.Data.Sql

    Public Class Form1
    Private Sub btnCustomer_Click(sender As Object, e As EventArgs) Handles btnCustomer.Click
    Dim myConn As SqlConnection
    'Dim myCmd As SqlCommand
    'Dim myReader As SqlDataReader
    'Dim results As String
    End Sub
    End Class

    I'm getting the following errors. It's like it's not seeing my Imports, even though they're right above the Form declaration. If I try to put it inside the form, it tells me it can't be there.

    >------ Build started: Project: EnterpriseLink, Configuration: Debug Any CPU ------
    1>C:\Util\Projects\EnterpriseLink\Form1.vb(7,23): error BC30002: Type 'SqlConnection' is not defined.
    1>C:\Util\Projects\EnterpriseLink\Form1.vb(7,13): warning BC42024: Unused local variable: 'myConn'.
    1>Done building project "EnterpriseLink.vbproj" -- FAILED.
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    Have you also added a reference to the System.Data.SqlClient libraries?

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2021
    Posts
    10

    Re: Errors with Imports, .Net 2019 (Just getting started)

    Quote Originally Posted by PlausiblyDamp View Post
    Have you also added a reference to the System.Data.SqlClient libraries?
    In the references section of the project's properties, I've checked "System.Data.Common", "System.Data.SqlClient" and "System.Data.SqlTypes"
    Name:  References.jpg
Views: 106
Size:  17.9 KB

  4. #4

    Thread Starter
    New Member
    Join Date
    Nov 2021
    Posts
    10

    Re: Errors with Imports, .Net 2019 (Just getting started)

    I don't know if it's relevant, but my Imports statements are washed out, not normal text like the rest of the code. It doesn't look like that on tutorials. Also, I tried to add a COM reference to System (2.4 and 2.0) and it gives me <The system cannot find the reference specified>. Not sure if that's the same thing.

  5. #5
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,398

    Re: Errors with Imports, .Net 2019 (Just getting started)


  6. #6

    Thread Starter
    New Member
    Join Date
    Nov 2021
    Posts
    10

    Re: Errors with Imports, .Net 2019 (Just getting started)

    From the article
    That is a feature of newer versions of VS: when you have an Imports statement but aren't using anything from that namespace, it darkens the text of that Imports statement to indicate it's not being used.
    But WHY is it not being used? I'm Dim-ing a SqlConnection, which should be in System.Data.SqlClient. I'm trying to use the Import, but it's telling me SqlConnection isn't found.

  7. #7
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,398

    Re: Errors with Imports, .Net 2019 (Just getting started)


  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Errors with Imports, .Net 2019 (Just getting started)

    Quote Originally Posted by Rando1000 View Post
    In the references section of the project's properties, I've checked "System.Data.Common", "System.Data.SqlClient" and "System.Data.SqlTypes"
    Name:  References.jpg
Views: 106
Size:  17.9 KB
    No you haven't. Actually look at your own screenshot. You haven't added anything in the References section. You've checked the boxes to import namespaces but that is useless if you haven't referenced a library that contains the declaration of a type that is a member of that namespace. You were specifically asked whether you referenced the System.Data.SqlClient library and the answer is "no". If you don't reference that library then your project doesn't know that any types declared in that library exist, which is exactly why you're being told that the SqlConnection type doesn't exist: because it doesn't, as far as your project is concerned.

    Start by following the Blog link in my signature below and read my post on Assemblies & Namespaces. Once you have done that and hopefully understand the principles, then add the appropriate reference to your project.

    That said, you probably won't be adding a reference directly. You would generally do that for .NET Framework projects but, if this was a .NET Framework project, you'd almost certainly already have the required reference, i.e. System.Data.dll. You have most likely created a .NET 5 project, which you can confirm on the Application page of the project properties. In that case, you add references by installing NuGet packages into your project. In that case, you can right-click the project in the Solution Explorer and select Manage NuGet Packages to open the NuGet GUI. You can then select the Browse tab and enter the name of the desired package, i.e. System.Data.SqlClient. Select the appropriate package and install it. If you then go back to the References page of the project properties, you should see that the appropriate reference has been added and your namespace import will then actually mean something.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width