Avatar of Gary Croxford
Gary Croxford
Flag for United Kingdom of Great Britain and Northern Ireland asked on

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

Hello

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
 
		objConn.Open
		objReader = objCmd.ExecuteReader
 
		while objReader.Read
			Response.write(objReader.GetString(0) & "<br>")
		end while 
		
		objConn.Close
	end sub
</script>
 
 
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:

Open in new window

ASP.NETDatabases

Avatar of undefined
Last Comment
Gary Croxford

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
RobertNZana

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
RobertNZana

Gary Croxford

ASKER
Thank you
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23