Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Modify Web.Config File Programmatically

Status
Not open for further replies.

Neld

Programmer
Jul 2, 2012
5
CA
Hi,

I'm new to C Sharp and have been trying these peices of codes below and i get the subject error above. I'm trying to modify a web.config.

Please kindly help me point out which line causes the error.

*************************
Target Framwork: .NET 3.5
C Sharp
Visual Studio 2010
*************************

Thanks in advance.

Nel

Sample appSettings Web.Config File
Code:
  <appSettings>
    <add key="Under Maintenance" value="N"/>
  </appSettings>

ASPX Page
Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Admin.aspx.cs" Inherits="OCPSearchCSharp.Admin" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]

<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" 
            Text="Update Web.Config" />
    
    </div>
    </form>
</body>
</html>

Code Behind
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Configuration;
using System.Configuration.Assemblies;
using System.Configuration.Internal;
using System.Configuration.Provider;

namespace OCPSearchCSharp
{
    public partial class Admin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

            //XmlElement element = xmlDoc.DocumentElement;

            foreach (XmlElement element in xmlDoc.DocumentElement)
            {
                if (element.Name.Equals("appSettings"))
                {
                    foreach (XmlNode node in element.ChildNodes)
                    {
                        if (node.Attributes[0].Value.Equals("Under Maintenance"))
                        {
                            node.Attributes[1].Value = "Yes";
                            Response.Write("<BR><BR>Hello World<BR><BR>");
                        }
                    }
                }
            }

            xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

            System.Configuration.ConfigurationManager.RefreshSection("appSettings");
        }
    }
}

Error Message in Line 36
Code:
Server Error in '/' Application.
--------------------------------------------------------------------------------

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.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 


Line 34:                     foreach (XmlNode node in element.ChildNodes)
Line 35:                     {
Line 36:                         if (node.Attributes[0].Value.Equals("Under Maintenance"))
Line 37:                         {
Line 38:                             node.Attributes[1].Value = "Yes";
 

Source File: C:\Data\OCPSearchCSharp\OCPSearchCSharp\OCPSearchCSharp\Admin.aspx.cs    Line: 36 

Stack Trace: 


[NullReferenceException: Object reference not set to an instance of an object.]
   OCPSearchCSharp.Admin.Button1_Click(Object sender, EventArgs e) in C:\Data\OCPSearchCSharp\OCPSearchCSharp\OCPSearchCSharp\Admin.aspx.cs:36
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565
 
Use the debugger. Trace through the code until you hit the line that causes the error.
 
I would also recommend reading up on using a Try Catch Finally block. This way you can also catch and handle errors without totally crashing...

Walt
IT Consulting for Robert Half
 
Hi Guys,

It is this line below, which is line 36.The error says,

"if (node.Attributes[0].Value.Equals("Under Maintenance"))"

The error says:

"Object reference not set to an instance of an object."


Thanks

Nel
 
Hi Guys,

It is this line below, which is line 36.The error says,

"if (node.Attributes[0].Value.Equals("Under Maintenance"))"

The error says:

"Object reference not set to an instance of an object."

How do i resolve the error message?

Thanks

Nel

 
First check if the node has attributes before testing the value
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top