以下这段程序编绎时,会出现这个问题,肯请大家帮忙看看...谢谢咯~~~
在           string storeID;
           storeID = StoresComboBox.SelectedValue.ToString();
这里抛出异常
"未处理的“System.NullReferenceException”类型的异常出现在 PopulatingAndUpdateingDataSets.exe 中。其他信息: 未将对象引用设置到对象的实例。"大家试着帮忙运行程序看看问题出在哪了.....private void Form1_Load(object sender, System.EventArgs e)
{
//TODO 1: Declare and instantiate a SqlDataAdapter, SqlCommand, and a DataTable
System.Data.SqlClient.SqlDataAdapter storesSQLDataAdapter = new System.Data.SqlClient.SqlDataAdapter();
System.Data.SqlClient.SqlCommand storesSelectSQLCommand = new System.Data.SqlClient.SqlCommand();
DataTable storesTable = new DataTable("Stores");
           
//TODO 2: Configure the SqlCommand object and add it to the SQLDataAdapter
storesSelectSQLCommand.CommandType=CommandType.Text;
    storesSelectSQLCommand.CommandText="SELECT stor_id, stor_name FORM stores";
storesSelectSQLCommand.Connection=sqlConnection1;
storesSQLDataAdapter.SelectCommand=storesSelectSQLCommand;     try
{
//TODO 3: Fill the table using the SQLDataAdapter and add it to the existing DataSet
   storesSQLDataAdapter.Fill(storesTable);
   StoreSalesDataSet1.Tables.Add(storesTable);

}
catch (Exception xcp)
{
MessageBox.Show(xcp.ToString());
}
//
StoresComboBox.DataSource = StoreSalesDataSet1.Tables["Stores"];
//The DisplayMemebr property specifies the field that will be displayed to the user
StoresComboBox.DisplayMember = "stor_name";
//The ValueMember property specifies an alternate field 
StoresComboBox.ValueMember = "stor_id";
UpdateTable();
//Create an event handler for the ComboBox's SelectedIndexChanged event
//after it is bound to the data. This will prevent the event from being 
//fired each time an item is added to the ComboBox.
this.StoresComboBox.SelectedIndexChanged += new System.EventHandler(this.StoresComboBox_SelectedIndexChanged); } private void UpdateTable()
{
string storeID;
storeID = StoresComboBox.SelectedValue.ToString();

//TODO 4: Assign storeID to the paremeters collection of SelectCommand 
//SelectCommand is one of the SQLCommand objects generated by the Data
//Adapter Configuration Wizard
SalesSQLDataAdapter.SelectCommand.Parameters["@stor_id"].Value = storeID ;

//TODO 5: Clear the current sales table of any existing data
//Because this is a typed DatSet you access the sales table as  a property of the dataset
//instead of using:
//StoreSalesDataSet1.Tables["sales"].Clear()
//StoreSalesDataSet1.sales.Clear();
try
{
//TODO 6: Use the DataAdapter to fill the sales table of the StoreSalesDataSet1 DataSet
SalesSQLDataAdapter.Fill(StoreSalesDataSet1.sales);

}
catch (Exception xcp)
{
MessageBox.Show(xcp.ToString());
            }
} private void UpdateButton_Click(object sender, System.EventArgs e)
{

try
{
//TODO 7: Use the Update method of the DataAdapter and pass it a copy of the StoreSalesDataSet1 DataSet
SalesSQLDataAdapter.Update(StoreSalesDataSet1);
}
catch (Exception xcp)
{
MessageBox.Show(xcp.ToString());
}
}

解决方案 »

  1.   

    谢谢楼上的回复...
    他说我的对象是空的...是指的这句private void UpdateTable()
    {
    string storeID;
    storeID = StoresComboBox.SelectedValue.ToString();
              ...
    }但是,storeID = StoresComboBox.SelectedValue.ToString();这样使用应该是没问题的吧..
      

  2.   

    將storeID = StoresComboBox.SelectedValue.ToString();
    改成 storeID = Convert.ToString(StoresComboBox.SelectedValue);
      

  3.   

    先查查StoreSalesDataSet1.Tables["Stores"];这个DataSet里有没有数据
    如果没有数据
    StoresComboBox.SelectedValue.ToString();
    这一句就是错的
      

  4.   

    应该是StoresComboBox.SelectedValue.ToString()的值为空,可能是绑定StoresComboBox的时候出错了,断点调试一下吧
      

  5.   

    storeID = StoresComboBox.SelectedValue.ToString();
    是正确的,
    可能是你在程序中根本没有选择StoresComboBox 
    或者StoresComboBox里面根本没有填充列表值未将对象引用设置到对象的实例 嘛~
      

  6.   


    "先查查StoreSalesDataSet1.Tables["Stores"];这个DataSet里有没有数据
    如果没有数据
    StoresComboBox.SelectedValue.ToString();
    这一句就是错的"StoreSalesDataSet1这个数据集中,是有DataTable的..
    在抛出异常前,先跳出这个对话框..看不懂...System.Data.SqlClient.SqlException:SQL Sever 不存在或访问被拒绝   
       at System.Data.SqlClient.ConnectionPool.GetConnection(Boolean & isInTransaction)
       at System.Data.SqlClient.SqlConnectionPoolManager.GetPooledConnection(SqlConnectionString options, Boolean & isInTransaction)
       at System.Data.SqlClient.SqlConnection.Open()
                 ...
                 ...
       at PopulatingAndUpdateingDataSets.Form1.Form1_load(Object sender,EventArgs e) in
    f:\c#\stater\form1.cs:line 239
      

  7.   

    回复   njbaige(白鸽) 
    F5程序后,跳不到窗体界面,所以没机会选择StoresComboBox 
    而且StoresComboBox里面设置了Items属性(添加了几条Items),结果是一样的...
    困惑
      

  8.   

    那就是当你进入页面的时候你还没有选择一条记录所以
    storeID = StoresComboBox.SelectedValue.ToString();
    会出错
    可以在StoresComboBox的选择改变时调用private void UpdateTable()
    这个方法调试一下