0

我正在寻找一个聪明的向导来指引我正确的方向。我正在使用 XML 和 C# 制作一个模拟漫画数据库。除了我的编辑页面外,一切都非常出色。在我的本地主机上,我得到一个对象引用错误,在我的主机上,我得到一个运行时错误。

直播页面在这里:(登录用户名是Administrator,密码是adminpass!)
http://www.lmabee.com/test/XMLProject/Admin/comicsEdit.aspx?id=2

我的代码如下:

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>

我的代码隐藏:

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";
}
}

我受够了,我现在从头开始写这个文件 6 次。有人愿意为这个悲伤的树液阐明这个问题吗?我会真的,真的很感激。在过去的 48 天里,我目前正在为 3 小时的睡眠工作,所以老实说,我会亲吻你的脚。

最好的!劳拉

4

2 回答 2

0

myXmlDocument调用时可能未加载问题btnEdit_Click

string file = Path.Combine(Request.PhysicalApplicationPath, "comic.xml");
if(File.Exists(file))
{
    myXmlDocument.Load(file);
    XmlNode rootNode = myXmlDocument.DocumentElement;
    if(rootNode !=null && rootNode.ChildNodes.Count> intComicID)
    {
       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;
            }
    }

}

在您的 btnEdit_Click 方法中

public void btnEdit_Click(object sender, EventArgs e)
{
    if(myXmlDocument != null && GrabComic != null && GrabComic.ChildNodes.Count >3){

        GrabComic.ChildNodes[0].InnerText = txtTitle.Text; 
        GrabComic.ChildNodes[1].InnerText = txtIssue.Text;
        GrabComic.ChildNodes[2].InnerText = txtDesc.Text;
        myXmlDocument.Save(Path.Combine(Request.PhysicalApplicationPath, "comic.xml"));

        lblMessage.Text = "You have successfully updated the Database";
    }
}
于 2012-04-19T08:40:03.610 回答
0

为什么你只是看不到堆栈跟踪?我猜您的 ChildNodes[0] 之一或全部为空。或者只是在您的方法上设置一个断点并确保您的对象不为空。

于 2012-04-19T08:41:06.967 回答