0

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

2
  • I have other .Net apps hitting that server without any problems. Commented Jun 17, 2009 at 0:10
  • Double check your permissions and tables on the sql2005 server; your connection code works fine for me with sql 2000, 2005 and 2008. Commented Jun 17, 2009 at 0:14

2 Answers 2

3

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"

Sign up to request clarification or add additional context in comments.

1 Comment

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

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.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.