最近新学了C#,做到数据库时写了个程序:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace _1
{
public partial class Form1 : Form
{
System.Data.OleDb.OleDbConnection con = null;
System.Data.OleDb.OleDbDataAdapter adapter = null;
System.Data.OleDb.OleDbCommandBuilder builder = null;
DataTable table = null;
BindingSource source=null;
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{ } private void bindingSource1_CurrentChanged(object sender, EventArgs e)
{
try
{
con = new System.Data.OleDb.OleDbConnection();
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=.//op.mdb";
con.Open();
adapter = new System.Data.OleDb.OleDbDataAdapter("select * from 表1", con);
builder = new System.Data.OleDb.OleDbCommandBuilder(adapter);
table = new DataTable();
adapter.Fill(table);
dataGridView1.DataSource = table;
source = new BindingSource();
source.DataSource = table;
con.Close();
}
catch (Exception er)
{
con.Close();
System.Diagnostics.Debug.WriteLine(er.ToString());
}
}
private void button1_Click(object sender, EventArgs e)
{
try
{
adapter.Update((DataTable)source.DataSource);
}
catch(Exception er)
{
System.Diagnostics.Debug.WriteLine(er.ToString());
}
}
}
}
这是按照网上给的程序写的,可是运行时,按下button1时,出现了下面的错误:
在 System.NullReferenceException 中第一次偶然出现的“11.exe”类型的异常
System.NullReferenceException: 未将对象引用设置到对象的实例。
在 _1.Form1.button1_Click(Object sender, EventArgs e) 位置 F:\C#\应用程序\11\11\11\Form1.cs:行号 56
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace _1
{
public partial class Form1 : Form
{
System.Data.OleDb.OleDbConnection con = null;
System.Data.OleDb.OleDbDataAdapter adapter = null;
System.Data.OleDb.OleDbCommandBuilder builder = null;
DataTable table = null;
BindingSource source=null;
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{ } private void bindingSource1_CurrentChanged(object sender, EventArgs e)
{
try
{
con = new System.Data.OleDb.OleDbConnection();
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=.//op.mdb";
con.Open();
adapter = new System.Data.OleDb.OleDbDataAdapter("select * from 表1", con);
builder = new System.Data.OleDb.OleDbCommandBuilder(adapter);
table = new DataTable();
adapter.Fill(table);
dataGridView1.DataSource = table;
source = new BindingSource();
source.DataSource = table;
con.Close();
}
catch (Exception er)
{
con.Close();
System.Diagnostics.Debug.WriteLine(er.ToString());
}
}
private void button1_Click(object sender, EventArgs e)
{
try
{
adapter.Update((DataTable)source.DataSource);
}
catch(Exception er)
{
System.Diagnostics.Debug.WriteLine(er.ToString());
}
}
}
}
这是按照网上给的程序写的,可是运行时,按下button1时,出现了下面的错误:
在 System.NullReferenceException 中第一次偶然出现的“11.exe”类型的异常
System.NullReferenceException: 未将对象引用设置到对象的实例。
在 _1.Form1.button1_Click(Object sender, EventArgs e) 位置 F:\C#\应用程序\11\11\11\Form1.cs:行号 56
这个为null呢
看你的代码必须先触发bindingSource1_CurrentChanged这个事件,没看到你出发的地方
{
try
{
con = new System.Data.OleDb.OleDbConnection();
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=.//op.mdb";
con.Open();
adapter = new System.Data.OleDb.OleDbDataAdapter("select * from 表1", con);
builder = new System.Data.OleDb.OleDbCommandBuilder(adapter);
table = new DataTable();
adapter.Fill(table);
dataGridView1.DataSource = table;
source = new BindingSource();
source.DataSource = table;
con.Close();
}
catch (Exception er)
{
con.Close();
System.Diagnostics.Debug.WriteLine(er.ToString());
}
} 最好能现将它们先放在load事件里,把adater实例化后,应该就不会有这个问题了