我构建了一个包含标签的简单自定义控件。在自定义控件的代码隐藏中,我试图设置标签的文本,但我得到一个空引用错误。有什么想法吗?
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;
}
}
}
}
其中显示'nameLabel.Text = Name'是发生错误的位置,随后是所有其他分配。
答案 0 :(得分:0)
nameLabel是页面上控件的ID吗?如果没有,您需要这样做:
Label nameLabel = this.lblSomeControlID;
为了让nameLabel引用任何有效的内容。
答案 1 :(得分:0)
我发现了问题。我的default.aspx.cs中的代码试图在创建时抓取每个自定义控件,并将其作为控件添加到默认页面上的另一个面板。问题是当XMLreader再生一个循环并返回null时。自定义控件代码隐藏不能分配空值。我感谢所有人的评论......尽管这可能是错综复杂的。
答案 2 :(得分:-1)
您应该在代码中添加try ... catch例程,直到您完成整理。
由于您使用文本值,因此有许多地方可能会出现空引用异常。
protected void Page_Load(object sender, EventArgs e) {
string conn = GetConnectionString();
if (String.IsNullOrEmpty(conn)) {
Console.WriteLine("No Connection String!");
return;
}
try {
SqlConnection connection = new SqlConnection(conn);
connection.Open();
SqlCommand cmd = new SqlCommand("DisplayCustomerReviews", connection);
cmd.CommandType = CommandType.StoredProcedure;
XmlReader reader = cmd.ExecuteXmlReader();
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();
} catch (Exception err) {
Console.WriteLine("Possible Problems:");
Console.WriteLine("DisplayCustomerReviews was not found or spelled incorrectly");
Console.WriteLine("One or more of the attributes listed above do not exist.");
Console.WriteLine(err.Message);
}
}
private string GetConnectionString() {
string connStr = null;
try {
connStr = ConfigurationManager.AppSettings["SQLConn"];
} catch (Exception err) {
Console.WriteLine("AppSettings is incorrect or does not contain SQLConn");
Console.WriteLine(err.Message);
}
return connStr;
}
private void CreateReviewPanel(string Name, string Message, string Rating, string Date, string Time) {
if (!String.IsNullOrEmpty(Name)) //need to check against nulls or empty strings to avoid extra reviewPanel being created at end of XML read.
{
Guid panelID = Guid.NewGuid();
Panel reviewPanel = new Panel();
try {
reviewPanel.ID = panelID.ToString();
} catch (Exception) {
Console.WriteLine("Panel Object does not contain an ID property.");
Console.WriteLine(err.Message);
}
nameLabel.Text = Name;
messageLabel.Text = Message;
dateLabel.Text = Date;
timeLabel.Text = Time;
try {
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;
}
Console.WriteLine("File Check: ", ratingImage.ImageUrl);
Console.WriteLine(File.Exists(ratingImage.ImageUrl));
} catch (Exception err) {
Console.WriteLine("This control type does not contain an ImageUrl property");
Console.WriteLine(err.Message);
}
}
}
答案 3 :(得分:-1)
代码看起来应该是这样的,可能是:
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;
}
}
}
}