Showing posts with label jet. Show all posts
Showing posts with label jet. Show all posts

Tuesday, March 13, 2012

Server.MapPath and ASP.Net

I have this code:

Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\unzipped\exceldotnet[1]\exceltest.xls;" & _
"Extended Properties=Excel 8.0;"

which works fine. I have copied the .xls-file to my project folder and tries this code:

Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & server.mappath("exceltest.xls") & ";" & _
"Extended Properties=Excel 8.0;"

But then I receive an error saying:

The Microsoft Jet database engine cannot open the file ''. It is already opened exclusively by another user, or you need permission to view its data.

What's wrong, can't I use the server.mappath?Make sure the asp.net account has access to the directory containing the .mdb file.
You're right, I'd forgot! Thanks ;)
Hi again, I have problems with Server.MapPath in a class. The intellisense wouldn't recognize server.mappath. Am I doing something wrong or do I need to specify a namespace? Here is my code:

Imports System.Data
Imports System.Data.OleDb
Public Class clsMedieroer : Inherits clsMateriale
Private _medie As Integer
Private _medie1 As Integer
Private _dimension As Integer

Sub New(ByVal s As Integer, ByVal m As String)
' konstruktren skaber et materiale med s
' (sker ved at kalde klassens metode Medie med s som parameter)
MyBase.New(m)
Medie = s
End Sub

Public Sub FindMedie()
Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & server.mappath("exceltest.xls") & ";" & _
"Extended Properties=Excel 8.0;"

Dim strSql As String = "SELECT * FROM [Sheet1$] WHERE Id=4"

Dim objConnection As New OleDbConnection(strConn)
Dim objCommand As New OleDbCommand(strSql, objConnection)
Dim objDataReader As OleDbDataReader

objConnection.Open()
objDataReader = objCommand.ExecuteReader()

Do While objDataReader.Read() = True
_dimension = CInt(objDataReader("pris"))
Loop
objDataReader.Close()
objConnection.Close()

Select Case _medieroer
Case Is = "St. 37.0 Konti"
_medie1 = 15
Case Else
_medie1 = _medie
End Select
End Sub

Public ReadOnly Property Medie1() As Integer
' returnerer vrdien af materialet, der lses fra
Get
Return _medie1
End Get
End Property

Public ReadOnly Property Dimension() As Integer
' returnerer vrdien af materialet, der lses fra
Get
Return _dimension
End Get
End Property

Public WriteOnly Property Medie() As Integer
' definerer vrdien af materialet
Set(ByVal Value As Integer)
_medie = Value
FindMedie()
End Set
End Property

End Class
using System.Web;
I've tried typing
Imports system.web
but it still doesn't work

server.mappath challenging problem

I created a page that uses the following line in it.:

Public votingDatabasePath = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.mappath("/datasources/votingDB.mdb")

and the page worked brilliantly.
I then changed this into a web user control, and the program always crashes on this line saying the following. The weird thing is that I load the user control in a try catch statement, so the page should never go to the full error page.

Any help would be greatly appriciated.

Jagdip

---------------------------
System.NullReferenceException: Object reference not set to an instance of an object.

The source code that generated this unhandled exception can only be shown when compiled in debug mode. To enable this, please follow one of the below steps, then request the URL:

1. Add a "Debug=true" directive at the top of the file that generated the error. Example:

<%@dotnet.itags.org. Page Language="C#" Debug="true" %
or:

2) Add the following section to the configuration file of your application:

<configuration>
<system.web>
<compilation debug="true"/>
</system.web>
</configuration
Note that this second technique will cause all files within a given application to be compiled in debug mode. The first technique will cause only that particular file to be compiled in debug mode.

Important: Running applications in debug mode does incur a memory/performance overhead. You should make sure that an application has debugging disabled before deploying into production scenario.
---------------------------
Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
System.Web.UI.UserControl.get_Server() +8
_test.jags_voting..ctor() in C:\Documents and Settings\.....voting\jags_voting.ascx.vb:18
ASP.jags_voting_ascx..ctor()

[TargetInvocationException: Exception has been thrown by the target of an invocation.]
System.RuntimeType.CreateInstanceImpl(Boolean publicOnly) +0
System.Activator.CreateInstance(Type type, Boolean nonPublic) +66
System.Web.UI.PartialCachingControl.CreateCachedControl() +225
System.Web.UI.BasePartialCachingControl.OnInit(EventArgs e) +93
System.Web.UI.Control.InitRecursive(Control namingContainer) +241
System.Web.UI.Control.InitRecursive(Control namingContainer) +179
System.Web.UI.Control.InitRecursive(Control namingContainer) +179
System.Web.UI.Control.InitRecursive(Control namingContainer) +179
System.Web.UI.Control.InitRecursive(Control namingContainer) +179
System.Web.UI.Control.InitRecursive(Control namingContainer) +179
System.Web.UI.Control.InitRecursive(Control namingContainer) +179
System.Web.UI.Control.AddedControl(Control control, Int32 index) +185
System.Web.UI.ControlCollection.Add(Control child) +153
_test.JagsDTGDashboard.writePanels() in C:\Documents and Settings\.....\JagsDTGDashboard.aspx.vb:666
_test.JagsDTGDashboard.dropDownListIndexChanged(Object sender, EventArgs e) in C:\Documents and Settings\....\JagsDTGDashboard.aspx.vb:259
System.Web.UI.WebControls.ListControl.OnSelectedIndexChanged(EventArgs e) +108
System.Web.UI.WebControls.DropDownList.System.Web.UI.IPostBackDataHandler.RaisePostDataChangedEvent() +26
System.Web.UI.Page.RaiseChangedEvents() +115
System.Web.UI.Page.ProcessRequestMain() +1072Server is not a property ofWebControl orControl, which are inherited by theUser Control. You have to reference Server via the Page property:

Public votingDatabasePath = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &Page.Server.mappath("/datasources/votingDB.mdb")

I'm afraid that that has not solved the problem. The same error appears.

I'm just wondering what the ..ctor() means in

[NullReferenceException: Object reference not set to an instance of an object.]
System.Web.UI.UserControl.get_Server() +8
_test.jags_voting..ctor() in C:\Documents and Settings\.....voting\jags_voting.ascx.vb:18
ASP.jags_voting_ascx..ctor()

Jagdip
I'm pretty sure that ..ctor() means 'constructor'. the control is failing to get created. With web controls, I have found that I have to make sure that any relative paths are relative to the parent page that will run the control. If your page that hosts the control is in a different folder from the control, the results are a little unpredictable.