我使用組合框時,我選擇它顯示在下面的是文本框該項目的ID的項目顯示的項目,我已經寫了選定的變化指標的事件裏面的代碼,當我執行的形式和關閉形式,而不在形式做任何事情就告訴我對象引用不設置到對象的實例,在我的查詢字符串 我的代碼如下:空引用的SelectedIndexChanged在關閉形式
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace TinyErp
{
public partial class frmaddprod : Form
{
public frmaddprod()
{
InitializeComponent();
}
private void productsBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.productsBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.orderMachineDataSet);
}
SqlConnection conn = new SqlConnection(Properties.Settings.Default.OrderMachineConnectionString);
string strgetsupp;
private void frmaddprod_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'orderMachineDataSet.Suppliers' table. You can move, or remove it, as needed.
this.suppliersTableAdapter.Fill(this.orderMachineDataSet.Suppliers);
// TODO: This line of code loads data into the 'orderMachineDataSet.Products' table. You can move, or remove it, as needed.
this.productsTableAdapter.Fill(this.orderMachineDataSet.Products);
//string strgetsupp = "select supplier_id from Suppliers where supplier_company='" + ((DataRowView)comboBox1.SelectedItem).Row["supplier_company"].ToString() + "'";
//conn.Open();
//SqlCommand cmd = new SqlCommand(strgetsupp, conn);
//SqlDataReader read;
//read = cmd.ExecuteReader();
//read.Read();
//int sid = int.Parse(read["supplier_id"].ToString());
//read.Close();
//conn.Close();
//supplier_idTextBox.Text = sid.ToString();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
strgetsupp = "select supplier_id from Suppliers where supplier_company='" + ((DataRowView)comboBox1.SelectedItem).Row["supplier_company"].ToString() + "'";
conn.Open();
SqlCommand cmd = new SqlCommand(strgetsupp, conn);
SqlDataReader read;
read = cmd.ExecuteReader();
read.Read();
int sid = int.Parse(read["supplier_id"].ToString());
supplier_idTextBox.Text = sid.ToString();
read.Close();
conn.Close();
}
private void productsDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void frmaddprod_FormClosing(object sender, FormClosingEventArgs e)
{
Application.Exit();
}
}
}
你知道哪條線崩潰? – 2013-02-14 17:08:46
strgetsupp = '+((DataRowView的)comboBox1.SelectedItem).Row [ 「supplier_company」]的ToString()+ 「」 「選擇從供應商那裏supplier_company = supplier_id」'; 這一 – 2013-02-14 17:16:19