Click here to Skip to main content
15,886,045 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi,

I am using the following code to connect SQL server 2012 DB using classic asp.

ASP
<%
	On error Resume Next
	Dim connectionstring1, connectionstring2
	Dim cn,cmd, rs 	
	
	connectionstring1 = "Provider=sqloledb;Data Source=KALPESH;Integrated Security=SSPI;Initial Catalog=Practice"
	connectionstring2 = "Provider=SQLNCLI11;Data Source=KALPESH;Integrated Security=SSPI;Initial Catalog=Practice"
	
	Set cn = Server.CreateObject("ADODB.Connection")
	Set cmd = Server.CreateObject("ADODB.Command")
	Set rs = Server.CreateObject("ADODB.Recordset")
	
	cn.ConnectionTimeout = 90
	cmd.CommandTimeout = 180
	query = "Select * from Users"
		
	cn.ConnectionString = connectinstring1  
	response.write  "<br>OLEDB connection : " & cn.ConnectionString 
	
	cn.Open
	cmd.ActiveConnection = cn
	cmd.CommandText = query
	Set rs = cmd.Execute
	
	If Err.number > 0 Then
		Response.Write("<br>" & Err.Number & "  - " & Err.Description)
		cn.close
	End If
	
	cn.ConnectionString = connectinstring2
	
	cn.Open
	cmd.ActiveConnection = cn
	cmd.CommandText = query
	Set rs = cmd.Execute

	If Err.number > 0 Then
		Response.Write("<br>" & Err.Number & "  - " & Err.Description)
		cn.close
	End If	
%>

I am receiving following errors with both the cases.
Output :
HTML
3709 - The connection cannot be used to perform this operation. It is either closed or invalid in this context.
3709 - The connection cannot be used to perform this operation. It is either closed or invalid in this context.

However, I am able to connect from excel - VBA to SQL server 2012 using following connectionstring.
HTML
objMyConn.ConnectionString = "Provider=sqloledb;Data Source=KALPESH;Integrated Security=SSPI;Initial Catalog=Practice"

I've searched the internet and checked my code against other examples. I am not sure why I am getting this error message. Any help would be greatly appreciated.
Posted
Updated 6-Oct-13 14:38pm
v2
Comments
Homero Rivera 6-Oct-13 15:03pm    
You're using OleDbConnection to SQl Server? You should probably use SqlConnection instead. OLE DB should be used for Access, Excel and stuff.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900