troubleshooting Question

Trying to Connect to SQl Server database - SqlConnection Not Defined Error

Avatar of Gary Croxford
Gary CroxfordFlag for United Kingdom of Great Britain and Northern Ireland asked on
3 Comments1 Solution221 ViewsLast Modified:

Thank you for looking at my question. I am trying to build a web-based system to interrogate database tables on our SQL server.

I am falling at the first hurdle - I can't connect to the database and don't know what I'm doing wrong. I have attached the code I am using and would appreciate somebody pointing out my error.

Thank you in advance for your help,
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.Odbc" %>
<script runat="server">
	sub Page_Load(obj as object, e as eventargs)
		dim objConn as new SqlConnection("user id=username; password=password; data source=ServerName; initial catalog=DatabaseName;")
		dim objCmd as new SqlCommand ("SELECT ColumnName FROM TableName", objConn)
		dim objReader as SqlDataReader
		objReader = objCmd.ExecuteReader
		while objReader.Read
			Response.write(objReader.GetString(0) & "<br>")
		end while 
	end sub
Error Message:
Compiler Error Message: BC30002: Type 'SqlConnection' is not defined.
Source Error:
Line 6:  	sub Page_Load(obj as object, e as eventargs)
Line 7:  
Line 8:  		dim objConn as new SqlConnection("user id=anodising; password=bugger; data source=SCLServer; initial catalog=SCLAnodising;")
Line 9:  		dim objCmd as new SqlCommand ("SELECT CustStockCode FROM ArCustStkXref", objConn)
Line 10:

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 3 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros