asp.net联库问题

来源:百度知道 编辑:UC知道 时间:2024/06/19 03:16:13
System.NullReferenceException: 未将对象引用设置到对象的实例。 在 _Default.Button1_Click(Object sender, EventArgs e) 位置 c:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\WebSite1\Default.aspx.cs:行号 24
protected void Button1_Click(object sender, EventArgs e)
{
try
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = ConfigurationManager.ConnectionStrings["user id=sa;password=sa;initial catalog=leilei;data source=127.0.0.1;Connect TimeOut=30"].ConnectionString;
SqlCommand cmd = new SqlCommand("select id,name from shoppingcar order by id,name desc" ,conn);
conn.Open();
SqlDataReader r = cmd.ExecuteReader(CommandBehavior.CloseConnection);
ListBox1.DataSource = r;
ListBox1.DataTextField = "id";
ListBox1.DataValueField = "name";

代码呢,这个表示空引用,有个值是null,你调试一下就看到了
你看一下24行,SqlDataReader r = cmd.ExecuteReader(CommandBehavior.CloseConnection);
这行吗,是的话说明CommandBehavior.CloseConnection是null值,你单步调试一下