views:

194

answers:

3

Using the connection string below I can connect to a SQL2000 DB but not a SQL2005. I have the code in an ASP file.

Dim connStr, cn, rs, sql
connStr = "Provider=SQLOLEDB;Persist Security Info=True" _
           & ";Initial Catalog=mydatabase"  _
           & ";User ID=dbuser" _
           & ";Password=dbpwd" _
           & ";Data Source=servername" 
sql = "SELECT TOP 1 [Column1] FROM [dbo].[MyTable] order by NEWID()"
Set cn = Server.CreateObject("ADODB.Connection")
cn.Open cn

set rs= server.CreateObject("ADODB.Recordset")
 rs.CursorLocation=3
 rs.Open sql,cn,3,4

 if not rs.EOF then
       Response.Write("<b>Column1: " & rs("Column1") & "</b><br />")
 end if
set rs.ActiveConnection= nothing    
 rs.Close
 set rs= nothing
if ucase(TypeName(cn)) = "CONNECTION" then
 cn.Close
 Set cn = Nothing
end if

I have even tired with SQLOLEDB.1 Sql login is enabled on the sql server.

Error: The connection cannot be used to perform this operation. It is either closed or invalid in this context. Happens on rs.Open sql,cn,3,4

A: 

Not a specific answer sorry, but this is a great site for all-things-connection-strings...

Seba Illingworth
Thanks, I have looked at that and several other sites. I have several years of ASP experience and have never encountered anything like this before.
Picflight
A: 

What is the error? SQL Server 2005/8 install with remote connections disabled -- check this support article.

I see, try setting your connString on your Connection object (you use conn instead of connStr). Uee option explicit to avoid these errors.

JP Alioto
I updated the post with the error. The SQL 2005 is configured for remote connections. I have other .Net apps hitting that server without any problems.
Picflight
+3  A: 

It happens to everybody sometime:

Dim connStr, cn, rs, sql
connStr = "Provider=SQLOLEDB;Persist Security Info=True" _
           & ";Initial Catalog=mydatabase"  _
           & ";User ID=dbuser" _
           & ";Password=dbpwd" _
           & ";Data Source=servername" 
sql = "SELECT TOP 1 [Column1] FROM [dbo].[MyTable] order by NEWID()"
Set cn = Server.CreateObject("ADODB.Connection")
cn.Open connStr

You are calling a variable conn as connection string but you have declared and filled connStr

Change "cn.Open conn" with "cn.Open connStr"

backslash17
And always use "Option Explicit" to prevent this in the future.
RBarryYoung