I'm going through the book "Sams teach yourself C# in 24 hours", but unfortunately don't have the CD, which contains "contacts.mdb". Can someone send me the file, as I need it for my C# project? Thanks!
Also, I have another question regarding this code:
namespace Database_Example
{
public partial class MainForm : Form
{
OleDbConnection m_cnADONetConnection = new OleDbConnection();
OleDbDataAdapter m_daDataAdapter;
OleDbCommandBuilder m_cbCommandBuilder;
DataTable m_dtContacts = new DataTable();
int m_rowPosition = 0;
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
m_cnADONetConnection.ConnectionString =
@"Provider=Microsot.Jet.OLEDB.4.0;Data Source=C:\Users\Baba Vida\Downloads\contacts.mdb";
m_cnADONetConnection.Open();
m_daDataAdapter =
new OleDbDataAdapter("Select * From Contacts", m_cnADONetConnection);
OleDbCommandBuilder m_cbCommandBuilder =
new OleDbCommandBuilder(m_daDataAdapter);
m_daDataAdapter.Fill(m_dtContacts);
this.ShowCurrentRecord();
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
m_cnADONetConnection.Close();
m_cnADONetConnection.Dispose();
}
private void ShowCurrentRecord()
{
if (m_dtContacts.Rows.Count == 0)
{
txtContactName.Text = "";
txtState.Text = "";
return;
}
txtContactName.Text =
m_dtContacts.Rows[m_rowPosition]["ContactName"].ToString();
txtState.Text = m_dtContacts.Rows[m_rowPosition]["State"].ToString();
}
private void btnMoveFirst_Click(object sender, EventArgs e)
{
// Move to the first row and show the data
m_rowPosition = 0;
this.ShowCurrentRecord();
}
private void btnMovePrevious_Click(object sender, EventArgs e)
{
// If not at the first row, go back one row and show the record
if (m_rowPosition != 0)
{
m_rowPosition--;
this.ShowCurrentRecord();
}
}
private void btnMoveNext_Click(object sender, 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++;
this.ShowCurrentRecord();
}
}
private void btnMoveLast_Click(object sender, 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, EventArgs e)
{
// If there is existing data, update it
if (m_dtContacts.Rows.Count != 0)
{
m_dtContacts.Rows[m_rowPosition]["ContactName"] = txtContactName.Text;
m_dtContacts.Rows[m_rowPosition]["State"] = txtState.Text;
m_daDataAdapter.Update(m_dtContacts);
}
}
private void btnAddNew_Click(object sender, EventArgs e)
{
DataRow drNewRow = m_dtContacts.NewRow();
drNewRow["ContactName"] = txtNewContactName.Text;
drNewRow["State"] = 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, 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();
}
}
}
}Any ideas why I get following error when trying to add entry to my database:
"An unhandled exception of type 'System.ArgumentException' occurred in System.Data.dll
Additional information: Column 'ContactName' does not belong to table ."