Tech Off Thread

18 posts

Where do you store single pieces of information?

Back to Forum: Tech Off
  • User profile image
    Sven Groot

    If I have a single piece of information that can change, but where only one will ever be needed, where do I store it? Do I create a database table that will only hold one row? Is there a better way?

  • User profile image
    punkouter

    umm,... configuration file? App.config or Web.config can do the job, I think. Or, you can use plain file and have it watched by using FileSystemWatcher. Or,... registry?

  • User profile image
    TommyCarlier

    Like punkouter said, a configuration file is a good option. If you want to store it in your database, you can also do that. I developed a basic layout for our database system that contains some default tables, views, stored procedures and functions that we apply to each of our databases. Before designing a new database, we first run a SQL script that creates these objects. One of the default tables is _ExtendedProperties, which is used to store single pieces of information. It has 2 fields: Key (NVARCHAR(MAX)) and Value (NVARCHAR(MAX)) and it's basically a dictionary. Of course it only contains string values, but that's usually OK. It is not used very often, but it is handy for cases like yours, where you want to store loose values.

  • User profile image
    Sven Groot

    Just to be a little more specific, it's just some text displayed on a single page of a website that the site owners will be able to change via the admin interface (it's actually two pieces of text: the version as the user input it and the html formatted version).

    That rules out web.config because I don't want to change that from code (has some nasty side-effects like resetting the application). I could use a flat file though. Such an ExtendedProperties table might be an idea, but the only thing it'd hold for the foreseeable future would be that piece of text.

  • User profile image
    vesuvius

    Why not create an object of this textual data, and serialize/deserialize it to an xml file? You could be messy and save it in 'viewstate' or a 'session' object, I'd stick with .xml, it's the .NET way.

  • User profile image
    TommyCarlier

    Even if it holds just 1 record, it's not a waste. Most of our databases usually have only 1 record in _ExtendedProperties. The main advantage (for us) is uniformity. We have this basic database layout, we have a .NET library for data access, which has a Database-class, which has methods like GetExtendedProperty and SetExtendedProperty that makes it really easy to store and retrieve single pieces of data from .NET code.

  • User profile image
    stevo_

    Another consideration I'd say is to think about 'data packaging' splitting data up can be a nightmare for keeping applications portable and scalable.

  • User profile image
    Matthew van Eerde

    My approach would be to stick only the original version as the user entered it in a flat file, and stick the HTML-encodified version in the Application object.

    Also, of course, in the Application_OnLoad event, I would read the file, HTML-encode it, and stick the result in the Application object.

    On the other hand, if I had any suspicion that I would get a call that "I accidentally changed the text and I want to put it back to what it was", I would use a wiki-style data record (so that changes were kept in perpetuity.)

  • User profile image
    punkouter

    Sven Groot wrote:
    Just to be a little more specific, it's just some text displayed on a single page of a website that the site owners will be able to change via the admin interface (it's actually two pieces of text: the version as the user input it and the html formatted version).

    That rules out web.config because I don't want to change that from code (has some nasty side-effects like resetting the application). I could use a flat file though. Such an ExtendedProperties table might be an idea, but the only thing it'd hold for the foreseeable future would be that piece of text.


    database is the way to go sven.

    you can use the ProfileProvider you can use the SetPropertyValues to insert any kind of properties you want the user to have. the data then will get serialized into a field in the profile table in the database and you dont even have to create a table for the text. you can even extend the profile provider to match your needs.

  • User profile image
    vesuvius

    This is the problem with programming. So may different ways to do the same thing (how many ways to skin a cat?). Most of them are right, some are ineloquent.

    We are all vying for our technique but most of the suggestions are right.
    Just do what feels right (Oh I meant the application object [Matt], been a year since I did any ASP.NET).

  • User profile image
    Sven Groot

    punkouter wrote:
    
    Sven Groot wrote:
    Just to be a little more specific, it's just some text displayed on a single page of a website that the site owners will be able to change via the admin interface (it's actually two pieces of text: the version as the user input it and the html formatted version).

    That rules out web.config because I don't want to change that from code (has some nasty side-effects like resetting the application). I could use a flat file though. Such an ExtendedProperties table might be an idea, but the only thing it'd hold for the foreseeable future would be that piece of text.


    database is the way to go sven.

    you can use the ProfileProvider you can use the SetPropertyValues to insert any kind of properties you want the user to have. the data then will get serialized into a field in the profile table in the database and you dont even have to create a table for the text. you can even extend the profile provider to match your needs.

    There are no profiles or users or whatever, so that'd be complete overkill as I'd need to set up the whole ASP.NET profile stuff in the database just to get that working.

  • User profile image
    wisemx

    Linq to XML in the App_Data folder.
    You will hardly believe how fast it is. Smiley

  • User profile image
    Sven Groot

    wisemx wrote:
    Linq to XML in the App_Data folder.
    You will hardly believe how fast it is. Smiley

    Unfortunately I don't have .Net 3.5 on that server. Sad

  • User profile image
    TommyCarlier

    If it's just some text, the simplest solution would probably be to use a simple text-file. Putting it in App_Data is probably a good idea. And File.ReadAllText(path) and File.WriteAllText(path, text) make it very easy to read and write the text.

  • User profile image
    Minh

    I'm exploring this scenario:

    web.config

    <configuration>
        <appSettings file="external.config">
           <add key="[required key]" value="[value]" />
        </appSettings>
    </configuration>


    external.config

    <appSettings>
        <add key="[changeable key]" value="[changeable value] />
    </appSettings>


    Then have a function where the user can OVERWRITE external.config. Probably won't work for all setups, but it won't require a DB.

    But if I had a DB, I'd go w/ that.

    Also, I'm seeing that changing external.config doesn't trigger an app restart if you were to change web.config.

  • User profile image
    MasterPi

    Wow, I just recently ran into that question. I was trying to have a little announcement appear on the main page of a site, letting the admins control what the text would be. I didn't want to create an entire table just to store that info, nor did I want to bother with text.

    In the end, I just used an Application object. But if that's not good for some reason, I too would like some advice on better methods.

  • User profile image
    littleguru

    Disclaimer: didn't read the whole thread but want to answer to Sven's original question.

    I usally store this stuff into the database (single row) if it is the same for ALL users who use the system - independent on which PC they sign in. Otherwise it's going directly to the config file.

    For ASP.NET I usually use the config file too because it's also the same for all the visitors.

  • User profile image
    jsampsonPC

    In the past I've had to do some things like this for small client sites. I wanted to give them the ability to change "the front page paragraph", "the about us column", and "our slogans on different pages."

    Of course I wouldn't learn of this until things were pretty much finished being developed, so I went with a simple two-column table:

    location
    text

    Examples:

    frontPageParagraph | Hello World
    aboutUsColumn | Hello World Again
    totalMembers | 2878
    websiteCreatedDate | 2008-01-01

    That way I could simply "SELECT text FROM oneLiners WHERE location = 'frontPageParagraph'".

    Again, probably not very elegant, but it worked. Like a previous niner said too, any solution of this type won't give you history, so you could simply toss in an isActive field of type bool, and rather than updating the text field, just insert a new record and give it the exclusive isActive value of True.

    Then it's merely "SELECT text FROM oneLiners WHERE (location = 'frontPageParagraph') AND (isActive = true)"

    Take it with a grain of salt Smiley

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.