I am looking for a wise wizard to point me in the right direction. I am making a mock comic database using XML and C#. Everything works splendidly except for my edit page. On my localhost I get an Object Reference error, on my host I get a runtime error.
The live page is here: (login username is Administrator and password is adminpass!)
http://www.lmabee.com/test/XMLProject/Admin/comicsEdit.aspx?id=2
My code is as follows:
comicsEdit.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/Admin/adminMaster.master" AutoEventWireup="true" CodeFile="comicsEdit.aspx.cs" Inherits="_Default" %>
<asp:Content ID="Content2" ContentPlaceHolderID="center" Runat="Server">
<asp:Label ID="lblOutput" runat="server" Visible="true" />
<asp:Panel ID="pnlEdit" runat="server" Visible="false">
<h1>
Edit Comic Database
</h1>
<div id="divMessage">
<asp:Label ID="lblMessage" runat="server"></asp:Label></div>
<table>
<tr>
<td >
<asp:Label ID="lblTitle" runat="server" Text="Label">Title:</asp:Label>
</td>
<td>
<asp:TextBox ID="txtTitle" runat="server" Width="200px"></asp:TextBox>
<asp:RequiredFieldValidator ID="val1" runat="server" ErrorMessage="Please enter a title"
Display="Dynamic" ControlToValidate="txtTitle"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td >
<asp:Label ID="lblIssue" runat="server" Text="Label">Issue:</asp:Label>
</td>
<td>
<asp:TextBox ID="txtIssue" runat="server" Width="200px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Please enter a Issue"
Display="Dynamic" ControlToValidate="txtIssue"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td >
<asp:Label ID="lblDesc" runat="server" Text="Label">Description:</asp:Label>
</td>
<td>
<asp:TextBox ID="txtDesc" runat="server" Width="200px" Height="200px" TextMode="MultiLine"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="Please enter a Description"
Display="Dynamic" ControlToValidate="txtDesc"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnEdit" OnClick="btnEdit_Click" runat="server" Text="Update Comic">
</asp:Button>
</td>
</tr>
</table>
</asp:Panel>
</asp:Content>
And my codebehind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
public partial class _Default : System.Web.UI.Page
{
int intComicID;
XmlDocument myXmlDocument = new XmlDocument();
XmlNode rootNode;
XmlNode GrabComic;
public void Page_Load(object Src, EventArgs E)
{
if (!Page.IsPostBack)
{
intComicID = Convert.ToInt32(Request.QueryString["id"]);
if (intComicID == null || intComicID.ToString() == "0")
{// doesn't include id parameter
lblOutput.Visible = true;
lblOutput.Text = "no item selected.";
}
else
{// id has value
myXmlDocument.Load(Request.PhysicalApplicationPath + @"comic.xml");
XmlNode rootNode = myXmlDocument.DocumentElement;
GrabComic = rootNode.ChildNodes[intComicID - 1];
if (GrabComic == null)
{// invalid id
lblOutput.Visible = true;
lblOutput.Text = "item doesn't exist.";
}
else
{// valid id
pnlEdit.Visible = true;
txtTitle.Text = GrabComic.ChildNodes[0].InnerText;
txtIssue.Text = GrabComic.ChildNodes[1].InnerText;
txtDesc.Text = GrabComic.ChildNodes[2].InnerText;
}
}
}
}
public void btnEdit_Click(object sender, EventArgs e)
{
GrabComic.ChildNodes[0].InnerText = txtTitle.Text;
GrabComic.ChildNodes[1].InnerText = txtIssue.Text;
GrabComic.ChildNodes[2].InnerText = txtDesc.Text;
myXmlDocument.Save(Request.PhysicalApplicationPath + @"comic.xml");
lblMessage.Text = "You have successfully updated the Database";
}
}
I was so fed up, I literally started from scratch on this file 6 times now. Would anyone be willing to shed some light on this issue for this sad sap?
I would truly, truly be grateful. I’m currently working on 3 hours sleep for the last 48, so I honestly would kiss your feet.
Best!
Laura
Problem may be
myXmlDocumentnot loaded when you callbtnEdit_Clickin your btnEdit_Click method