0

我是 c# .Net 的新手,我正在开发 c# winform 应用程序。我需要你的帮助。

public partial class Form1 : Form 
{ 
    OleDbConnection baglanti = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Burak YEŞİLYURT\Desktop\secret.accdb"); 
    OleDbCommand komutcu; 
    OleDbDataAdapter adpt; 
    DataSet ds; 
    public Form1() 
    { 
        InitializeComponent(); 
        baglanti.Open(); 
        OleDbCommand komutcu = new OleDbCommand("SELECT * FROM todo", baglanti); 
        OleDbDataAdapter adpt = new OleDbDataAdapter(komutcu); 
        DataSet ds = new DataSet(); 
        adpt.Fill(ds); 
        dataGridView1.DataSource = ds.Tables[0]; 

    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
        OleDbCommandBuilder komut = new OleDbCommandBuilder(adpt); 
        DataSet yeni = new DataSet(); 
        yeni = ds.GetChanges(DataRowState.Modified); //here i get the error
        adpt.Update(yeni.Tables[0]); 
    } 
} 

当我调试我的程序时,我收到以下错误yeni = ds.GetChanges(DataRowState.Modified);

“你调用的对象是空的。 ”

我的目的:填充DataGridView和用户编辑单元格后,将更改保存在单元格中。我无法解决这个问题。欢迎任何帮助。谢谢。

4

3 回答 3

2

修复你的构造函数

public Form1() 
    { 
        InitializeComponent(); 
        baglanti.Open(); 
        OleDbCommand komutcu = new OleDbCommand("SELECT * FROM todo", baglanti); 
        OleDbDataAdapter adpt = new OleDbDataAdapter(komutcu); 
        this.ds = new DataSet();  //there was an error in this line
        ..

    } 

您在构造函数中创建了 的本地实例DataSet,但忘记保存对类变量 的引用ds。然后在button1_Click您尝试访问未初始化的字段ds(它有一个null值)。

所以,而不是这条线

DataSet ds = new DataSet();

你应该写

this.ds = new DataSet();
于 2012-08-15T19:53:27.210 回答
0

你已经定义了两次数据集 oledbcommand 和 oldbDataAdapter 所以它不能工作

 public partial class Form1 : Form 
    { 
        OleDbConnection baglanti = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Burak YEŞİLYURT\Desktop\secret.accdb"); 
        OleDbCommand komutcu; 
        OleDbDataAdapter adpt; 
        DataSet ds; 
        public Form1() 
        { 
            InitializeComponent(); 
            baglanti.Open(); 
            komutcu = new OleDbCommand("SELECT * FROM todo", baglanti); 
            adpt = new OleDbDataAdapter(komutcu); 
            ds = new DataSet(); 
            adpt.Fill(ds); 
            dataGridView1.DataSource = ds.Tables[0]; 

        } 

        private void button1_Click(object sender, EventArgs e) 
        { 
            OleDbCommandBuilder komut = new OleDbCommandBuilder(adpt); 
            DataSet yeni = new DataSet(); 
            yeni = ds.GetChanges(DataRowState.Modified); //here i get the error
            adpt.Update(yeni.Tables[0]); 
        } 
    } 
于 2012-08-15T19:55:08.133 回答
0
OleDbConnection baglanti = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data     Source=C:\Users\Burak YEŞİLYURT\Desktop\secret.accdb"); 
    OleDbCommand komutcu; 
    OleDbDataAdapter adpt; 
    DataSet ds = new DataSet(); 
    public Form1() 
    { 
        InitializeComponent(); 
        baglanti.Open(); 
        OleDbCommand komutcu = new OleDbCommand("SELECT * FROM todo", baglanti); 
        OleDbDataAdapter adpt = new OleDbDataAdapter(komutcu); 
        //DataSet ds = new DataSet(); 
        adpt.Fill(ds); 
        dataGridView1.DataSource = ds.Tables[0]; 

    } 
于 2012-08-15T20:00:52.427 回答