【发布时间】:2014-07-12 01:24:17
【问题描述】:
我正在开发一个适用于计算机的应用商店,我需要一个从商店中删除产品的选项,因此我创建了一个 DataGridView 和 数据源 其中是访问的数据库文件。
当我第一次调试时,我删除了一行并在 DataGridView 中更新并在 Access 数据库文件中更新,但是当我退出应用程序重新调试时,列表再次显示已删除的行(虽然它没有显示在访问数据库文件中)。
当我删除任何行时,它也会导致错误(SystemNullReferenceException)
我正在使用 OleDb 提供程序。
这是我的代码:
namespace CompStore
{
public partial class ProductRemove : Form
{
private string str = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=J:\C#\CompStore\Store.accdb";
OleDbConnection con;
OleDbCommand com;
public ProductRemove()
{
con = new OleDbConnection(str);
InitializeComponent();
}
private void ProductRemove_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'storeDataSet.Products' table. You can move, or remove it, as needed.
this.productsTableAdapter.Fill(this.storeDataSet.Products);
}
private void button1_Click_1(object sender, EventArgs e)
{
con.Open();
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
DataGridViewRow delrow = dataGridView1.Rows[i];
if (delrow.Selected == true)
{
try
{
com.CommandText = "DELETE FROM Products WHERE ProductID=" + dataGridView1.Rows[i].Cells[0].Value + "";
com.Connection = con;
int count = com.ExecuteNonQuery();
}
catch (Exception ex) { MessageBox.Show(ex.ToString()); }
dataGridView1.Rows.RemoveAt(i);
}
}
con.Close();
}
}
}
【问题讨论】:
标签: c# database oledb nullreferenceexception