Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# Theory question

C# Theory question

Scheduled Pinned Locked Moved C#
questiondatabasecsharptoolshelp
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gonad
    wrote on last edited by
    #1

    I'm new to c# and programming altogether, so can you help me figure out the best way to do this? i have a "Tools > Options" dialog box where a user can put in the db settings to connect to a database (IP, username, password, etc). once a user submits these settings, i want the whole application to reference these settings for the database connection. i have about 5 forms on this small app that connect to a db, so i don't want to statically assign these settings per form. can i (or should I) use a struct and define the users settings as constants? if so, how can i call the struct from each form? Thanks for your help.

    L N 2 Replies Last reply
    0
    • G gonad

      I'm new to c# and programming altogether, so can you help me figure out the best way to do this? i have a "Tools > Options" dialog box where a user can put in the db settings to connect to a database (IP, username, password, etc). once a user submits these settings, i want the whole application to reference these settings for the database connection. i have about 5 forms on this small app that connect to a db, so i don't want to statically assign these settings per form. can i (or should I) use a struct and define the users settings as constants? if so, how can i call the struct from each form? Thanks for your help.

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      .gonad wrote: i have a "Tools > Options" dialog box You have or want? .gonad wrote: i want the whole application to reference these settings for the database connection. Add a name/value to your config file. Then you can just get the value via dynamic properties. leppie::AllocCPArticle(Generic DFA State Machine for .NET);

      G 1 Reply Last reply
      0
      • L leppie

        .gonad wrote: i have a "Tools > Options" dialog box You have or want? .gonad wrote: i want the whole application to reference these settings for the database connection. Add a name/value to your config file. Then you can just get the value via dynamic properties. leppie::AllocCPArticle(Generic DFA State Machine for .NET);

        G Offline
        G Offline
        gonad
        wrote on last edited by
        #3

        Hi. actually, I already have a dialog box where the user will have to enter the db connection properties. basically, i wrote this because i use this app at home AND at the office, but my network settings are different, so this "tools > options" dialog box will be helpful for me to make the these changes easily. I just don't know how to make those changes permanent throughout the execution of the app and how to call those settings from each of the forms. Thanks for you help. :)

        1 Reply Last reply
        0
        • G gonad

          I'm new to c# and programming altogether, so can you help me figure out the best way to do this? i have a "Tools > Options" dialog box where a user can put in the db settings to connect to a database (IP, username, password, etc). once a user submits these settings, i want the whole application to reference these settings for the database connection. i have about 5 forms on this small app that connect to a db, so i don't want to statically assign these settings per form. can i (or should I) use a struct and define the users settings as constants? if so, how can i call the struct from each form? Thanks for your help.

          N Offline
          N Offline
          Nathan Blomquist
          wrote on last edited by
          #4

          The best way to make the settings global to your application is by having a class that is visible to the whole app. Something like: public class MySettings { public static string MyDbConnectionString; public static string SomeOtherSettings; } Notice the keyword static infront. This means that you do not need an instance of the class to reference those members. You would access them like: MySettings.MyDbConnectionString = ... So you just load your settings from a file at the start of your app, and then just set them again from the options dialog. Hope this helps and gives you a start, Nathan --------------------------- Hmmm... what's a signature?

          G L 2 Replies Last reply
          0
          • N Nathan Blomquist

            The best way to make the settings global to your application is by having a class that is visible to the whole app. Something like: public class MySettings { public static string MyDbConnectionString; public static string SomeOtherSettings; } Notice the keyword static infront. This means that you do not need an instance of the class to reference those members. You would access them like: MySettings.MyDbConnectionString = ... So you just load your settings from a file at the start of your app, and then just set them again from the options dialog. Hope this helps and gives you a start, Nathan --------------------------- Hmmm... what's a signature?

            G Offline
            G Offline
            gonad
            wrote on last edited by
            #5

            Thank you for your help. that's exactly what i needed to know, and it works like i expected. :-O

            N 1 Reply Last reply
            0
            • N Nathan Blomquist

              The best way to make the settings global to your application is by having a class that is visible to the whole app. Something like: public class MySettings { public static string MyDbConnectionString; public static string SomeOtherSettings; } Notice the keyword static infront. This means that you do not need an instance of the class to reference those members. You would access them like: MySettings.MyDbConnectionString = ... So you just load your settings from a file at the start of your app, and then just set them again from the options dialog. Hope this helps and gives you a start, Nathan --------------------------- Hmmm... what's a signature?

              L Offline
              L Offline
              leppie
              wrote on last edited by
              #6

              Its generally better to place things like your own SQL/Access connectiong string in the machine.config file. That data will be available to all the applications running on the machine via Dynamic properties. leppie::AllocCPArticle(Generic DFA State Machine for .NET);

              J 1 Reply Last reply
              0
              • L leppie

                Its generally better to place things like your own SQL/Access connectiong string in the machine.config file. That data will be available to all the applications running on the machine via Dynamic properties. leppie::AllocCPArticle(Generic DFA State Machine for .NET);

                J Offline
                J Offline
                James T Johnson
                wrote on last edited by
                #7

                leppie wrote: Its generally better to place things like your own SQL/Access connectiong string in the machine.config file. I would advise against this for a couple reasons:

                1. Your app will break if you move to a new version of the framework, but forget to update the machine.config for that version of the framework. Or worse, another application does the same thing, but storing a different connection string.
                2. You are taking something that should be local, and making it global. You wouldn't want your connection strings entered into another application so you should keep them local to the apps that need it.

                Instead you should store the read-only setting in the app.config file. If you expect to make more applications using the same connection string then you would be better off creating your own settings class and storing that in the user's application directory, or the common application directory if the settings should apply to everyone. James "I despise the city and much prefer being where a traffic jam means a line-up at McDonald's" Me when telling a friend why I wouldn't want to live with him

                L 1 Reply Last reply
                0
                • G gonad

                  Thank you for your help. that's exactly what i needed to know, and it works like i expected. :-O

                  N Offline
                  N Offline
                  Nathan Blomquist
                  wrote on last edited by
                  #8

                  Glad that I could be of help :) --------------------------- Hmmm... what's a signature?

                  1 Reply Last reply
                  0
                  • J James T Johnson

                    leppie wrote: Its generally better to place things like your own SQL/Access connectiong string in the machine.config file. I would advise against this for a couple reasons:

                    1. Your app will break if you move to a new version of the framework, but forget to update the machine.config for that version of the framework. Or worse, another application does the same thing, but storing a different connection string.
                    2. You are taking something that should be local, and making it global. You wouldn't want your connection strings entered into another application so you should keep them local to the apps that need it.

                    Instead you should store the read-only setting in the app.config file. If you expect to make more applications using the same connection string then you would be better off creating your own settings class and storing that in the user's application directory, or the common application directory if the settings should apply to everyone. James "I despise the city and much prefer being where a traffic jam means a line-up at McDonald's" Me when telling a friend why I wouldn't want to live with him

                    L Offline
                    L Offline
                    leppie
                    wrote on last edited by
                    #9

                    James T. Johnson wrote: You are taking something that should be local, and making it global. You wouldn't want your connection strings entered into another application so you should keep them local to the apps that need it. I realise that! I was thinking more in a development enviroment, rather than production. Obviously on deployment, an app.config file is created to carry the setting. That makes sense doesnt it? leppie::AllocCPArticle(Generic DFA State Machine for .NET);

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups