Link to home
Start Free TrialLog in
Avatar of CIT_DEV
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.Permissions
Imports System.Collections
Imports System.Web
Imports System.Collections.Specialized
Imports System.Web.HttpContext.current





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.DirectorySeparatorChar
        'loop through the files uploaded
        Dim colFiles As System.Web.HttpFileCollection = System.Web.HttpContext.Current.Request.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("appSettings"), NameValueCollection)
        sPath = oUploadPath("app_upload_path").ToString()

        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(objCurrentFile.FileName)

                If strCurrentFileName <> "" Then
                    strCurrentFileExtension = System.IO.Path.GetExtension(strCurrentFileName)
                    If strCurrentFileExtension <> "" Then
                        If Not IsEditMode = 0 Then
                            Call InsertAttachments(lAdvertisementID, enumInspectionTypeID.Advertisement, strCurrentFileName, lUser_ID)

                        Else
                            Call UpdateAttachments(lAttachmentID, lAdvertisementID, enumInspectionTypeID.Advertisement, strCurrentFileName, lUser_ID)
                        End If
                        Try
                            Dim root As String = System.Web.UI.Server.MapPath("Attachments\" & "\" & lAdvertisementID)
                            Dim postedFile = objCurrentFile
                            Dim filename = Path.GetFileName(postedFile.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.ToString() & 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!
ASKER CERTIFIED SOLUTION
Avatar of ecc204
ecc204

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi,

perhaps it my own lack of something but to me the line

Dim root As String = System.Web.UI.Server.MapPath("Attachments\" & "\" & lAdvertisementID)

makes little sense...

to use server.mapPath i would simply do the following...

Imports System.Data.OleDb

dbconn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data source=" & server.mappath("northwind.mdb"))

it then maps a path relative to the location of the file you are calling it from.

Hope this helps.

Avatar of CIT_DEV
CIT_DEV

ASKER

The line should read
Dim root As String = Server.MapPath("Attachments\" & "\" & lAdvertisementID)