+ Reply to Thread
Results 1 to 7 of 7

Thread: Writing to web.config

  1. #1
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Writing to web.config

    I have added a key/value to AppSettings to hold an integer value that will increment when a function is called. In my global.asaxon application start I want to call a function to populate my appsetting key with an initial value.

    When I try loading the app I'm getting an access denied error on a .tmp file.

    The code that's called from my Global.asax is as follows:-
    Code:
            public static void GetEvalNo()
            {
                Configuration config = WebConfigurationManager.OpenWebConfiguration("~/");
                //config.AppSettings.Settings["EvalNo"].Value = "Got Here";
                //config.Save(ConfigurationSaveMode.Modified);
                //return;
                //KeyValueConfigurationElement appValue = config.AppSettings.Settings["EvalNo"];
                int evalNo = GlobalDAL.GetEvalNo();
                try
                {
                    config.AppSettings.Settings["EvalNo"].Value = evalNo++.ToString();
                    config.Save(ConfigurationSaveMode.Modified);
                    //return evalNo++.ToString();
                }
                catch (Exception ex)
                {
                    //appValue.Value = ex.ToString();
                }
            }
    
    My web.config is like this:-
    Code:
      <appSettings>
        <add key="EvalNo" value="" />
      </appSettings>
    
    I thought it should be possible to update this value. Any ideas?

  2. #2
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    OK. IIS_User only had Read & Execute permission on the folder.

  3. #3
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Hmm...I'm having another problem.

    Each time I refresh the page, it's resetting the value in my web.config.

    I'm calling the function in Application_Start of Global.asax. Why is the value getting reset each time I refresh the page? Surely it should only call this when the app is compiled?

  4. #4
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    Because, every time you're writing to the web.config file, the application recompiles and everything is reset. The web.config file is best used for "set and forget" values.
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


  5. #5
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Ahhh...That would make sense...

    Where's the best place to store an incremental application level value?

    Basically I have a product table that users allocate an integer value to (there is also a PK integer field) and I have a web service to retrieve the next available value and drop it into the textbox control.

    But, if two people access the form at the same time, I want the second person to get the next available value from the application variable, not from the database value.

    Does that make sense?

    Is it easier to just store in a db field and get the value to reset on application start?

  6. #6
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    2,386
    Blog Entries
    5
    Real Name
    Wolff
    Rep Power
    15

    Well, you could create an small XML file akin to the web.config. This file could then be updated as you have envisioned above.

    You could have the value stored in a db field, as you indicated, but then you have the problem of updating the value in the database so as to get the next available value when the application restarts.

    If having 'holes' in the values doesn't bother you, you could create a db table with an auto increment field and just get the next available value each time. If the user abandons the transaction tho -- that value would not be used.

    Have you considered using a guid rather then an integer value.
    Wolffy
    .-- ----- ..-. ..-. -.--
    Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Void where prohibited by law. Not valid in California. Your mileage may vary.

  7. #7
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Yeah I did think about a seperate XML file. I just thought if I'm going to do that, I may aswell just store it in a db.

    It's kind of a legacy system. They number each potential product that will be produced and I wanted a simple method to ensure duplicate entries weren't made, but allowing the user to enter a value at the same time. Gaps are not a major problem, just didn't want huge gaps to appear.

    I think it'll be slightly redundant, I just want to avoid the possibility of duplicate entries being made. Problem being ensuring no duplicate entries on a user defined field, but also ensuring users who open the page whilst another user is still entering details can not get duplicate.

    If that makes any sense...

+ Reply to Thread

Similar Threads

  1. web.config <handlers>
    By willids in forum .NET Development
    Replies: 23
    Last Post: January 21st, 2010, 08:34 AM
  2. Retrieving ConnectionString from web.config
    By richyrich in forum .NET Development
    Replies: 3
    Last Post: November 13th, 2009, 10:27 AM
  3. Is web.config cached?
    By Wolffy in forum .NET Development
    Replies: 3
    Last Post: September 9th, 2008, 05:04 PM
  4. IIS7 Error from web.config
    By Shem in forum Microsoft IIS
    Replies: 7
    Last Post: August 21st, 2008, 10:30 PM
  5. Writing Expressions
    By nboscaino in forum Microsoft Access
    Replies: 3
    Last Post: August 21st, 2008, 05:48 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO