English 中文(简体)
在将案文贴在标签上时,我收到反对意见。
原标题:When assigning text to a label I get an Object Reference Not Set To And Instance of an Object

我建立了包含标签的简单习俗控制。 在习惯控制背后的代码中,我试图确定标签案文,但我没有提及错误。 任何想法?

protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection connection = new SqlConnection(GetConnectionString());
        connection.Open();
        SqlCommand cmd = new SqlCommand("DisplayCustomerReviews", connection);
        cmd.CommandType = CommandType.StoredProcedure;


        XmlReader reader = cmd.ExecuteXmlReader();


        //while (reader.Read())
        while (!reader.EOF)
        {
            reader.ReadToFollowing("review");

            reader.MoveToAttribute("Name");
            string Name = reader.Value;

            reader.MoveToAttribute("Message");
            string Message = reader.Value;

            reader.MoveToAttribute("Rating");
            string Rating = reader.Value;

            reader.MoveToAttribute("Date");
            string Date = reader.Value;

            reader.MoveToAttribute("Time");
            string Time = reader.Value;

            CreateReviewPanel(Name, Message, Rating, Date, Time);

        }

        reader.Close();
        connection.Close();
    }

    private string GetConnectionString()
    {
        return ConfigurationManager.AppSettings["SQLConn"];
    }

    private void CreateReviewPanel(string Name, 
                                   string Message, 
                                   string Rating, 
                                   string Date, 
                                   string Time)
    {

        // need to check against nulls or empty strings to avoid 
        // extra reviewPanel being created at end of XML read. 
        if (Name != "") 
        {

            Guid panelID = Guid.NewGuid();

            Panel reviewPanel = new Panel();
            reviewPanel.ID = panelID.ToString();

            nameLabel.Text = Name;

            messageLabel.Text = Message;

            dateLabel.Text = Date;

            timeLabel.Text = Time;

            switch (Rating)
            {
                case "1":
                    ratingImage.ImageUrl = "~/images/one-star.gif";
                    break;
                case "2":
                    ratingImage.ImageUrl = "~/images/two-stars.gif";
                    break;
                case "3":
                    ratingImage.ImageUrl = "~/images/three-stars.gif";
                    break;
                case "4":
                    ratingImage.ImageUrl = "~/images/four-stars.gif";
                    break;
                case "5":
                    ratingImage.ImageUrl = "~/images/five-stars.gif";
                    break;
            }
        }
    }
}

它说的是劳工。 案文 = 姓名为发生错误之处,随后是所有其他转让。

问题回答

是否称“Label”是网页上控制的一个标志? 如果没有,你需要:

Label nameLabel = this.lblSomeControlID;

为了提及任何有效内容。

我发现这个问题。 这是在我缺席时的法典。 作为正试图 gr取每一条习俗控制权的px.cs,随着这一控制的建立,加之作为违约页另一小组的控制。 问题在于,XMLreader使一人再次出院,返回无效。 习惯控制法背后不能归于无效。 我赞赏大家的评论......这些评论可能已经提出。

法典应当这样看,或许:

    private void CreateReviewPanel(string Name, string Message, string Rating, string Date, string Time)
    {

        if (Name != "") //need to check against nulls or empty strings to avoid extra reviewPanel being created at end of XML read.
        {

            Guid panelID = Guid.NewGuid();

            ReviewPanel reviewPanel = new ReviewPanel();
            reviewPanel.ID = panelID.ToString();

            reviewPanel.nameLabel.Text = Name;

            reviewPanel.messageLabel.Text = Message;

            reviewPanel.dateLabel.Text = Date;

            reviewPanel.timeLabel.Text = Time;

            switch (Rating)
            {
                case "1":
                    reviewPanel.ratingImage.ImageUrl = "~/images/one-star.gif";
                    break;
                case "2":
                    reviewPanel.ratingImage.ImageUrl = "~/images/two-stars.gif";
                    break;
                case "3":
                    reviewPanel.ratingImage.ImageUrl = "~/images/three-stars.gif";
                    break;
                case "4":
                    reviewPanel.ratingImage.ImageUrl = "~/images/four-stars.gif";
                    break;
                case "5":
                    reviewPanel.ratingImage.ImageUrl = "~/images/five-stars.gif";
                    break;
            }
        }
    }
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签