Link to home
Create AccountLog in
Avatar of Gary Croxford
Gary CroxfordFlag 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

ASKER CERTIFIED SOLUTION
Avatar of RobertNZana
RobertNZana
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of Gary Croxford

ASKER

Thank you