Forum Moderators: open

Message Too Old, No Replies

Connecting to an access database

I'm using ASP.net (VB code)

         

chris_f

4:16 pm on May 26, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm tring to connect to an access database using ASP.net. This works if I put the connection sting on the page, however, if I store the connection string in the global.asa file and refer to that, it breaks. Has anyone got any ideas. I've trebel checked my code. The code below works, however, if I replace the connection string with Session("ConnectionString") if breaks.

---testconnect.aspx---


<%@ Page Language="vb" Debug="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>

<html>
<head>
<title>Database Connection Test</title>
<script runat="server">
sub Page_Load(sender as Object, e as EventArgs)

varlabel.text = Session("var")

'Create a connection string
Dim connString as String
connString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=C:\Inetpub\wwwroot\sites\portfolio\portfoliodb.mdb;"


'Open a connection
Dim objConnection as OleDbConnection
objConnection = New OleDbConnection(connString)
objConnection.Open()

'Specify the SQL string
Dim strSQL as String = "SELECT * FROM test"

'Create a command object
Dim objCommand as OleDbCommand
objCommand = New OleDbCommand(strSQL, objConnection)

'Get a datareader
Dim objDataReader as OleDbDataReader
objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection)

'Do the DataBinding
dgResults.DataSource = objDataReader
dgResults.DataBind()

'Close the datareader/db connection
objDataReader.Close()

end sub
</script>
</head>
<body>

<!--<asp:DataGrid id="dgResults" runat="server" />-->
<br><br>
<asp:Label ID="varlabel" Runat="server" />

</body>
</html>

---Global.asa
Dim ConnectionString As String -- This is at the top of the Gloal.asa in the public class just below the inherits


ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=C:\Inetpub\wwwroot\sites\portfolio\portfoliodb.mdb;"
-- This is in the SessionStart part of the global.asa


Additional: The forum is adding the http:// before the wwwroot. It is not in my code.

Grumpus

4:54 pm on May 26, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's not going to work in your global ASA file. The safest way to do it is to put that string in a blank file called "connection.asp" or something like that and use a standard SSI from the main asp file (i.e. include virtual="/connections.asp" ) or whatever it is.

Or simply paste the whole string into every page.

G.

chris_f

10:43 am on May 27, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I know this is achievable throught the global.asa. I have worked on sites that have done this. Thanks for your help. I will keep trying.

chris_f

8:20 am on May 28, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Rebuilt the project from the ground up and seems to work now. Thanks for you help anyway.

Chris

andrey_sea

3:10 am on May 30, 2002 (gmt 0)

10+ Year Member



Chris,

You might want to use "code-behind" pages which alow for better separation of server logic from presentation side HTML... It is a good idea to swich early before you have coded too many pages...