using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.Odbc;
namespace SQLData
{
///
/// Summary description for Form1.
///
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox connString;
private System.Windows.Forms.DataGrid theGrid;
private System.Windows.Forms.Button button2;
///
/// Required designer variable.
///
private System.ComponentModel.Container components = null;
private DataSet venueSet;
private DataTable venueTable;
private DataRow venueRow;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
///
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.theGrid = new System.Windows.Forms.DataGrid();
this.connString = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.theGrid)).BeginInit();
this.SuspendLayout();
//
// theGrid
//
this.theGrid.DataMember = "";
this.theGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.theGrid.Location = new System.Drawing.Point(8, 8);
this.theGrid.Name = "theGrid";
this.theGrid.Size = new System.Drawing.Size(688, 360);
this.theGrid.TabIndex = 0;
this.theGrid.DoubleClick += new System.EventHandler(this.theGrid_DoubleClick);
this.theGrid.MouseUp += new System.Windows.Forms.MouseEventHandler(this.theGrid_MouseUp);
//
// connString
//
this.connString.Location = new System.Drawing.Point(8, 376);
this.connString.Name = "connString";
this.connString.Size = new System.Drawing.Size(480, 20);
this.connString.TabIndex = 1;
this.connString.Text = "DSN=Tix21;UID=root;PWD=xxxxxx";
//
// button1
//
this.button1.Location = new System.Drawing.Point(512, 376);
this.button1.Name = "button1";
this.button1.TabIndex = 2;
this.button1.Text = "LOAD";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(600, 376);
this.button2.Name = "button2";
this.button2.TabIndex = 3;
this.button2.Text = "SAVE";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(704, 406);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.connString);
this.Controls.Add(this.theGrid);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.theGrid)).EndInit();
this.ResumeLayout(false);
}
#endregion
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
Cursor = Cursors.WaitCursor;
OdbcConnection dbConn = new OdbcConnection();
// Now Pass a Connection String To the Connection
dbConn.ConnectionString = connString.Text;
// Create an Adapter
OdbcDataAdapter da = new OdbcDataAdapter("select * from VenueMaster", dbConn);
// Create a New DataSet
venueSet = new DataSet();
// Fill The DataSet With the Contents of the table
da.Fill(venueSet, "VenueMaster");
// Now Return ds which is a DataSet
theGrid.SetDataBinding(venueSet, venueSet.Tables[0].TableName);
venueTable = venueSet.Tables[0];
Cursor = Cursors.Default;
}
private void button2_Click(object sender, System.EventArgs e)
{
Cursor = Cursors.WaitCursor;
venueRow = venueTable.NewRow();
venueTable.Rows.Add(venueRow);
venueRow["lockKey"] = 0;
venueRow["whenCreated"] = DateTime.Now;
venueRow["venueName"] = "The Name";
venueRow["contactEmail"] = "xxx@yyy.com";
venueRow["theState"] = "IL";
venueRow["tollfree"] = "(800)332-1997";
venueRow["contactExtension"] = "324";
venueRow["directions"] = "Get on highway and drive";
venueRow["contactFirst"] = "Joe";
venueRow["defaultEventTax"] = Convert.ToDouble("3.25");
venueRow["active"] = true;
venueRow["zip"] = "12345";
venueRow["fax"] = "(615)998-1099";
venueRow["city"] = "Naperville";
venueRow["address2"] = "suite 101";
venueRow["address1"] = "123 W. 57th St";
venueRow["contactLast"] = "Contact";
venueRow["website"] = "www.junk.com";
venueRow["phone"] = "(717)991-1886";
if (!venueSet.HasChanges())
{
Cursor = Cursors.Default;
MessageBox.Show("No changes were made to the data", "ATTENTION", MessageBoxButtons.OK,
MessageBoxIcon.Stop);
}
else
{
// send delta to update routine
DataSet cd = venueSet.GetChanges();
try
{
OdbcConnection dbConn = new OdbcConnection();
// Now Pass a Connection String To the Connection
dbConn.ConnectionString = connString.Text;
foreach (DataTable tbl in venueSet.Tables)
{
// Create an Adapter
OdbcDataAdapter da = new OdbcDataAdapter();
// setup the select statement for this table
da.SelectCommand = new OdbcCommand("SELECT * FROM " + tbl.TableName +
" where id = -1", dbConn);
OdbcCommandBuilder cb = new OdbcCommandBuilder(da);
// do the updates we need
da.Update(venueSet, tbl.TableName);
}
}
catch(Exception ex)
{
Cursor = Cursors.Default;
MessageBox.Show(ex.Message, "AN ERROR HAS OCCURRED", MessageBoxButtons.OK,
MessageBoxIcon.Stop);
return;
}
venueSet.AcceptChanges();
Cursor = Cursors.Default;
}
}
private void theGrid_DoubleClick(object sender, System.EventArgs e)
{
}
private void theGrid_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button != MouseButtons.Left)
return;
System.Windows.Forms.DataGrid.HitTestInfo hitMe;
hitMe = theGrid.HitTest(e.X, e.Y);
if (hitMe.Type.ToString() == "ColumnHeader" || hitMe.Type.ToString() == "None")
return;
Cursor = Cursors.WaitCursor;
BindingManagerBase bmb = theGrid.BindingContext[theGrid.DataSource, theGrid.DataMember];
DataRowView drv = (DataRowView)bmb.Current;
venueRow = drv.Row;
venueRow["defaultEventTax"] = Convert.ToDouble("7.75");
if (!venueSet.HasChanges())
{
Cursor = Cursors.Default;
MessageBox.Show("No changes were made to the data", "ATTENTION", MessageBoxButtons.OK,
MessageBoxIcon.Stop);
}
else
{
// send delta to update routine
DataSet cd = venueSet.GetChanges();
try
{
OdbcConnection dbConn = new OdbcConnection();
// Now Pass a Connection String To the Connection
dbConn.ConnectionString = connString.Text;
foreach (DataTable tbl in venueSet.Tables)
{
// Create an Adapter
OdbcDataAdapter da = new OdbcDataAdapter();
// setup the select statement for this table
da.SelectCommand = new OdbcCommand("SELECT * FROM " + tbl.TableName +
" where id = -1", dbConn);
OdbcCommandBuilder cb = new OdbcCommandBuilder(da);
// do the updates we need
da.Update(venueSet, tbl.TableName);
}
}
catch(Exception ex)
{
Cursor = Cursors.Default;
MessageBox.Show(ex.Message, "AN ERROR HAS OCCURRED", MessageBoxButtons.OK,
MessageBoxIcon.Stop);
return;
}
venueSet.AcceptChanges();
Cursor = Cursors.Default;
}
}
}
}