public partial class _Default : System.Web.UI.Page 
{    protected void Page_Load(object sender, EventArgs e)
    {
        string conStr = ConfigurationManager.ConnectionStrings["mysqlconnection"].ConnectionString;
        SqlConnection connection = new SqlConnection(conStr);
        connection.Open();
        string selectionStr = "SELECT * FROM Shippers";
        SqlCommand command = new SqlCommand();
        command.Connection = connection;
        command.CommandText = selectionStr;
        SqlDataReader dataReader = command.ExecuteReader();        while (dataReader.Read())
        {
            ListBox1.Items.Add(new ListItem(
                        dataReader[0] + " -- " + dataReader[1] + " -- " + dataReader[2],dataReader[0].ToString()));
        }
        dataReader.Close();        selectionStr = "SELECT * FROM Products";
        command.CommandText = selectionStr;
        dataReader = command.ExecuteReader();        ListBox2.DataSource = dataReader;
        ListBox2.DataTextField = "ProductName";
        ListBox2.DataValueField = "ProductID";
        ListBox2.DataBind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {        TextBox1.Text = ListBox2.SelectedItem.Text;
    }为什么 TextBox1.Text = ListBox2.SelectedItem.Text 这行代码在执行时出错?!
Object reference not set to an instance of an object.
谢谢解答!

解决方案 »

  1.   

    selectionStr = "SELECT * FROM Products";
            command.CommandText = selectionStr;
            dataReader = command.ExecuteReader();        ListBox2.DataSource = dataReader;
            ListBox2.DataTextField = "ProductName";
            ListBox2.DataValueField = "ProductID";
            ListBox2.DataBind();
    没有设置Connection 
    ListBox2为Null吧
      

  2.   

    Object reference not set to an instance of an object
    没有实例化
    单步跟踪看看
      

  3.   

    command.Connection = connection;
            command.CommandText = selectionStr;
            dataReader = command.ExecuteReader();
            ListBox2.DataSource = dataReader;
            ListBox2.DataTextField = "ProductID";
            ListBox2.DataValueField = "ProductName";
            ListBox2.DataBind();加上connection还是一样问题啊!
      

  4.   

    把代码TextBox1.Text = ListBox2.SelectedItem.Text;
    加到page_load中看看是否可以赋值。别点button
      

  5.   

    哥哥,这个问题太简单了,你跟踪一下就可以发现,SelectedItem是一个DataRowView,你需要DataRowView drv=ListBox2.SelectedItem;TextBox1.Text =drv["COL"].tostring();
      

  6.   

    page_load里也是不行.
    DotNET3, 看MSDN里的, 人家都是这用就可以, 究竟我的代码哪里与她不同?!
    <%@ Page Language="C#" AutoEventWireup="True" %>
    <%@ Import Namespace="System.Data" %>
     <html>
     <script language="C#" runat="server">
        ICollection CreateDataSource() {
           DataTable dt = new DataTable();
           DataRow dr;
           dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
           dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
           dt.Columns.Add(new DataColumn("DateTimeValue", typeof(DateTime)));
           dt.Columns.Add(new DataColumn("BoolValue", typeof(bool)));
           dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double)));
           for (int i = 0; i < 9; i++) {
              dr = dt.NewRow();
              dr[0] = i;
              dr[1] = "Item " + i.ToString();
              dr[2] = DateTime.Now;
              dr[3] = (i % 2 != 0) ? true : false;
              dr[4] = 1.23 * (i+1);
              dt.Rows.Add(dr);
           }
           DataView dv = new DataView(dt);
           return dv;
        }
        void Page_Load(Object sender, EventArgs e) {
           if (!IsPostBack) {
              RadioButtonList1.DataSource = CreateDataSource();
              RadioButtonList1.DataTextField="StringValue";
              RadioButtonList1.DataValueField="CurrencyValue";
              RadioButtonList1.DataBind();
           }
        }
        void Index_Changed(Object sender, EventArgs e) {
    //注意下面的三行代码--------------------------------------------------------------
           Label1.Text = "You selected " + RadioButtonList1.SelectedItem.Text +
                         " with a value of $" + RadioButtonList1.SelectedItem.Value +
                         ".";    }
     </script>
     <body>
        <form runat=server>
           <asp:RadioButtonList id="RadioButtonList1" 
                OnSelectedIndexChanged="Index_Changed"
                AutoPostBack="true"
               runat="server"/>
           <br>
           <asp:Label id="Label1" runat="server"/>
        </form>
     </body>
     </html>