C# 如果在结束窗体上选择组合框DexChanged,则我的列名中存在空引用
我正在使用combobox显示项目当我选择项目时,它在其下方的文本框中显示该项目的ID,我已在所选更改索引事件中写入代码,当我执行表单并关闭表单而不在表单中执行任何操作时,它会向我显示未设置为字符串查询中对象实例的对象引用 我的代码如下:C# 如果在结束窗体上选择组合框DexChanged,则我的列名中存在空引用,c#,C#,我正在使用combobox显示项目当我选择项目时,它在其下方的文本框中显示该项目的ID,我已在所选更改索引事件中写入代码,当我执行表单并关闭表单而不在表单中执行任何操作时,它会向我显示未设置为字符串查询中对象实例的对象引用 我的代码如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.D
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();
}
}
}
在执行任何其他操作之前,请检查comboBox1.SelectedItem是否为null
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if(comboBox1.SelectedItem==null)
return;
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();
}
您知道哪一行崩溃了吗?strgetupp=“从供应商\公司='”的供应商中选择供应商\ id+((DataRowView)comboBox1.SelectedItem)。行[“供应商\公司]。ToString()+”;这一次它显示错误是因为出于某种原因,它试图在表单关闭时调用comboBox1\u SelectedIndexChanged。我不知道为什么。显然,当您关闭表单时,SelectedItem为null,并且不理解null.Row