未将对象引用设置到对象实例!错误在private void BindData()函数里!该怎么解决?

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.OleDb;
using System.Data.SqlClient;

namespace ConnectionDB
{
public partial class Form2 : Form
{

DataSet ds;
int recordCount;

public Form2()
{

BuildData();
BindData();
recordCount = this.BindingContext[ds, "StudentInfo"].Count;

}
private void BuildData()
{
string constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\\C#实验\\ConnectionDB\\Database2.accdb";
string str = "select * from [StudentInfo]";
OleDbConnection conn = new OleDbConnection(constr);
conn.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter(str, conn);
OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter);
ds = new DataSet();
adapter.Fill(ds, "StudentInfo");
}
private void BindData()
{
this.textBox1.DataBindings.Add(new Binding("Text", ds, "StudentInfo.SNo"));
this.textBox2.DataBindings.Add(new Binding("Text", ds, "StudentInfo.Class"));
this.textBox3.DataBindings.Add(new Binding("Text", ds, "StudentInfo.SName"));
this.textBox4.DataBindings.Add(new Binding("Text", ds, "StudentInfo.Sgrade"));

}

private void Form2_Load(object sender, EventArgs e)
{

}

private void button2_Click(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{

this.BindingContext[ds, "StudentInfo"].Position -= 1;

}

private void button3_Click(object sender, EventArgs e)
{
this.BindingContext[ds, "StudentInfo"].Position -= 1;
}
}
}

红糖糍粑
浏览 178回答 1
1回答

慕标琳琳

private void BuildData(){string constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\\C#实验\\ConnectionDB\\Database2.accdb";string str = "select * from [StudentInfo]";OleDbConnection conn = new OleDbConnection(constr);try{conn.Open();OleDbDataAdapter adapter = new OleDbDataAdapter(str, conn);OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter);ds = new DataSet();adapter.Fill(ds, "StudentInfo");}catch (Exception){throw;}}看看输出什么错误?
打开App,查看更多内容
随时随地看视频慕课网APP