System.NullReferenceException: 未将对象引用设置到对象的实例。
我先将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();
}
}
}