Error  while compiling a class.

theresamary
theresamary used Ask the Experts™
on
Hai ,
I created a namespace with a class.I am importing system.data and system.data.sqlclient .but when I tried to compile it,its giving me a error as 'sqlconnection is not defined'.I am not able to import any system namespaces like system.web or system.web.UI  .Please help me.
Thanks.
Theresa Mary
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
The reason you are getting this error is simple. for example..

class simple
  dim cn as sqlconnection
end class

This would throw up the problem you have as the compiler would not be able to make sense of sqlconnection. Fully qualifying the decaration of cn or importing the namespace would solve the problem.

imports system.data.sqlclient
class simple
  dim cn as sqlconnection
end class

OR

class simple
  dim cn as system.data.sqlclient.sqlconnection
end class

You say you have imported this namespace BUT you say you cannot import any system namespaces. Why not? If you definately can't then why do you think this import statement has worked? I presume it has as the error would be different. This means that you must have some syntactical error in you code. You probably have not instantiated the connection object.

imports system.data.sqlclient
class simple
  dim cn as sqlconnection
  cn = new sqlconnection()
end class

Hope this helps.

Author

Commented:
hai
   I have done already what u said.this is my code.I saved this as DataObj.vb   .
**********************************************************

Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Reflection

Namespace DataLayer

    Public Class DataObjVB
    private _connstr as string
        Public Sub New()
            mybase.new()
            _connstr=""
           
        End Sub
       
        Public Sub New(connstr as string)
            _connstr=connstr
           
        End Sub
       
        public property connectionstring as string
        get
            return _connstr
        end get
        set
            _connstr=value
        end set
       
        public function getdepartments() as dataview
            dim con as SqlConnection
            con=new SqlConnection(_connstr)
            dim mycommand as sqldataadapter
            mycommand=new sqldataadapter("select deptname from department",con)
            dim ds as dataset
            ds=new dataset()
            mycommand.fill(ds,"depts")
            return ds.tables("depts").defaultview
       
       
        end function
       
        public function getemployeesfordepartment(deptname as string) as dataview
            dim con as sqlconnection
            con=new SqlConnection(_connstr)
            dim mycommand as sqldataadapter
            mycommand=new sqldataadapter("select e.empno,e.empname,d.deptname from employee e,depatment d where e.deptno=d.deptno and e.deptname='" & deptname & "'",con)
            dim ds as dataset
            ds=new dataset()
            mycommand.fill(ds,"emp")
            return ds.tables("emp").defaultview
       
       
        end function
    End Class
End Namespace

***********************************************************



When I compile it,I am getting errors as

Namespace or type 'Data' for the Imports system.data cannot be found
Namespace or type 'SqlClient' for the Imports system.data cannot be found.

Error number for both is BC30466


Type Sqlconnection is not defined.Error number is BC30002

same error for sqldataadapter,dataset.


In another class i imported "system.web" .Again it is throwing the same error.I am using Web Matrix editor.

Thanks





Commented:
I never used Web Matrix but I did get the same kind of messages a while ago. All I had to do was reference the System.Web namespace. Within VS.net IDE I went to references and added the System.Web namespace. I don't know the equivalent in Web matrix.

CJ
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

Commented:
Hold on, let me try this for myself...

Commented:
For starters the property "connectionstring" has no "End Property" Doh! Try it now!

Also I do not beleive that your computer will have a machine.config (search for it). To be able to import an assembly the compiler needs to be able to find it. It obviously cannot! First it looks in the bin directory for the project and then Global Assembly Cache. However, projects usually inherit settings from machine.config that are overridden in the web.config for the project. In machine.config are these things

<compilation>
<assemblies>
<add assembly="MyFirstControl, ..." />
</assemblies>
</compilation>

that tell the project what to refernce by default. You will either need to add these entries into the web.config for your project OR (and this is what I recommend) copy the appropriate dlls into the bin directory for your project.

Author

Commented:
Hai,
   My system has a machine.config .i created a web.config
file and added a assembly which i compiled already(but that class does not import system.data or system.data.sqlclient).I have created a simple .aspx file importing system.data and system.data.sqlclient.It works fine.So when compiling manually a class only i am getting this error.Please help me.


Thanks

Author

Commented:
Hai,
   My system has a machine.config .i created a web.config
file and added a assembly which i compiled already(but that class does not import system.data or system.data.sqlclient).I have created a simple .aspx file importing system.data and system.data.sqlclient.It works fine.So when compiling manually a class only i am getting this error.Please help me.


Thanks

Author

Commented:
Hai,
   My system has a machine.config .i created a web.config
file and added a assembly which i compiled already(but that class does not import system.data or system.data.sqlclient).I have created a simple .aspx file importing system.data and system.data.sqlclient.It works fine.So when compiling manually a class only i am getting this error.Please help me.


Thanks

Commented:
Have you included the reference to the namespace in the reference tab... .Net needs to reference to be implicit for the imports statement to work.

In the solution explorer, right click on references
 click add Reference

Scroll down the list and select system.data

>>> and select system.web for the web imports

Maybe this will help

Dreffed

Author

Commented:
hai,
  I am using webmatrix editor.How to add the reference?
Thanks

Author

Commented:
hai,
  I am using webmatrix editor.How to add the reference?
Thanks

Commented:
Try usibg the sdk compiler to compile this class. It should be able to resolve the references for you.

Click  Start > Run > Type "Command", Hit enter

Type: "vbc /out:DataObj.dll /t:library DataObj.vb", Hit Enter


The Visual Basic Compiler for .net can be found in

c:/windows/microsoft.net/framework/<version>

the version will probably be v1.0.3705

Author

Commented:
hai,
  I found out a compile file in the framework.When i click on that it opening a screen like a dos prompt and suddenly getting disappeared.I dont know why its not running.
Thanks

Commented:
You dont double click it to run it. Use the instructions posted previously to use it.

Open a dos prompt manually and type the command:

vbc /out:DataObj.dll /t:library DataObj.vb

Author

Commented:
Yes I was compiling like that only.The error statement i got was in the DOS prompt only.

Commented:
And the error was?

Author

Commented:
This is my command

C:\vbc /t:library /out:D:\dotnet\bin\DataObjVB.dll D:\dotnet\DataObj.vb


Error :


D:\dotnet\DataObj.vb(5) : error BC30466 : Namespace or type 'Data' for the Imports
'System.Data' cannot be found.


Same  error statement 'System.Data.SqlClient'
Most Valuable Expert 2012
Top Expert 2008

Commented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

PAQ/Refund

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

TheLearnedOne
EE Cleanup Volunteer
PAQed, with points refunded (100)

Computer101
E-E Admin

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial