CIT_DEV
asked on
Creating a class library
I am creating a class library that will encapsulate all of the database
access code. Within the class library, I want the code to establish the
Server.MapPath of a database file for use in a connection string.
Currently, Visual Studio.NET gives me an error on the Server.MapPath code.
When i compile i get the following message
Name "Server" is not declared.
What namespace should import into the class library so VS.NET will
understand the Server.MapPath code. I have tried a bunch, but none seems to
work.
The Names spaces used in this class library
Imports System
Imports System.Data.SqlClient
Imports System.Web.HttpContext
Imports System.Web.Mail
Imports System.Text
Imports System.IO
Imports eBusiness
Imports Microsoft.VisualBasic
Imports System.Security
Imports System.Security.Permission s
Imports System.Collections
Imports System.Web
Imports System.Collections.Special ized
Imports System.Web.HttpContext.cur rent
Here is the Code>>>>>
Public Function UploadAttachments(ByVal lAttachmentID As Long, ByVal lAdvertisementID As Long, ByVal lUser_ID As Long, ByVal IsEditMode As Long)
Dim directorySeparatorChar As Char = Path.DirectorySeparatorCha r
'loop through the files uploaded
Dim colFiles As System.Web.HttpFileCollect ion = System.Web.HttpContext.Cur rent.Reque st.Files
'Message to the user
Dim strMessage As New System.Text.StringBuilder( "Files Uploaded:<br><br>")
Dim intFileCntr As System.Int32
Dim Attachment_Type_ID As Long
Dim oUploadPath As NameValueCollection
Dim sPath As String
'Get Upload Path from WebConfig file
oUploadPath = CType(HttpContext.Current. GetConfig( "appSettin gs"), NameValueCollection)
sPath = oUploadPath("app_upload_pa th").ToStr ing()
Try
For intFileCntr = 0 To colFiles.Count - 1
Dim objCurrentFile As System.Web.HttpPostedFile = colFiles(intFileCntr)
Dim strCurrentFileName, strCurrentFileExtension As System.String
strCurrentFileName = System.IO.Path.GetFileName (objCurren tFile.File Name)
If strCurrentFileName <> "" Then
strCurrentFileExtension = System.IO.Path.GetExtensio n(strCurre ntFileName )
If strCurrentFileExtension <> "" Then
If Not IsEditMode = 0 Then
Call InsertAttachments(lAdverti sementID, enumInspectionTypeID.Adver tisement, strCurrentFileName, lUser_ID)
Else
Call UpdateAttachments(lAttachm entID, lAdvertisementID, enumInspectionTypeID.Adver tisement, strCurrentFileName, lUser_ID)
End If
Try
Dim root As String = System.Web.UI.Server.MapPa th("Attach ments\" & "\" & lAdvertisementID)
Dim postedFile = objCurrentFile
Dim filename = Path.GetFileName(postedFil e.FileName )
Dim contentType As String = postedFile.ContentType
Dim contentLength As Integer = postedFile.ContentLength
If Not Directory.Exists(root) Then
Directory.CreateDirectory( root)
End If
postedFile.SaveAs(root & directorySeparatorChar.ToS tring() & filename)
Catch ex As Exception
Throw New Exception("Error Creating Directory: " & ex.Message)
End Try
End If
End If
Next
Return True
Catch Ex As System.Exception
Throw New Exception("Error Saving File : " & ex.Message)
End Try
End Function
<<<<<<<End of Code>>>>>>
Thanks in advance!
access code. Within the class library, I want the code to establish the
Server.MapPath of a database file for use in a connection string.
Currently, Visual Studio.NET gives me an error on the Server.MapPath code.
When i compile i get the following message
Name "Server" is not declared.
What namespace should import into the class library so VS.NET will
understand the Server.MapPath code. I have tried a bunch, but none seems to
work.
The Names spaces used in this class library
Imports System
Imports System.Data.SqlClient
Imports System.Web.HttpContext
Imports System.Web.Mail
Imports System.Text
Imports System.IO
Imports eBusiness
Imports Microsoft.VisualBasic
Imports System.Security
Imports System.Security.Permission
Imports System.Collections
Imports System.Web
Imports System.Collections.Special
Imports System.Web.HttpContext.cur
Here is the Code>>>>>
Public Function UploadAttachments(ByVal lAttachmentID As Long, ByVal lAdvertisementID As Long, ByVal lUser_ID As Long, ByVal IsEditMode As Long)
Dim directorySeparatorChar As Char = Path.DirectorySeparatorCha
'loop through the files uploaded
Dim colFiles As System.Web.HttpFileCollect
'Message to the user
Dim strMessage As New System.Text.StringBuilder(
Dim intFileCntr As System.Int32
Dim Attachment_Type_ID As Long
Dim oUploadPath As NameValueCollection
Dim sPath As String
'Get Upload Path from WebConfig file
oUploadPath = CType(HttpContext.Current.
sPath = oUploadPath("app_upload_pa
Try
For intFileCntr = 0 To colFiles.Count - 1
Dim objCurrentFile As System.Web.HttpPostedFile = colFiles(intFileCntr)
Dim strCurrentFileName, strCurrentFileExtension As System.String
strCurrentFileName = System.IO.Path.GetFileName
If strCurrentFileName <> "" Then
strCurrentFileExtension = System.IO.Path.GetExtensio
If strCurrentFileExtension <> "" Then
If Not IsEditMode = 0 Then
Call InsertAttachments(lAdverti
Else
Call UpdateAttachments(lAttachm
End If
Try
Dim root As String = System.Web.UI.Server.MapPa
Dim postedFile = objCurrentFile
Dim filename = Path.GetFileName(postedFil
Dim contentType As String = postedFile.ContentType
Dim contentLength As Integer = postedFile.ContentLength
If Not Directory.Exists(root) Then
Directory.CreateDirectory(
End If
postedFile.SaveAs(root & directorySeparatorChar.ToS
Catch ex As Exception
Throw New Exception("Error Creating Directory: " & ex.Message)
End Try
End If
End If
Next
Return True
Catch Ex As System.Exception
Throw New Exception("Error Saving File : " & ex.Message)
End Try
End Function
<<<<<<<End of Code>>>>>>
Thanks in advance!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
The line should read
Dim root As String = Server.MapPath("Attachment s\" & "\" & lAdvertisementID)
Dim root As String = Server.MapPath("Attachment
perhaps it my own lack of something but to me the line
Dim root As String = System.Web.UI.Server.MapPa
makes little sense...
to use server.mapPath i would simply do the following...
Imports System.Data.OleDb
dbconn=New OleDbConnection("Provider=
it then maps a path relative to the location of the file you are calling it from.
Hope this helps.