Classic ASP DB opening function does not work - asp-classic

I have transferred a Classic asp site running on windows server 2003 to windows server 2008 but suddenly the below code has stopped working.
Const connStr_FC08 = "Provider=SQLNCLI10;Server=DS-47500;Database=TestDB;Uid=TestLogin;Pwd=test;Network=dbmssocn;"
Function connDB(OpenDB)
DIM conn
SET conn = Server.CreateObject("ADODB.Connection")
conn.open = connStr_FC08
If OpenDB = "Y" Then conn.open
connDB = conn
End Function
dim cn, cmd
cn = connDB("Y")
response.Write(cn.state)
This returns the below error
Microsoft VBScript runtime error '800a01a8'
Object required: 'Provider=SQLNCLI10.1'
This happens on the below line
response.write(cn.state)
Thanks
Chris

I see a few possible syntax issues with the code you posted:
...
conn.open = connStr_FC08
...
connDB = conn
...
cn = connDB("Y")
Should it be updated to the following?
...
conn.ConnectionString = connStr_FC08
...
Set connDB = conn
...
Set cn = connDB("Y")

You have that SQL Provider installed right?
You can put that function into a simple VBScript script to test without altering your pages any.

If I take opening of the connection out of the function and put in inline then there is no error and it works.
But my whole site works by using this function so a) I dont want to have to rewrite my code and b) I dont understand why this does not work when it used to.

Related

Apache Ignite ODBC and ADODB Recordset problem fetching data

i try to fetch records from Apache Ignite inmemory database via Microsoft Access 2016 32-Bit and Apache Ignite 32-Bit ODBC-driver with default settings. OS is Windows 10.
Import as a linked table does not work so i tried via ADODB-Class.
Connection.Open and Recordset.Open works and i can see all columns (ID and NAME) of sample table CITY in the Recordset. But when i try to fetch the first record with MoveFirst or MoveNext, i get the error 'specified attribute is not supported'. I tried the same with CursorLocation=adUseClient and the error message changes to 'wrong parameter'. Default Provider is MSDASQL.1. Is this the correct Provider? Any idea how to fetch records with ADODB?
Code
`
Public Sub QueryIgnite()
Dim ADOrs As ADODB.Recordset
Dim ADOcon As ADODB.Connection
Set ADOcon = New ADODB.Connection
ADOcon.ConnectionString = "DSN=Apache-Ignite-DSN"
'ADOcon.CursorLocation = adUseClient
ADOcon.Open
Set ADOrs = New ADODB.Recordset
ADOrs.Open "select * from city", ADOcon, adOpenForwardOnly
ADOrs.MoveNext
Debug.Print ADOrs.Fields("NAME")
ADOrs.Close
ADOcon.Close
End Sub
`
Thank you in advance.
Regards.
Guido Clesius
A ticket on apache.org is created to fix the bug. See https://issues.apache.org/jira/browse/IGNITE-18210

Migrating ADODB connection from ASP to ASPX

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
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
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

Windows command to view all DSNs?

I have some VB code that connects to a DSN (data source) setup on this Windows machine. The code looks like:
Dim myConnection As OdbcConnection = New OdbcConnection()
myConnection.ConnectionString = "DSN=MYALERTS"
It still works & connects, however the user who set this up is no longer around. I need to view/edit this, but when I go into "ODBC Data Source Administrator" in Windows I see no DSNs listed. I assume this is because I'm under my own user. Even under "System DSN" tab, the list is blank.
Is there a Windows command (or even VB code) to view all DSNs on this Windows 7 machine?
I was able to view all DSNs in the registry:
HKEY_LOCAL_MACHINE->SOFTWARE->ODBC->ODBC.INI
I realize this is a little dated, but perhaps will help someone. When using the ODBC Manager on a 64 bit Windows OS, you will only see 64 ODBC connections. If the connection you are seeking was setup for 32 bit, you can browse down to \Windows\SysWOW64\odbcad32.exe applet and manage the legacy 32 bit DSNs. This is helpful when working with applications that are compiled to target 32 bit.
This prints user and system DSN (data source name) from Windows registry.
Module ModuleDsn
Public Enum DataSourceType
System
User
End Enum
Sub Main()
Dim SU As SortedList = GetDataSourceNames(DataSourceType.User)
Dim SS As SortedList = GetDataSourceNames(DataSourceType.System)
Dim count As Integer = SU.Count + SS.Count
Dim mKeys As [String]() = New String(count - 1) {}
SU.Keys.CopyTo(mKeys, 0)
SS.Keys.CopyTo(mKeys, SU.Keys.Count)
For i As Integer = 0 To mKeys.Length - 1
Console.WriteLine(mKeys(i))
Next
End Sub
Public Function GetDataSourceNames(ByVal dsnType As DataSourceType) As System.Collections.SortedList
Dim dsnList As New System.Collections.SortedList()
Dim reg As Microsoft.Win32.RegistryKey = Nothing
If dsnType = DataSourceType.User Then
reg = (Microsoft.Win32.Registry.CurrentUser).OpenSubKey("Software")
Else
reg = (Microsoft.Win32.Registry.LocalMachine).OpenSubKey("Software")
End If
If reg IsNot Nothing Then
reg = reg.OpenSubKey("ODBC")
If reg IsNot Nothing Then
reg = reg.OpenSubKey("ODBC.INI")
If reg IsNot Nothing Then
reg = reg.OpenSubKey("ODBC Data Sources")
If reg IsNot Nothing Then
For Each sName As String In reg.GetValueNames()
dsnList.Add(sName, DataSourceType.User)
Next
End If
Try
reg.Close()
Catch
End Try
End If
End If
End If
Return dsnList
End Function
End Module

Asp classic on iis7 dns less connection issue

I cant get dns less connection to Ms Access db to work here the code.
In the dbconnection.asp file is this
Dim MM_IMT_STRING
''MM_IMT_STRING = "dsn=EDA;"
MM_IMT_STRING =("Driver={Microsoft Access Driver (*.mdb)};DBQ=C:\inetpub\wwwroot\essel\Connections\EDA.mdb")
and in the default.asp is this
<!--#include file="Connections/dbconnection.asp" -->
Dim CHANNEL
Dim CHANNEL_numRows
Set CHANNEL = Server.CreateObject("ADODB.Recordset")
CHANNEL.ActiveConnection = MM_IMT_STRING
I have been struggling with this issue for days now, if someone know why its not linking up please tell me thanks.
Where is your Connection object created?
I'm missing something like
Set objConnMDB = Server.CreateObject("ADODB.Connection")
objConnMDB.ConnectionString = MM_IMT_STRING
objConnMDB.Open
CHANNEL.ActiveConnection = objConnMDB
' // do your stuff here '
objConnMDB.Close
Set objConnMDB = Nothing

ASP.NET application opening WAY too many SQL connections

I have an issue on a codebit with an ASP.NET Webforms 4 application.
I am using SQL server 2008 R2, IIS 7, the website is running on Windows Server 2008 R2 in separate Application pool (integrated mode, .NET4, with support for 32bit assemblies).
The following code is posing problems:
Dim sqlCmd = New SqlClient.SqlCommand
With sqlCmd
Using sqlConnectionToUse As SqlClient.SqlConnection = GetActiveConnexion(pstrConnectString), _
vAdaptor As New SqlClient.SqlDataAdapter
.Connection = sqlConnectionToUse
.CommandText = pstrSQL 'function parameter
vAdaptor.SelectCommand = sqlCmd
'query1: SELECT somecolumn FROM table WHERE somecolumn '' ==> opens a new connection in SQL Server
'query2: SELECT someothercolumn FROM anothertable WHERE someothercolumn 23 ==> uses my WebSite process active connection
vAdaptor.Fill(vDataSet)
End Using
End With
UPDATE: the GetActiveConnexion() method simply does the following code in my case:
Return New SqlClient.SqlConnection("my connection string obtained from the web.config file")
When I run the query2, everything goes smoothly, the ASP.NET application uses the openned connection of the application pool and I get my results in the dataset.
However, whenever I run the query1, a NEW connection is openned in SQL server (I can see it show up in the SSMS's Activity Monitor) and this one remains openned. The problem is that If I run this query1 100 times, I reach the connection pool's limit and very bad things happens. I still gets the results in the dataset, can use them etc...
The new connection is created on the call of vAdaptator.Fill().
Any idea on what's wrong ?
Thanks a lot for your time.
(PS: sorry for the bad english).
Here is the code in C# for those who prefer:
object sqlCmd = new SqlClient.SqlCommand();
using (SqlClient.SqlConnection sqlConnectionToUse = GetActiveConnexion(pstrConnectString)) {
using (SqlClient.SqlDataAdapter vAdaptor = new SqlClient.SqlDataAdapter()) {
sqlCmd.Connection = sqlConnectionToUse;
sqlCmd.CommandText = pstrSQL; //function parameter
vAdaptor.SelectCommand = sqlCmd;
//query1: SELECT F10_ID FROM FIN_MONTANT_TT_F10 WHERE F10_ID_TT_F19 = '' ==> opens a new connection in SQL Server
//query2: SELECT A48_ID FROM ADH_EPARTICIPANT_ADMIN_A48 WHERE A48_ID=23 ==> uses my WebSite process active connection
vAdaptor.Fill(vDataSet);
}
}
Your SqlCommand instance should be wrapped in a using block as it's Disposable. It's probably the source of your problems.
using (SqlClient.SqlConnection sqlConnectionToUse = GetActiveConnexion(pstrConnectString))
{
using (SqlCommand sqlCmd = sqlConnectionToUse.CreateCommand())
{
using (SqlClient.SqlDataAdapter vAdaptor = new SqlClient.SqlDataAdapter())
{
...
}
}
}
Or VB
Using sqlConnectionToUse As SqlClient.SqlConnection = GetActiveConnexion(pstrConnectString)
Using sqlCmd As SqlCommand = sqlConnectionToUse.CreateCommand()
Using vAdaptor As New SqlClient.SqlDataAdapter()
...
End Using
End Using
End Using

Resources