System.NullReferenceException: 未将对象引用设置到对象的实例。

mengfly 2005-03-09 04:53:31
我先将Northwind中的Employees绑定到RadioButtonList中 成功
但是为什么将RadioButtonList的数据用Label1显示时总提示错误:
System.NullReferenceException: 未将对象引用设置到对象的实例。
{
行 82: //Response.Write((RadioButtonList1.SelectedValue).GetType());
行 83: Label1.Text=RadioButtonList1.SelectedItem.Value.ToString();
行 84:
行 85: }


这是个什么错误?是不是数据源没有绑定好?
请指教 谢谢!

RadioButtonList绑定语句:
objConnection.Open();
RadioButtonList1.DataSource=objCommand.ExecuteReader();
RadioButtonList1.DataTextField="LastName";
RadioButtonList1.DataValueField="EmployeeID";
RadioButtonList1.DataBind();
objConnection.Close();



源代码:
RadioButtonList.aspx
<%@ Page language="c#" Codebehind="radiobuttonlist.aspx.cs" AutoEventWireup="false" Inherits="test1.radiobuttonlist" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>radiobuttonlist</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="宋体"></FONT><FONT face="宋体"></FONT><FONT face="宋体"></FONT><FONT face="宋体">
</FONT>
<asp:DataGrid id="DataGrid1" style="Z-INDEX: 102; LEFT: 16px; POSITION: absolute; TOP: 200px"
runat="server"></asp:DataGrid>
<asp:Label id="Label1" style="Z-INDEX: 104; LEFT: 80px; POSITION: absolute; TOP: 160px" runat="server"></asp:Label>
<asp:Button id="Button1" style="Z-INDEX: 103; LEFT: 16px; POSITION: absolute; TOP: 160px" runat="server"
Text="提交"></asp:Button>
<br>
<FONT face="宋体">Using Radio button </FONT>
<asp:RadioButtonList id="RadioButtonList1" style="Z-INDEX: 101; LEFT: 24px; POSITION: absolute; TOP: 80px"
runat="server" RepeatColumns="3" RepeatDirection="Horizontal" AutoPostBack="True"></asp:RadioButtonList>
</form>
</body>
</HTML>

RadioButtonList.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace test1
{
/// <summary>
/// radiobuttonlist 的摘要说明。
/// </summary>
public class radiobuttonlist : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.RadioButtonList RadioButtonList1;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
string strConnection="server=localhost;uid=sa;password=;database=Northwind";
string strCommand="select LastName,EmployeeID from Employees order by EmployeeID";

SqlConnection objConnection=new SqlConnection(strConnection);
SqlCommand objCommand=new SqlCommand(strCommand,objConnection);

objConnection.Open();
RadioButtonList1.DataSource=objCommand.ExecuteReader();
RadioButtonList1.DataTextField="LastName";
RadioButtonList1.DataValueField="EmployeeID";
RadioButtonList1.DataBind();
objConnection.Close();
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.RadioButtonList1.SelectedIndexChanged += new System.EventHandler(this.RadioButtonList1_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void RadioButtonList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
/**/string strConnection="server=localhost;database=Northwind;uid=sa;password=";
string strCommand="select * from Employees";// where EmployeeID="+RadioButtonList1.SelectedValue.ToString();


SqlConnection objConnection=new SqlConnection(strConnection);
SqlCommand objCommand=new SqlCommand(strCommand,objConnection);

objConnection.Open();
DataGrid1.DataSource=objCommand.ExecuteReader();
DataGrid1.DataBind();
objConnection.Close();
}

private void Button1_Click(object sender, System.EventArgs e)
{
//Response.Write((RadioButtonList1.SelectedValue).GetType());
Label1.Text=RadioButtonList1.SelectedItem.Value.ToString();

}
}
}
...全文
203 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
SeeSunSet 2005-03-10
  • 打赏
  • 举报
回复
Label1.Text=RadioButtonList1.Items(RadioButtonList1.SelectedIndex).Value.ToString();

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧