Mr. Ant
Mr. Ant

Reputation: 700

sqlconnection is not defined

I am trying to update some code. I have a vb file that begins with this...

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

<script runat="server">

...and it is failing here...

Using oConn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("tps_write").ConnectionString())

The error it returns is...

"Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: BC30002: Type 'SqlConnection' is not defined."

Am I missing some system class?

EDIT: I updated the code to this...

Using oConn As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("tps_write").ConnectionString())

...and it accepts it. Why do I need to explicitly write out System.Data.SqlClient every time I use an object from that class???

Upvotes: 5

Views: 20455

Answers (5)

IT2428
IT2428

Reputation: 41

I came across this thread when searching for help with a similar issue of not being able to create an SQL connection in a vb.net .NET5 Windows Desktop Application. I was also getting the error of "sqlconnection is not defined".

My solution was to add a NuGet package called "System.Data.SqlClient" by Microsoft. You can open the NuGet Package manager by going to Tools > NuGet Package Manager... > Manage NuGet Packages for Solution...

Hopefully, this will help you or others looking for a solution.

Upvotes: 2

Mr. Ant
Mr. Ant

Reputation: 700

So, it turns out this line was the issue...

Using oConn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("tps_write").ConnectionString())

...instead of using "tps_write," I changed the permission to "tpsWrite." Apparently the tps_write is an outdated permission no longer used here at work. I wish the error messages were more clear. ;)

Thanks for everyone's help!

Upvotes: 1

Jason Towne
Jason Towne

Reputation: 8050

EDIT: I updated the code to this...

Using oConn As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("tps_write").ConnectionString())

...and it accepts it. Why do I need to explicitly write out System.Data.SqlClient every time I use an object from that class???

My best guess is that there's another class out there called SqlConnection and .NET doesn't know which type to use until you specify the System.Data.SqlClient.SqlConnection one explicitly.

Upvotes: 2

coolblaze03
coolblaze03

Reputation: 51

Make sure you have specified your connection string in the web.config. You should see a tag in there like this

<connectionStrings>
<--! Below is your connection string>
<add name="ConnName" connectionString="Data Source=PROGRAMMER2\SQLServer;Initial Catalog=PPSSecurity;Integrated Security=true;" />
</connectionStrings>

Upvotes: 0

Jeff
Jeff

Reputation: 14279

If your project is a web application, you might be missing a reference to System.Data. Right click the project in Solution Explorer, and go to Add References. On the .NET tab, select System.Data.dll and click OK.

Upvotes: 0

Related Questions