-
beginner's question
I'm trying to connect to database using a ASP.NET Web Application.
I drag and drop an OleDbConnection control and then import files,
provide the connection string and test connection to Access NWIND.mdb
Imports System
Imports System.Data
Imports System.Data.Common
Imports System.Data.OleDb
then in the page_load event
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Try
OleDbConnection1.Open()
Response.Write("Success")
Catch dbException As OleDbException
Response.Write("Unable to open database")
End Try
End Sub
It says Unable to open database.
Whereas I do the same steps in Windows application it gets connected to the same database just fine.
I can't figureout what am I doing wrong?
-
i think u are missing some lines of code. u need to look not only in ur form_load sub. Search after e.g. OleDbConnection1 declaration maybe at the top somewhere...
-
Well here is the whole code, when I debug it it first goes to page_init, executes the line initialize_component but I don't see it going to it then goes to try_catch and fails there.Please suggest:
Imports Microsoft.VisualBasic
Imports System
Imports System.Data
Imports System.Data.Common
Imports System.Data.OleDb
Public Class WebForm1
Inherits System.Web.UI.Page
Protected WithEvents OleDbConnection1 As System.Data.OleDb.OleDbConnection
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.OleDbConnection1 = New System.Data.OleDb.OleDbConnection()
'
'OleDbConnection1
'
Me.OleDbConnection1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Password="""";User ID=Admin;Data Source=C:\Inetpub" & _
"\wwwroot\Database\NWIND.MDB;Mode=Share Deny None;Extended Properties="""";Jet OLED" & _
"B:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Database Password="""";J" & _
"et OLEDB:Engine Type=5;Jet OLEDB:Database Locking Mode=1;Jet OLEDB:Global Partia" & _
"l Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:New Database Passwor" & _
"d="""";Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet" & _
" OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repa" & _
"ir=False;Jet OLEDB:SFP=False"
End Sub
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Try
OleDbConnection1.Open()
Response.Write("Success")
Catch dbException As OleDbException
Response.Write("Unable to open database")
End Try
End Sub
End Class
-
I'm track the error and I get
The Microsoft Jet database engine cannot open the file 'C:\Inetpub\wwwroot\Database\login.mdb'. It is already opened exclusively by another user, or you need permission to view its data.
Whereas if I try to open it with VB.NET application it opens just fine!
-
Make sure the directory contaning the access file has read access and is granted to the ASPNET account.
-
I tried it in the same directory under wwwroot and also ouside it.
The directory is not read only. Also pleae explain what do you mean by 'and is granted to the ASPNET account'
-
try using this connectionstring for ur db:
Code:
"Provider=Microsoft.Jet.OLEDB.4.0; data source=" & C:\Inetpub" & _
"\wwwroot\Database\NWIND.MDB"
or just remove that "Mode=Share Deny None" thing, maybe that works also, or find some other proberty for that mode.
one more thing, u could write in ur try catch code not only "Unable to open database" evenmore u could use "dbException.message" that will return the exactly errormessage. And dont forget to close ur dbconnection when u finished with ur access to it.
greetings
p.s.: if u restart ur iis it should work again too, but u should do this only on ur local machine ^^