为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

红糖糍粑 2021-10-13 15:11:21
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;}}}
查看完整描述

1 回答

?
慕标琳琳

TA贡献1830条经验 获得超9个赞

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;
}
}
看看输出什么错误?



查看完整回答
反对 回复 2021-10-17
  • 1 回答
  • 0 关注
  • 172 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信