Question

I have to migrate some Classic ASP pages to .NET. I've got the problem with ADODB connection that has been used in ASP App. Here is the code of old db.asp

<%
Option Explicit

' Declare variables...
Dim cnn  ' ADO connection
Dim rst  ' ADO recordset
Dim strTitle 'Title for each page

Sub OpenDatabase()  
    ' Create an ADO Connection.
    Set cnn = Server.CreateObject("ADODB.Connection")

    ' We're using SQL Server connection string
    cnn.Open Session("SQLConnectString")
    cnn.CommandTimeout = 0
    Server.ScriptTimeout = 3000

    ' Create an ADO Recordset object
    Set rst = Server.CreateObject("ADODB.Recordset")
End Sub

Sub RunSQL(strSQL)           
    'Open a recordset from the strSQL.
    rst.Open strSQL, cnn

End Sub

Sub CloseDatabase()  
    rst.Close
    Set rst = Nothing
    cnn.Close
    Set cnn = Nothing   
End Sub
%>

I want to use this code on every page for connection to DB. know that I have to remove Option Explicit from my code and add header as <%@ Page Language="VB" %> I've copied this code to the new aspx page and now I'm getting errors:

1) VS ask me to put End Sub before Sub OpenDatabase(), but there is no Open Sub that need to be closed.

2) VS don't see those variables cnn, rst, strTitle

3) Now I'm storing ConnectionString in Web.config, so I've replaced open with the following code:

cnn.Open(System.Configuration.ConfigurationManager.ConnectionStrings("SQLConnectString").ConnectionString)

What else should I change to fix it? Any advise=) Thanks

Was it helpful?

Solution

You do not use ADODB in DotNet. Technically, you can, but that's not the way to do.

You use ADO.Net, IDataReaders, DataSets (loose or strongly-typed, I prefer strongly-typed).

ASP.NET is not ASP.

Don't feel bad, I was trying the same thing you are (albeit, back in 2002). Until someone told me differently.

Here is a tutorial...probably at the right level for where you are now.

http://www.aspsnippets.com/Articles/Difference-between-ExecuteReader-ExecuteScalar-and-ExecuteNonQuery.aspx

OTHER TIPS

Rule #1 in NET: connection string better be in web.config or other config files. Or in some cases in OS registry.

Using connection string defined in each and every page in NET is bad practice from security, maintenance and lot of other reasons and on top of that it show low qualification of a programmer who build it.

Rule #2. You can use inline SQL statement but for the same reason as in rule #1 it is a bad idea. Use parametrized stored procedures unless you do not have any like while working with access or Excel or plain text files as data storage.

So in your web.config you should have following entry:

<connectionStrings>
    <add name="DBCS"
        connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|ProjectDatabases.mdf;Integrated Security=True;User Instance=True"
        providerName="System.Data.SqlClient" />
</connectionStrings>

then in your code you call

Public void main()
{
   String CONN
   String SQLString
 CONN = String.Format(ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString, rootPath);
 SQLString=/// your stored procedure and parameters if any
 SqlCommand cmd = new SqlCommand();
 cmd.CommandType = CommandType.StoredProcedure;
 cmd = new SqlCommand(SQLString), CONN);
 CONN.Open();
 SqlDataReader reader = cmd.ExecuteReader();

 /// do what ever you need to work with your data like build a string, html document etc
closeConn();
}

public void closeConn()
{
            if (reader != null)
            {
                reader.Close();
            }

           if (CONN!= null)
            {
                CONN.Close();
            }
}

You do not need Option Explicit for simple reason: C# will not allow you to use any undeclared variable

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top