locked
The name 'Database' does not exist in the current context RRS feed

  • Question

  • User1045460610 posted

    I have a WebForm in my web site project that runs on my test server from Internet Explorer and Chrome on the network so I know it can work. When I copy the web site to our vendors web portal on the development server to a sub folder the page runs on the server in Visual Studio 2017 internal browser and browse with Internet Explorer. If I try to browse with Internet Explorer or Chrome from the network I get the error below. The Databases is a Public Class I listed below. I have a web.config in the sub folder where I copied the web site project with some database connection strings. I think I probably need to put my connection strings in the web.config of the portal folder one node up. If it runs in Visual Studio and Internet Explorer and Chrome on the development server but not from the network, what is a way I can debug that to find out why I get this error as listed below?
    listcoursestoday.aspx is the web page I'm testing. I see it listed at the end of the Detailed Compiler Output.

    .net target framework 4.6.1. 

    Server Error in '/ICS' Application.
    Compilation Error
    Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

    Compiler Error Message: CS0103: The name 'Database' does not exist in the current context

    Source Error:


    Line 17: if (!Page.IsPostBack)
    Line 18: {
    Line 19: SqlConnection con = new SqlConnection(Database.ConnectionString);
    Line 20: //SqlDataAdapter da = new SqlDataAdapter("select * from courses order by RecordID", con);
    Line 21:

    Source File: c:\Program Files\Jenzabar\ICS.NET\Portal\StaticPages\WebForm.CheckIn\listcoursestoday.aspx.cs Line: 19


    Compiler Warning Messages:
    Warning: CS1685: The predefined type 'System.Threading.Tasks.Task' is defined in multiple assemblies in the global alias; using definition from 'c:\Windows\Microsoft.NET\Framework64\v4.0.30319\mscorlib.dll'
    Source Error:


    [No relevant source lines]

    Warning: CS1685: The predefined type 'System.Threading.Tasks.Task' is defined in multiple assemblies in the global alias; using definition from 'c:\Windows\Microsoft.NET\Framework64\v4.0.30319\mscorlib.dll'
    Source Error:


    [No relevant source lines]

    This compiler is provided as part of the Microsoft (R) .NET Framework, but only supports language versions up to C# 5, which is no longer the latest version. For compilers that support newer versions of the C# programming language, see http://go.microsoft.com/fwlink/?LinkID=533240

    warning CS1685: The predefined type 'System.Threading.Tasks.Task' is defined in multiple assemblies in the global alias; using definition from 'c:\Windows\Microsoft.NET\Framework64\v4.0.30319\mscorlib.dll'
    warning CS1685: The predefined type 'System.Threading.Tasks.Task' is defined in multiple assemblies in the global alias; using definition from 'c:\Windows\Microsoft.NET\Framework64\v4.0.30319\mscorlib.dll'
    c:\Program Files\Jenzabar\ICS.NET\Portal\StaticPages\WebForm.CheckIn\listcoursestoday.aspx.cs(19,51): error CS0103: The name 'Database' does not exist in the current context

    using System;
    using System.Configuration;
    using System.Web.Configuration;

    public class Database
    {
    static public String ConnectionString
    {
    get
    { // get connection string with name database from web.config.
    //return WebConfigurationManager.ConnectionStrings["ICS_NETConnectionString"].ConnectionString;
    return WebConfigurationManager.ConnectionStrings["OnGroundsWebSiteConnectionString"].ConnectionString;
    }
    }
    }

    Tuesday, September 10, 2019 1:18 PM

All replies

  • User753101303 posted

    Hi,

    This message basically means that you try to use a 'Database' variable which is not declared.

    It seems you want to give access to this connection string through a public static class Database (this class is not static for now) ?

    Tuesday, September 10, 2019 1:48 PM
  • User1045460610 posted

    This class is declared. The webform runs in Visual Studio and Internet Explorer on the server. I get the error when I try to load the webform on the network. Maybe these are references that are not on the web site root. But, I've copied them to the subfolder on the web site. So, it runs in Visual Studio but not From the Network. There must be a different way that references are found when loading the WebForm from a browser on the network. I tried to step through it in debugger in Internet Explorer and Chrome but it just shows the error not the code behind.

    public class Database
    {
    static public String ConnectionString
    {
    get
    { // get connection string with name database from web.config.
    //return WebConfigurationManager.ConnectionStrings["ICS_NETConnectionString"].ConnectionString;
    return WebConfigurationManager.ConnectionStrings["OnGroundsWebSiteConnectionString"].ConnectionString;
    }
    }
    }

    Tuesday, September 10, 2019 2:22 PM
  • User753101303 posted

    Sorry was a bit too quick. Strictly speaking this message means nothing named Database is in scope. Could it be you are just missing a namespace maybe ?

    A quick console demo:

    using System.Data.SqlClient;
    //using DbLayer;
    namespace DbLayer
    {
        class Database
        {
            static public string cs { get { return "Test"; } }
        }
    }
    namespace ConsoleDemo
    {
        class Program
        {
            static void  Main(string[] args)
            {
               
                var a = new SqlConnection(Database.cs); // CS0103 compile time error
                var b = new SqlConnection(DbLayer.Database.cs); // Works or the line above works if uncommenting using DbLayer;
            }
        }
    }

    Tuesday, September 10, 2019 2:34 PM
  • User1045460610 posted

    Thanks, the webform loads in Visual Studio internal browser and with Visual Studio browse from Internet Explorer. The Website is in a subfolder on the web portal. When I try to load it from the network in Internet Explorer or Chrome I get the error? How can I get the scope corrected when I browse from the network? What is different about the scope in Browsing from the server which will load the page or browsing from the network which gets this error? It must be that when I run from Visual Studio the browser is using that Visual Studio web server. It must be that the portal is using the IIS web server and is configured with the references in the web portal project. Can I run a subfolder as a web site as a virtual folder so that the references or pointing to the right files? What I'm trying to do here is pass variables from the web portal to a WebForm in the subfolder. Will the portal variables be in scope if I run the subfolder as a virtual folder. Variables like AccountNumber.

    Tuesday, September 10, 2019 3:14 PM
  • User753101303 posted

    You are using a "Web site project" or a "Web application project" ?

    For now it seems the code which is compiled on the fly is perhaps not exactly the one you have on your own machine ? You have other working forms maybe ? Don't they have a using directive which is not found in this page ?

    What if you start from line 19 ie :

    SqlConnection con = new SqlConnection(Database.ConnectionString);

    and wonder how the compiler could find what is "Database"? Do you have a using NameSpacePath, NameSpacePath being the namespace in which your Database class is declared ?

    Tuesday, September 10, 2019 3:24 PM
  • User1045460610 posted

    It must be that when I run from Visual Studio the browser is using that Visual Studio web server IIS Express. It must be that the portal is using the server IIS web server and is configured with the references in the web portal project.

    Tuesday, September 10, 2019 3:29 PM
  • User1045460610 posted

    I have a web site project in a subfolder. I think the web portal is a web application project because I see visual studio project files in it. For a time it wouldn't build. I was getting "IIdentity' does not contain a definition for 'GetUserName' and no accessible extension method 'GetUserName' accepting a first argument of type 'IIdentity' could be found (are you missing a using directive or an assembly reference?)" on sitemaster.cs and manager.cs. But I commented out those lines and then it did build. 

    <%--<p>You're logged in as <strong><%: User.Identity.GetUserName() %></strong>.</p>--%>

    I hope I didnt' need those lines for anything.

    Tuesday, September 10, 2019 7:00 PM
  • User1045460610 posted

    This warning might be relevant since I'm running a web site project in a sub folder. I used the roslyn compiler. I don't know what compiler the root web portal is using. I see msbuild references in my web site project and I saw that message when I created a web site project in the visual studio pick list. Limited support for msbuild. I'm reading about how web.config inheritance works in subfolders to see if that has anything to do with it. It might have something to do with building, compiling, references and how to know which my web site folder is referencing. Plus the c # version seems to be 5, and I used c# 6. There may be a syntax difference with that database class.

    Warning: CS1685: The predefined type 'System.Threading.Tasks.Task' is defined in multiple assemblies in the global alias; using definition from 'c:\Windows\Microsoft.NET\Framework64\v4.0.30319\mscorlib.dll'

    Tuesday, September 10, 2019 7:07 PM
  • User753101303 posted

    And the sub folder is marked as an application root in IIS ?

    This is just C# ands for now the compiler doesn't find this "Database" name. What is the full namespace for this class and use the fully qualified name for example. Make sure how the compiler could find this (other source compiled at the same time or from a DLL ?)

    I would perhaps first start with a standalone folder to make this work. Once it works I know other issues are related to moving this app under a parent root site.

    Edit: the source code for this Database class is in the App_Code folder ?

    Wednesday, September 11, 2019 7:29 AM
  • User288213138 posted

    Hi Tom4IT,

    Tom4IT

    When I copy the web site to our vendors web portal on the development server to a sub folder the page runs on the server in Visual Studio 2017 internal browser and browse with Internet Explorer.

    According to your description, I couldn’t understand your requirement clearly.

    Did you put this project in the folder of another project and run it in vs2017 then you have this problem?

    If so, please clarify the scenario that require to put and run your project to another project folder.

    Do you want to publish the project to an IIS existing website virtual directory or application?

    Best regards,

    Sam

    Wednesday, September 11, 2019 8:02 AM
  • User1045460610 posted

    I copied the folder using visual studio copy from my test server where it worked to a sub folder on the web portal.  I ran it from Visual Studio browse with the internal browser and Internet Explorer which runs on IIS Express.

    This portal is using some variable to pass things like AccountNumber. I want to be able to include the references so that I can assign variables from the portlet. That works like this code listing

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Jenzabar.Common.ApplicationBlocks.Data; // SQL Helper
    using System.Data.SqlClient; // SQL Helper - direct SQL statement
    //using Jenzabar.Portal.Framework.Facade.IPortalUserFacade

    namespace HostIDPortletWebForm
    {
    public partial class HostIDWebForm : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    //declare a variable for hostid
    //assign HostID to variable
    //string HostID = Jenzabar.Portal.Framework.PortalUser.Current.HostID; //http://lists.jenzabar.net/cgi-bin/wa.exe?A2=ind1502&L=ICS-DEV-L&P=R15171&X=O2501588C930E4B7DD3&Y=thines%40parker.edu&u=4174

    var hostId = Jenzabar.Common.ObjectFactoryWrapper.GetInstance<Jenzabar.Portal.Framework.Facade.IPortalUserFacade>().GetCurrent().HostID;
    //Error CS0234 The type or namespace name 'Facade' does not exist in the namespace 'Jenzabar.Portal.Framework' (are you missing an assembly reference?)
    //added Jenzabar.Portal.Framework.dll

    //build concatenated url with variable
    //http://aspnet-test.parkernet.edu/instructorcourse.aspx?HostID=@@HostID

    }
    }
    }

    I think I need to publish the web site as a folder. If it's a virutal directory I don't know if I will be able to access the references for the account number which is the HostID.

    Wednesday, September 11, 2019 7:06 PM
  • User1045460610 posted

    That's a great idea! I tried the folder on the root and still got the same error. That must mean the error is with frameworks and compilers.

    Wednesday, September 11, 2019 7:12 PM
  • User288213138 posted

    Hi Tom4IT,

    When I copy the web site to our vendors web portal on the development server to a sub folder the page runs on the server in Visual Studio 2017 internal browser and browse with Internet Explorer.

    What is your web portal and what does it do?

    Is it a project or a place to provide test environment?

    Best regards,

    Sam

    Thursday, September 12, 2019 7:45 AM
  • User1045460610 posted

    The web portal is a third party project. It uses all precompiled dll's and everything is a user control or class library. I think maybe my web site type in a subfoldr that uses compile on the fly doesn't work because they don't have the compiler installed for compile on the fly.

    Monday, September 16, 2019 8:59 PM
  • User753101303 posted

    You try to install a separate web applciation "under" an existing web app? I would try first to install it "beside".

    It seems to be a web site project. For now the problem is that you have a piece of code that doesn't know about yoiur database class declared in some other file. Either you misss a namespace or it is in a file which is not compiled in the correct order.

    Wednesday, September 18, 2019 7:06 AM
  • User288213138 posted

    Hi Tom4IT,

    You can try to create an Apps under the website in IIS instead of direct copy paste.

    Best regards,

    sam

    Monday, September 23, 2019 6:33 AM