King Abadon
May 9th, 2007, 03:09 AM
here's the code
private void InitializeComponent()
{
this.SelectComand1.CommandText = "SELECT Client.* FROM Client";
this.SelectComand1.Connection = this.SelectConnection;
this.SelectConnection.ConnectionString = "server=DANI;Trusted_Connection=yes;database=TimeTracker; connection timeout=30";
this.SelectConnection.FireInfoMessageEventOnUserErrors = false;
//
this.InsertComand1.CommandText = "INSERT INTO Client (ClientID,ClientName) VALUES (?,?)";
this.InsertComand1.Connection = this.SelectConnection;
this.InsertComand1.Parameters.AddRange(new System.Data.SqlClient.SqlParameter[] {
new System.Data.SqlClient.SqlParameter("ClientID", System.Data.SqlDbType.NVarChar, 10, "ClientID"),
new System.Data.SqlClient.SqlParameter("ClientName", System.Data.SqlDbType.NVarChar, 50, "ClientName")});
//
this.UpdateComand1.Connection = this.SelectConnection;
this.UpdateComand1.Parameters.AddRange(new System.Data.SqlClient.SqlParameter[] {
new System.Data.SqlClient.SqlParameter("ClientID", System.Data.SqlDbType.NVarChar, 10, "ClientID"),
new System.Data.SqlClient.SqlParameter("ClientName", System.Data.SqlDbType.NVarChar, 50, "ClientName"),
new System.Data.SqlClient.SqlParameter("Original_ClientID", System.Data.SqlDbType.NVarChar, 10, System.Data.ParameterDirection.Input, false, ((byte)(0)), ((byte)(0)), "ClientID", System.Data.DataRowVersion.Original, null),
new System.Data.SqlClient.SqlParameter("Original_ClientName", System.Data.SqlDbType.NVarChar, 50, System.Data.ParameterDirection.Input, false, ((byte)(0)), ((byte)(0)), "ClientName", System.Data.DataRowVersion.Original, null)});
//
this.sqlDataAdapter.DeleteCommand = this.DeleteComand1;
this.sqlDataAdapter.InsertCommand = this.InsertComand1;
this.sqlDataAdapter.SelectCommand = this.SelectComand1;
this.sqlDataAdapter.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
new System.Data.Common.DataTableMapping("Table", "Client", new System.Data.Common.DataColumnMapping[] {
new System.Data.Common.DataColumnMapping("ClientID", "ClientID"),
new System.Data.Common.DataColumnMapping("ClientName", "ClientName")})});
this.sqlDataAdapter.UpdateCommand = this.UpdateComand1;
this.sqlDataAdapter.RowUpdated += new System.Data.SqlClient.SqlRowUpdatedEventHandler(this.sqlDataAdapter_RowUpdated);
this.timeTrackerDataSet.DataSetName = "TimeTrackerDataSet";
this.timeTrackerDataSet.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema;
this.clientBindingSource.DataMember = "Client";
this.clientBindingSource.DataSource = this.timeTrackerDataSet;
this.clientTableAdapter.ClearBeforeFill = true;
}
#endregion
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new clsDataGridForm());
}
private void clsDataGridForm_Load(object sender, System.EventArgs e)
{
the 'timeTrackerDataSet.Client' table. You can move, or remove it, as needed.
this.clientTableAdapter.Fill(this.timeTrackerDataSet.Client);
try
{
fnRefresh();
}
catch(Exception ex)
{
MessageBox.Show("Connection failed..."+ex.Message);
Application.Exit(); //end the program
}
}
private void fnAllButtonsClicks(object sender, System.EventArgs e)
{
Button bt=(Button)sender; //get which button clicked
switch (bt.Text) //button-text
{
case "Insert" : fnInsertNew();
break;
case "Save/Update"
: fnSaveUpdate();
break;
case "Delete" : fnDelete();
break;
case "ReadFromXML"
: fnDataReadingFromXMLFile("C:\\MyXMLdata.xml");
break;
case "Refresh" : fnRefresh();
fnEnableDisableAllButtons(true);
break;
case "CopyToXML": fnCopyToXMLandTextFile();
break;
case "WriteToTextFile"
: fnWriteToTextFile();
break;
case "Exit" : Application.Exit();
break;
}
}
private void fnInsertNew()
{
//keep in mind the previous clicked row to unselect
int iPrevRowindex=this.iRowIndex;
MessageBox.Show("Enter the new record at the end of the DataGrid and click 'Save/Update'-button", "Stop");
this.btInsertnew.Enabled=false;
//get how many records in the table
this.iRowIndex=this.timeTrackerDataSet.Tables["Client"].Rows.Count;
//select the last row
this.dataGrid1.Select(this.iRowIndex);
//unselect the previous row
this.dataGrid1.UnSelect(iPrevRowindex);
}
private void fnSaveUpdate()
{
try
{ //TimeTrackerDataSet
//put the modified DataSet into a new DataSet(myChangedDataset)
DataSet myChangedDataset = this.timeTrackerDataSet.GetChanges();
if (myChangedDataset != null)
{
//get how many rows changed
int modifiedRows = this.sqlDataAdapter.Update(myChangedDataset);
MessageBox.Show("Database has been updated successfully: " + modifiedRows + " Modified row(s) ", "Success");
this.timeTrackerDataSet.AcceptChanges(); //accept the all changes
fnRefresh();
}
else //no changes
{
MessageBox.Show("Nothing to save", "No changes");
}//if-else
}
catch(Exception ex)
{
//if something somehow went wrong
MessageBox.Show("An error occurred updating the database: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.timeTrackerDataSet.RejectChanges(); //cancel the changes
}//try-catch
fnEnableDisableAllButtons(true);
}
private void fnDelete()
{
//ask user if wanting to delete
DialogResult dr=MessageBox.Show("Are you sure you want to delete this row ? ", "Confirm deleting", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dr ==DialogResult.Yes) //if
{
//user clicked the "Delete" button
DataTable tbl=new DataTable("Client");
tbl=this.timeTrackerDataSet.Tables[0];
int i=this.iRowIndex;//get the index of the row you clicked
tbl.Rows[i].Delete();
this.sqlDataAdapter.Update(tbl); this.fnRefresh();
}//if
}
private void dataGrid1_Click(object sender, System.EventArgs e)
{
this.iRowIndex =this.dataGrid1.CurrentRowIndex;
this.btInsertnew.Enabled=true;
fnShowCurrentRecord(this.iRowIndex);
}
r
private void fnShowCurrentRecord(int pos)
{
this.dataGrid1.CaptionText=" Record: "+((pos)+1);
}
//-------------------------------------------------------------------------
//- This is the way to enable or disable ALL the buttons on the Form
//- if flag true all buttons enabled, if false buttons disabled
public void fnEnableDisableAllButtons(bool flag)
{
string str;
foreach(Control ctrl in this.Controls)
{
str = Convert.ToString(ctrl.GetType());
if(str == "System.Windows.Forms.Button")
{
ctrl.Enabled = flag;
}//if
}//foreach
}
private void dataGrid1_Navigate(object sender, NavigateEventArgs ne)
{
}
private void oleDbDataAdapter1_RowUpdated(object sender, OleDbRowUpdatedEventArgs e)
{
}
private void sqlDataAdapter_RowUpdated(object sender, SqlRowUpdatedEventArgs e)
{
}
private void oleDbConnection1_InfoMessage(object sender, OleDbInfoMessageEventArgs e)
{
}
}//class
}//namespace
this is a grid that allows me to edit its rows
ty in advance
private void InitializeComponent()
{
this.SelectComand1.CommandText = "SELECT Client.* FROM Client";
this.SelectComand1.Connection = this.SelectConnection;
this.SelectConnection.ConnectionString = "server=DANI;Trusted_Connection=yes;database=TimeTracker; connection timeout=30";
this.SelectConnection.FireInfoMessageEventOnUserErrors = false;
//
this.InsertComand1.CommandText = "INSERT INTO Client (ClientID,ClientName) VALUES (?,?)";
this.InsertComand1.Connection = this.SelectConnection;
this.InsertComand1.Parameters.AddRange(new System.Data.SqlClient.SqlParameter[] {
new System.Data.SqlClient.SqlParameter("ClientID", System.Data.SqlDbType.NVarChar, 10, "ClientID"),
new System.Data.SqlClient.SqlParameter("ClientName", System.Data.SqlDbType.NVarChar, 50, "ClientName")});
//
this.UpdateComand1.Connection = this.SelectConnection;
this.UpdateComand1.Parameters.AddRange(new System.Data.SqlClient.SqlParameter[] {
new System.Data.SqlClient.SqlParameter("ClientID", System.Data.SqlDbType.NVarChar, 10, "ClientID"),
new System.Data.SqlClient.SqlParameter("ClientName", System.Data.SqlDbType.NVarChar, 50, "ClientName"),
new System.Data.SqlClient.SqlParameter("Original_ClientID", System.Data.SqlDbType.NVarChar, 10, System.Data.ParameterDirection.Input, false, ((byte)(0)), ((byte)(0)), "ClientID", System.Data.DataRowVersion.Original, null),
new System.Data.SqlClient.SqlParameter("Original_ClientName", System.Data.SqlDbType.NVarChar, 50, System.Data.ParameterDirection.Input, false, ((byte)(0)), ((byte)(0)), "ClientName", System.Data.DataRowVersion.Original, null)});
//
this.sqlDataAdapter.DeleteCommand = this.DeleteComand1;
this.sqlDataAdapter.InsertCommand = this.InsertComand1;
this.sqlDataAdapter.SelectCommand = this.SelectComand1;
this.sqlDataAdapter.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
new System.Data.Common.DataTableMapping("Table", "Client", new System.Data.Common.DataColumnMapping[] {
new System.Data.Common.DataColumnMapping("ClientID", "ClientID"),
new System.Data.Common.DataColumnMapping("ClientName", "ClientName")})});
this.sqlDataAdapter.UpdateCommand = this.UpdateComand1;
this.sqlDataAdapter.RowUpdated += new System.Data.SqlClient.SqlRowUpdatedEventHandler(this.sqlDataAdapter_RowUpdated);
this.timeTrackerDataSet.DataSetName = "TimeTrackerDataSet";
this.timeTrackerDataSet.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema;
this.clientBindingSource.DataMember = "Client";
this.clientBindingSource.DataSource = this.timeTrackerDataSet;
this.clientTableAdapter.ClearBeforeFill = true;
}
#endregion
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new clsDataGridForm());
}
private void clsDataGridForm_Load(object sender, System.EventArgs e)
{
the 'timeTrackerDataSet.Client' table. You can move, or remove it, as needed.
this.clientTableAdapter.Fill(this.timeTrackerDataSet.Client);
try
{
fnRefresh();
}
catch(Exception ex)
{
MessageBox.Show("Connection failed..."+ex.Message);
Application.Exit(); //end the program
}
}
private void fnAllButtonsClicks(object sender, System.EventArgs e)
{
Button bt=(Button)sender; //get which button clicked
switch (bt.Text) //button-text
{
case "Insert" : fnInsertNew();
break;
case "Save/Update"
: fnSaveUpdate();
break;
case "Delete" : fnDelete();
break;
case "ReadFromXML"
: fnDataReadingFromXMLFile("C:\\MyXMLdata.xml");
break;
case "Refresh" : fnRefresh();
fnEnableDisableAllButtons(true);
break;
case "CopyToXML": fnCopyToXMLandTextFile();
break;
case "WriteToTextFile"
: fnWriteToTextFile();
break;
case "Exit" : Application.Exit();
break;
}
}
private void fnInsertNew()
{
//keep in mind the previous clicked row to unselect
int iPrevRowindex=this.iRowIndex;
MessageBox.Show("Enter the new record at the end of the DataGrid and click 'Save/Update'-button", "Stop");
this.btInsertnew.Enabled=false;
//get how many records in the table
this.iRowIndex=this.timeTrackerDataSet.Tables["Client"].Rows.Count;
//select the last row
this.dataGrid1.Select(this.iRowIndex);
//unselect the previous row
this.dataGrid1.UnSelect(iPrevRowindex);
}
private void fnSaveUpdate()
{
try
{ //TimeTrackerDataSet
//put the modified DataSet into a new DataSet(myChangedDataset)
DataSet myChangedDataset = this.timeTrackerDataSet.GetChanges();
if (myChangedDataset != null)
{
//get how many rows changed
int modifiedRows = this.sqlDataAdapter.Update(myChangedDataset);
MessageBox.Show("Database has been updated successfully: " + modifiedRows + " Modified row(s) ", "Success");
this.timeTrackerDataSet.AcceptChanges(); //accept the all changes
fnRefresh();
}
else //no changes
{
MessageBox.Show("Nothing to save", "No changes");
}//if-else
}
catch(Exception ex)
{
//if something somehow went wrong
MessageBox.Show("An error occurred updating the database: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.timeTrackerDataSet.RejectChanges(); //cancel the changes
}//try-catch
fnEnableDisableAllButtons(true);
}
private void fnDelete()
{
//ask user if wanting to delete
DialogResult dr=MessageBox.Show("Are you sure you want to delete this row ? ", "Confirm deleting", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dr ==DialogResult.Yes) //if
{
//user clicked the "Delete" button
DataTable tbl=new DataTable("Client");
tbl=this.timeTrackerDataSet.Tables[0];
int i=this.iRowIndex;//get the index of the row you clicked
tbl.Rows[i].Delete();
this.sqlDataAdapter.Update(tbl); this.fnRefresh();
}//if
}
private void dataGrid1_Click(object sender, System.EventArgs e)
{
this.iRowIndex =this.dataGrid1.CurrentRowIndex;
this.btInsertnew.Enabled=true;
fnShowCurrentRecord(this.iRowIndex);
}
r
private void fnShowCurrentRecord(int pos)
{
this.dataGrid1.CaptionText=" Record: "+((pos)+1);
}
//-------------------------------------------------------------------------
//- This is the way to enable or disable ALL the buttons on the Form
//- if flag true all buttons enabled, if false buttons disabled
public void fnEnableDisableAllButtons(bool flag)
{
string str;
foreach(Control ctrl in this.Controls)
{
str = Convert.ToString(ctrl.GetType());
if(str == "System.Windows.Forms.Button")
{
ctrl.Enabled = flag;
}//if
}//foreach
}
private void dataGrid1_Navigate(object sender, NavigateEventArgs ne)
{
}
private void oleDbDataAdapter1_RowUpdated(object sender, OleDbRowUpdatedEventArgs e)
{
}
private void sqlDataAdapter_RowUpdated(object sender, SqlRowUpdatedEventArgs e)
{
}
private void oleDbConnection1_InfoMessage(object sender, OleDbInfoMessageEventArgs e)
{
}
}//class
}//namespace
this is a grid that allows me to edit its rows
ty in advance