it is very strange£¬
when i pressSaveUpdate £¬if only change txtState£¬it's not problem£¬
but if i change txtContactName, it will be show error£¬
rj.mdb £º
id auto
date string
content string
£¿
thanks!
using System;
using System.Drawing;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;
using System.Data;
using System.Data.Ole Db;
namespace Database_Exampl e
{
/// <summary>
/// Form1
/// </summary>
public class Form1 : System.Windows. Forms.Form
{
OleDbConnection m_cnADONetConne ction =new OleDbConnection ();
OleDbDataAdapte r m_daDataAdapter = new OleDbDataAdapte r();
DataTable m_dtContacts = new DataTable();
int m_rowPosition = 0;
private System.Windows. Forms.TextBox txtState;
private System.Windows. Forms.TextBox txtContactName;
private System.Windows. Forms.Button btnMoveFirst;
private System.Windows. Forms.Button btnMovePrevious ;
private System.Windows. Forms.Button btnMoveNext;
private System.Windows. Forms.Button btnMoveLast;
private System.Windows. Forms.Button btnSave;
private System.Windows. Forms.TextBox txtNewContactNa me;
private System.Windows. Forms.TextBox txtNewState;
private System.Windows. Forms.GroupBox grpNewRecord;
private System.Windows. Forms.Button btnAddNew;
private System.Windows. Forms.Button btnDelete;
/// <summary>
/// ±ØÐèµÄÉè¼ÆÆ÷±äÁ ¿¡£
/// </summary>
private System.Componen tModel.Containe r components = null;
public Form1()
{
//
// Windows ´°ÌåÉè¼ÆÆ÷Ö§³ÖË ù±ØÐèµÄ
//
InitializeCompo nent();
//
// TODO: ÔÚ InitializeCompo nent µ÷ÓúóÌí¼ÓÈκι ¹Ô캯Êý´úÂë
//
}
/// <summary>
/// ÇåÀíËùÓÐÕýÔÚÊ¹Ó ÃµÄ×ÊÔ´¡£
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Disp ose();
}
}
base.Dispose( disposing );
}
#region Windows
/// <summary>
/// </summary>
private void InitializeCompo nent()
{
this.txtState = new System.Windows. Forms.TextBox() ;
this.txtContact Name = new System.Windows. Forms.TextBox() ;
this.btnMoveFir st = new System.Windows. Forms.Button();
this.btnMovePre vious = new System.Windows. Forms.Button();
this.btnMoveNex t = new System.Windows. Forms.Button();
this.btnMoveLas t = new System.Windows. Forms.Button();
this.btnSave = new System.Windows. Forms.Button();
this.txtNewCont actName = new System.Windows. Forms.TextBox() ;
this.txtNewStat e = new System.Windows. Forms.TextBox() ;
this.grpNewReco rd = new System.Windows. Forms.GroupBox( );
this.btnAddNew = new System.Windows. Forms.Button();
this.btnDelete = new System.Windows. Forms.Button();
this.grpNewReco rd.SuspendLayou t();
this.SuspendLay out();
//
// txtState
//
this.txtState.L ocation = new System.Drawing. Point(192, 40);
this.txtState.N ame = "txtState";
this.txtState.S ize = new System.Drawing. Size(320, 25);
this.txtState.T abIndex = 0;
this.txtState.T ext = "txtState";
//
// txtContactName
//
this.txtContact Name.Location = new System.Drawing. Point(40, 40);
this.txtContact Name.Name = "txtContactName ";
this.txtContact Name.Size = new System.Drawing. Size(136, 25);
this.txtContact Name.TabIndex = 1;
this.txtContact Name.Text = "txtContactName ";
//
// btnMoveFirst
//
this.btnMoveFir st.Location = new System.Drawing. Point(32, 128);
this.btnMoveFir st.Name = "btnMoveFir st";
this.btnMoveFir st.TabIndex = 2;
this.btnMoveFir st.Text = "<<";
this.btnMoveFir st.Click += new
System.EventHan dler(this.btnMo veFirst_Click);
//
// btnMovePrevious
//
this.btnMovePre vious.Location = new System.Drawing. Point(104, 128);
this.btnMovePre vious.Name = "btnMovePreviou s";
this.btnMovePre vious.TabIndex = 3;
this.btnMovePre vious.Text = "<";
this.btnMovePre vious.Click += new
System.EventHan dler(this.btnMo vePrevious_Clic k);
//
// btnMoveNext
//
this.btnMoveNex t.Location = new System.Drawing. Point(176, 128);
this.btnMoveNex t.Name = "btnMoveNex t";
this.btnMoveNex t.TabIndex = 4;
this.btnMoveNex t.Text = ">";
this.btnMoveNex t.Click += new
System.EventHan dler(this.btnMo veNext_Click);
//
// btnMoveLast
//
this.btnMoveLas t.Location = new System.Drawing. Point(248, 128);
this.btnMoveLas t.Name = "btnMoveLas t";
this.btnMoveLas t.TabIndex = 5;
this.btnMoveLas t.Text = ">>";
this.btnMoveLas t.Click += new
System.EventHan dler(this.btnMo veLast_Click);
//
// btnSave
//
this.btnSave.Lo cation = new System.Drawing. Point(32, 168);
this.btnSave.Na me = "btnSave";
this.btnSave.Si ze = new System.Drawing. Size(96, 23);
this.btnSave.Ta bIndex = 6;
this.btnSave.Te xt = "SaveUpdate ";
this.btnSave.Cl ick += new System.EventHan dler(this.btnSa ve_Click);
//
// txtNewContactNa me
//
this.txtNewCont actName.Locatio n = new System.Drawing. Point(8, 24);
this.txtNewCont actName.Name = "txtNewContactN ame";
this.txtNewCont actName.TabInde x = 8;
this.txtNewCont actName.Text = "";
//
// txtNewState
//
this.txtNewStat e.Location = new System.Drawing. Point(8, 64);
this.txtNewStat e.Name = "txtNewStat e";
this.txtNewStat e.TabIndex = 9;
this.txtNewStat e.Text = "";
//
// grpNewRecord
//
this.grpNewReco rd.Controls.Add (this.btnAddNew );
this.grpNewReco rd.Controls.Add (this.txtNewCon tactName);
this.grpNewReco rd.Controls.Add (this.txtNewSta te);
this.grpNewReco rd.Location = new System.Drawing. Point(360, 96);
this.grpNewReco rd.Name = "grpNewReco rd";
this.grpNewReco rd.Size = new System.Drawing. Size(200, 136);
this.grpNewReco rd.TabIndex = 10;
this.grpNewReco rd.TabStop = false;
this.grpNewReco rd.Text = "New Contact";
//
// btnAddNew
//
this.btnAddNew. Location = new System.Drawing. Point(32, 104);
this.btnAddNew. Name = "btnAddNew" ;
this.btnAddNew. TabIndex = 10;
this.btnAddNew. Text = "Add";
this.btnAddNew. Click += new System.EventHan dler(this.btnAd dNew_Click);
//
// btnDelete
//
this.btnDelete. Location = new System.Drawing. Point(240, 168);
this.btnDelete. Name = "btnDelete" ;
this.btnDelete. TabIndex = 11;
this.btnDelete. Text = "Delete";
this.btnDelete. Click += new System.EventHan dler(this.btnDe lete_Click);
//
// Form1
//
this.AutoScaleB aseSize = new System.Drawing. Size(8, 18);
this.ClientSize = new System.Drawing. Size(568, 264);
this.Controls.A dd(this.btnDele te);
this.Controls.A dd(this.grpNewR ecord);
this.Controls.A dd(this.btnSave );
this.Controls.A dd(this.btnMove Last);
this.Controls.A dd(this.btnMove Next);
this.Controls.A dd(this.btnMove Previous);
this.Controls.A dd(this.btnMove First);
this.Controls.A dd(this.txtCont actName);
this.Controls.A dd(this.txtStat e);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHan dler(this.Form1 _Load);
this.Closed += new System.EventHan dler(this.Form1 _Closed);
this.grpNewReco rd.ResumeLayout (false);
this.ResumeLayo ut(false);
}
#endregion
/// <summary>
/// Ó¦ÓóÌÐòµÄÖ÷Èë¿ Úµã¡£
/// </summary>
[STAThread]
static void Main()
{
Application.Run (new Form1());
}
private void Form1_Load(obje ct sender, System.EventArg s e)
{
m_cnADONetConne ction.Connectio nString=@"Provi der=Microsoft.J et.OLEDB.4.0;Da ta
Source=rj.mdb;" ;
m_cnADONetConne ction.Open();
m_daDataAdapter =
new OleDbDataAdapte r("Select * From rj ",m_cnADONetCon nection);//rj.mdb
, rj table
OleDbCommandBui lder m_cbCommandBuil der =
new OleDbCommandBui lder(m_daDataAd apter);
m_daDataAdapter .Fill(m_dtConta cts);
this.ShowCurren tRecord();
}
private void Form1_Closed(ob ject sender, System.EventArg s e)
{
m_cnADONetConne ction.Close();
}
private void ShowCurrentReco rd()
{
if (m_dtContacts.R ows.Count==0)
{
txtContactName. Text = "";
txtState.Text = "";
return;
}
txtContactName. Text =
m_dtContacts.Ro ws[m_rowPosition]["date"].ToString();
txtState.Text = m_dtContacts.Ro ws[m_rowPosition]["content"].ToString();
}
private void btnMoveFirst_Cl ick(object sender, System.EventArg s e)
{
// Move to the first row and show the data.
m_rowPosition = 0;
this.ShowCurren tRecord();
}
private void btnMovePrevious _Click(object sender, System.EventArg s e)
{
// If not at the first row, go back one row and show the record.
if (m_rowPosition != 0)
{
m_rowPosition = m_rowPosition-1;
this.ShowCurren tRecord();
}
}
private void btnMoveNext_Cli ck(object sender, System.EventArg s e)
{
// If not on the last row, advance one row and show the record.
if (m_rowPosition < m_dtContacts.Ro ws.Count-1)
{
m_rowPosition = m_rowPosition + 1;
this.ShowCurren tRecord();
}
}
private void btnMoveLast_Cli ck(object sender, System.EventArg s e)
{
// If there are any rows in the data table,
// move to the last and show the record.
if (m_dtContacts.R ows.Count != 0)
{
m_rowPosition = m_dtContacts.Ro ws.Count-1;
this.ShowCurren tRecord();
}
}
private void btnSave_Click(o bject sender, System.EventArg s e)
{
// If there is existing data, update it.
if(m_dtContacts .Rows.Count!=0)
{
m_dtContacts.Ro ws[m_rowPosition]["date"]=txtContactName .Text;
m_dtContacts.Ro ws[m_rowPosition]["content"]=txtState.Text;
try
{
m_daDataAdapter .Update(m_dtCon tacts);//error
}
catch(System.Da ta.OleDb.OleDbE xception ex)
{
MessageBox.Show (ex.Message) ;
}
}
}
private void btnAddNew_Click (object sender, System.EventArg s e)
{
DataRow drNewRow=m_dtCo ntacts.NewRow() ;
drNewRow["date"]=txtNewContactN ame.Text;
drNewRow["content"]=txtNewState.Te xt;;
m_dtContacts.Ro ws.Add(drNewRow );
m_daDataAdapter .Update(m_dtCon tacts);
m_rowPosition = m_dtContacts.Ro ws.Count-1;
this.ShowCurren tRecord();
}
private void btnDelete_Click (object sender, System.EventArg s e)
{
// If there is data, delete the current row.
if (m_dtContacts.R ows.Count !=0)
{
m_dtContacts.Ro ws[m_rowPosition].Delete();
m_daDataAdapter .Update(m_dtCon tacts);
m_rowPosition=0 ;
this.ShowCurren tRecord();
}
}
}
}