0

I need to pass the value entered in a textbox control by the admin on application start to the global.asax file , where I can then make it into a Application object. I want this application object to persist for all users and thier sessions but only for a particular database connection string. Is Application object the right choice for this purpose , and if yes how to incorporate it ?

This is what i have tried :

global.asax:

public class Global : System.Web.HttpApplication
{
    SqlConnection con=new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ConnectionString);
    protected void Application_Start(object sender, EventArgs e)
    {



        string folderName = SetPaths.Tbrootpath.Text.ToString();

        string pathString = System.IO.Path.Combine(folderName, "ScannedData");

        Application["scannedDataPath"] = pathString;
    }

and i call it where required as :

  string pathstring = HttpContext.Current.Application["ScannedDataPath"].ToString();

I get SetPaths.Tbrootpath is inaccessible ..

Anita Mathew
  • 183
  • 1
  • 15
  • 1
    Not sure how you should go about it but if you need to enter something, _the application has to already be started_, so you can't do it while it's starting. – wazz Jul 18 '19 at 08:08
  • ok that means i should put it in the session start method instead? but i am trying to put a admin defined path as the application object which persists till the application is shut .so when should i assign it? – Anita Mathew Jul 18 '19 at 09:39

1 Answers1

0

Not quite sure what you mean by "only for a particular database connection string" but I think I understand the gist of what you're getting at.

Like wazz stated, you can't enter a value into a textbox before the application has started. You also can't access a textbox from the Global.asax file, so you'll have to store and retrieve the value from a database.

Your code will also have to be able to handle the scenario in which the admin user has not yet entered the folder name. At minimum, the admin page will need to be able to function without this value being set in order for the admin user to be able to enter the value.

So your code may look something like this:

global.asax

public class Global : System.Web.HttpApplication
{
    SqlConnection con=new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ConnectionString);
    protected void Application_Start(object sender, EventArgs e)
    {
        string folderName = GetFolderNameFromDatabase();
        string pathString = "Default/Path"; // Set a default path that can be used until the admin has entered a folder name for the first time. Could also be empty string etc
        // If a value is found in the database, create path from it.
        // Otherwise, continue with default path.
        if(! string.IsNullOrEmpty(folderName) {    
            pathString = System.IO.Path.Combine(folderName, "ScannedData");
        }

         Application["scannedDataPath"] = pathString;
    }

adminPage.aspx (submit event)

string folderName = Tbroothpath.Text; // Get value from the textbox.

StoreFolderNameInDataBase(folderName); // Store value in the database.
Gabe
  • 837
  • 9
  • 13
  • Is there a way we could move this to a conversation so that i can explain the scenario and help you to better understand what i need so that you could then provide the best possible workaround? – Anita Mathew Jul 20 '19 at 06:38