ado.net help

M

Michael

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.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
namespace Database_Example
{
/// <summary>
/// Form1
/// </summary>
public class Form1 : System.Windows.Forms.Form
{

OleDbConnection m_cnADONetConnection =new OleDbConnection();
OleDbDataAdapter m_daDataAdapter = new OleDbDataAdapter();
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 txtNewContactName;
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.ComponentModel.Container components = null;



public Form1()
{
//
// Windows ´°ÌåÉè¼ÆÆ÷Ö§³ÖËù±ØÐèµÄ
//
InitializeComponent();

//
// TODO: ÔÚ InitializeComponent µ÷ÓúóÌí¼ÓÈκι¹Ô캯Êý´úÂë
//
}

/// <summary>
/// ÇåÀíËùÓÐÕýÔÚʹÓõÄ×ÊÔ´¡£
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows
/// <summary>

/// </summary>
private void InitializeComponent()
{
this.txtState = new System.Windows.Forms.TextBox();
this.txtContactName = new System.Windows.Forms.TextBox();
this.btnMoveFirst = new System.Windows.Forms.Button();
this.btnMovePrevious = new System.Windows.Forms.Button();
this.btnMoveNext = new System.Windows.Forms.Button();
this.btnMoveLast = new System.Windows.Forms.Button();
this.btnSave = new System.Windows.Forms.Button();
this.txtNewContactName = new System.Windows.Forms.TextBox();
this.txtNewState = new System.Windows.Forms.TextBox();
this.grpNewRecord = new System.Windows.Forms.GroupBox();
this.btnAddNew = new System.Windows.Forms.Button();
this.btnDelete = new System.Windows.Forms.Button();
this.grpNewRecord.SuspendLayout();
this.SuspendLayout();
//
// txtState
//
this.txtState.Location = new System.Drawing.Point(192, 40);
this.txtState.Name = "txtState";
this.txtState.Size = new System.Drawing.Size(320, 25);
this.txtState.TabIndex = 0;
this.txtState.Text = "txtState";
//
// txtContactName
//
this.txtContactName.Location = new System.Drawing.Point(40, 40);
this.txtContactName.Name = "txtContactName";
this.txtContactName.Size = new System.Drawing.Size(136, 25);
this.txtContactName.TabIndex = 1;
this.txtContactName.Text = "txtContactName";
//
// btnMoveFirst
//
this.btnMoveFirst.Location = new System.Drawing.Point(32, 128);
this.btnMoveFirst.Name = "btnMoveFirst";
this.btnMoveFirst.TabIndex = 2;
this.btnMoveFirst.Text = "<<";
this.btnMoveFirst.Click += new
System.EventHandler(this.btnMoveFirst_Click);
//
// btnMovePrevious
//
this.btnMovePrevious.Location = new System.Drawing.Point(104, 128);
this.btnMovePrevious.Name = "btnMovePrevious";
this.btnMovePrevious.TabIndex = 3;
this.btnMovePrevious.Text = "<";
this.btnMovePrevious.Click += new
System.EventHandler(this.btnMovePrevious_Click);
//
// btnMoveNext
//
this.btnMoveNext.Location = new System.Drawing.Point(176, 128);
this.btnMoveNext.Name = "btnMoveNext";
this.btnMoveNext.TabIndex = 4;
this.btnMoveNext.Text = ">";
this.btnMoveNext.Click += new
System.EventHandler(this.btnMoveNext_Click);
//
// btnMoveLast
//
this.btnMoveLast.Location = new System.Drawing.Point(248, 128);
this.btnMoveLast.Name = "btnMoveLast";
this.btnMoveLast.TabIndex = 5;
this.btnMoveLast.Text = ">>";
this.btnMoveLast.Click += new
System.EventHandler(this.btnMoveLast_Click);
//
// btnSave
//
this.btnSave.Location = new System.Drawing.Point(32, 168);
this.btnSave.Name = "btnSave";
this.btnSave.Size = new System.Drawing.Size(96, 23);
this.btnSave.TabIndex = 6;
this.btnSave.Text = "SaveUpdate";
this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
//
// txtNewContactName
//
this.txtNewContactName.Location = new System.Drawing.Point(8, 24);
this.txtNewContactName.Name = "txtNewContactName";
this.txtNewContactName.TabIndex = 8;
this.txtNewContactName.Text = "";
//
// txtNewState
//
this.txtNewState.Location = new System.Drawing.Point(8, 64);
this.txtNewState.Name = "txtNewState";
this.txtNewState.TabIndex = 9;
this.txtNewState.Text = "";
//
// grpNewRecord
//
this.grpNewRecord.Controls.Add(this.btnAddNew);
this.grpNewRecord.Controls.Add(this.txtNewContactName);
this.grpNewRecord.Controls.Add(this.txtNewState);
this.grpNewRecord.Location = new System.Drawing.Point(360, 96);
this.grpNewRecord.Name = "grpNewRecord";
this.grpNewRecord.Size = new System.Drawing.Size(200, 136);
this.grpNewRecord.TabIndex = 10;
this.grpNewRecord.TabStop = false;
this.grpNewRecord.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.EventHandler(this.btnAddNew_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.EventHandler(this.btnDelete_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(8, 18);
this.ClientSize = new System.Drawing.Size(568, 264);
this.Controls.Add(this.btnDelete);
this.Controls.Add(this.grpNewRecord);
this.Controls.Add(this.btnSave);
this.Controls.Add(this.btnMoveLast);
this.Controls.Add(this.btnMoveNext);
this.Controls.Add(this.btnMovePrevious);
this.Controls.Add(this.btnMoveFirst);
this.Controls.Add(this.txtContactName);
this.Controls.Add(this.txtState);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.Closed += new System.EventHandler(this.Form1_Closed);
this.grpNewRecord.ResumeLayout(false);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// Ó¦ÓóÌÐòµÄÖ÷Èë¿Úµã¡£
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{

m_cnADONetConnection.ConnectionString=@"Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=rj.mdb;";
m_cnADONetConnection.Open();

m_daDataAdapter =
new OleDbDataAdapter("Select * From rj ",m_cnADONetConnection);//rj.mdb
, rj table


OleDbCommandBuilder m_cbCommandBuilder =
new OleDbCommandBuilder(m_daDataAdapter);

m_daDataAdapter.Fill(m_dtContacts);


this.ShowCurrentRecord();



}

private void Form1_Closed(object sender, System.EventArgs e)
{
m_cnADONetConnection.Close();
}

private void ShowCurrentRecord()
{
if (m_dtContacts.Rows.Count==0)
{
txtContactName.Text = "";
txtState.Text = "";
return;
}
txtContactName.Text =
m_dtContacts.Rows[m_rowPosition]["date"].ToString();
txtState.Text = m_dtContacts.Rows[m_rowPosition]["content"].ToString();
}

private void btnMoveFirst_Click(object sender, System.EventArgs e)
{
// Move to the first row and show the data.
m_rowPosition = 0;
this.ShowCurrentRecord();

}

private void btnMovePrevious_Click(object sender, System.EventArgs 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.ShowCurrentRecord();
}

}

private void btnMoveNext_Click(object sender, System.EventArgs e)
{
// If not on the last row, advance one row and show the record.
if (m_rowPosition < m_dtContacts.Rows.Count-1)
{
m_rowPosition = m_rowPosition + 1;
this.ShowCurrentRecord();
}

}

private void btnMoveLast_Click(object sender, System.EventArgs e)
{
// If there are any rows in the data table,
// move to the last and show the record.
if (m_dtContacts.Rows.Count != 0)
{
m_rowPosition = m_dtContacts.Rows.Count-1;
this.ShowCurrentRecord();
}

}

private void btnSave_Click(object sender, System.EventArgs e)
{
// If there is existing data, update it.

if(m_dtContacts.Rows.Count!=0)
{

m_dtContacts.Rows[m_rowPosition]["date"]=txtContactName.Text;
m_dtContacts.Rows[m_rowPosition]["content"]=txtState.Text;
try
{
m_daDataAdapter.Update(m_dtContacts);//error
}
catch(System.Data.OleDb.OleDbException ex)
{
MessageBox.Show(ex.Message) ;
}
}
}



private void btnAddNew_Click(object sender, System.EventArgs e)
{
DataRow drNewRow=m_dtContacts.NewRow();
drNewRow["date"]=txtNewContactName.Text;
drNewRow["content"]=txtNewState.Text;;
m_dtContacts.Rows.Add(drNewRow);
m_daDataAdapter.Update(m_dtContacts);
m_rowPosition = m_dtContacts.Rows.Count-1;
this.ShowCurrentRecord();


}

private void btnDelete_Click(object sender, System.EventArgs e)
{
// If there is data, delete the current row.
if (m_dtContacts.Rows.Count !=0)
{
m_dtContacts.Rows[m_rowPosition].Delete();
m_daDataAdapter.Update(m_dtContacts);
m_rowPosition=0;
this.ShowCurrentRecord();
}

}





}
}
 
J

Jani Järvinen [MVP]

Hi Michael,
when i pressSaveUpdate£¬if only change txtState£¬it's not problem£¬
but if i change txtContactName,it will be show error£¬

It would help if you would let us know the error message you get. If I would
guess, it is some kind of SQL error or security and/or constraint error, but
could of course be something else too.

Also, why do you call a field in the database a "date" and then use it as
"contact name" in the code? That's confusing.

--
Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
(e-mail address removed)
http://www.saunalahti.fi/janij/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top