Reading from web.config

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • arun1985
    New Member
    • Jul 2007
    • 8

    #1

    Reading from web.config

    Is it correct to read from web.config file in the following way? i am using asp.net 2.0 VS 2005.I get the following error

    Object reference not set to an instance of an object.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.NullRefe renceException: Object reference not set to an instance of an object.

    Source Error:

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

    Stack Trace:


    [NullReferenceEx ception: Object reference not set to an instance of an object.]
    anmar.SharpWebM ail.UI.Login.Lo gin_Click(Objec t sender, EventArgs args) in login.cs:75
    System.EventHan dler.Invoke(Obj ect sender, EventArgs e) +0
    System.Web.UI.W ebControls.Butt on.OnClick(Even tArgs e) +105
    System.Web.UI.W ebControls.Butt on.RaisePostBac kEvent(String eventArgument) +107
    System.Web.UI.W ebControls.Butt on.System.Web.U I.IPostBackEven tHandler.RaiseP ostBackEvent(St ring eventArgument) +7
    System.Web.UI.P age.RaisePostBa ckEvent(IPostBa ckEventHandler sourceControl, String eventArgument) +11
    System.Web.UI.P age.RaisePostBa ckEvent(NameVal ueCollection postData) +33
    System.Web.UI.P age.ProcessRequ estMain(Boolean includeStagesBe foreAsyncPoint, Boolean includeStagesAf terAsyncPoint) +5102


    The point where the error is shown is

    int login_mode = (int)Applicatio n["sharpwebma il/login/mode"];

    Are application variables available in asp.net 2.0
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Values stored in web.config are generally accessed like this:

    Code:
    Code:
    private static AppSettingsReader ap = new AppSettingsReader();
    public static int SitesPerGrouping
        {
            get
            {
                return (int)ap.GetValue("SitesPerGrouping", typeof(int));
            }
        }
    web.config
    Code:
    <configuration>
    	<appSettings>
    		<add key="SitesPerGrouping" value="500"/>
    	</appSettings>
    </configuration>

    Comment

    • arun1985
      New Member
      • Jul 2007
      • 8

      #3
      Originally posted by Plater
      Values stored in web.config are generally accessed like this:

      Code:
      Code:
      private static AppSettingsReader ap = new AppSettingsReader();
      public static int SitesPerGrouping
          {
              get
              {
                  return (int)ap.GetValue("SitesPerGrouping", typeof(int));
              }
          }
      web.config
      Code:
      <configuration>
      	<appSettings>
      		<add key="SitesPerGrouping" value="500"/>
      	</appSettings>
      </configuration>
      <?xml version="1.0" encoding="UTF-8" ?>

      <configuratio n>
      <configSections >
      <section name="log4net" type="System.Co nfiguration.Ign oreSectionHandl er" />
      <section name="sharpwebm ail" type="anmar.Sha rpWebMail.Sharp WebMailConfigHa ndler, SharpWebMail" />
      </configSections>
      <sharpwebmail >
      <!--
      The <general> section is used to configure global options
      default_lang: Default localization language
      title: Title displayed in every page.
      -->
      <general
      default_lang="e n"
      title = "[general title] configuration setting"
      />
      <!--
      The <login> section is used to configure login screen options

      title: Title displayed in login screen

      mode:
      1-> Full email required for login
      2-> Any username is allowed
      3-> Any username is allowed but the append
      string is added to supplied username

      append: text to append to user supplied username

      serverselection :
      auto -> The server is selected using the configured regular expressions.
      manual -> The server is selected by the user.

      enablequerystri nglogin: Login credentials can be given in the querystring of
      the login page. The provided credentials must comply with the same
      restrictions as when provided manually. The querystring items used are
      username and password. Possible values:
      true-> enabled
      false-> disabled

      Note: Be careful when using this option as the passwords will be seen in
      clear text easily (for example in the browser history).
      Example: http://localhost/login.aspx?user name=anmar@dev. anmar.eu.org&pa ssword=testpass
      -->
      <login
      title="[login title] configuration setting"
      mode="1"
      append=""
      serverselection ="auto"
      enablequerystri nglogin="false"
      />

      If i have a web.config section like this and i have to read the value of mode in login section then how to read the value?

      Comment

      Working...