/* U S I N G A D O D A T A S O U R C E S A N D S Q L D A T A B A S E S * * Created 5/14/07 * Ron Kessler * * * Shows how to use the DataSource wizard to show SQL 2005 DB in a grid control * Has complete insert, edit, update, & delete capabilities * * Has error handling included. * * Updated 5/14/07 */ using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; //add this for error handling namespace ReadSQL_DB_Part_1 { public partial class frmMain : Form { public frmMain() { InitializeComponent(); } private void customersBindingNavigatorSaveItem_Click(object sender, EventArgs e) { try { if (this.Validate()) { this.customersBindingSource.EndEdit(); this.customersTableAdapter.Update(this._customers_05DataSet.customers); } } catch (SqlException sqlEX) { MessageBox.Show("A SQL error occurred." + sqlEX.Message, "System Message", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (DataException dataEX) { MessageBox.Show("A data error occurred." + dataEX.Message, "System Message", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (DBConcurrencyException concurrEX) { MessageBox.Show("A concurrency error occurred." + concurrEX.Message, "System Message", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void Form1_Load(object sender, EventArgs e) { try { this.customersTableAdapter.Fill(this._customers_05DataSet.customers); this.customersTableAdapter.Fill(this._customers_05DataSet.customers); } catch (Exception EX) { MessageBox.Show("I could not read the database." + EX.Message, "System Message", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } }