Link to home
Start Free TrialLog 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
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Gary Croxford

ASKER

Thank you