I have the code for deleting a record but i noticed even though user cancels the action it removes the current record from the form.
Any ideas how can i stop the action when user cancel the action on the confirmation message box?
| private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e) { if (MessageBox.Show("Delete?", "Confirm Delete", MessageBoxButtons.OKCancel) == DialogResult.OK) MessageBox.Show("DELETE"); else { //Code for Cancel action MessageBox.Show("ABORT"); } } |
Thank you in advance.
bkwdesignPosted Dec 23, 2014, 10:38 AM
1. First go to bindingnavigator's properties window.
2. Then set 'DeleteItem' property to '(none)'.
3. Then write the following code under bindingNavigatorDeleteItem_Click event.
if (MessageBox.Show("Delete?", "Confirm Delete", MessageBoxButtons.OKCancel) == DialogResult.OK)
{PerformDataDeleteLogic();
bindingSource.RemoveCurrent();
MessageBox.Show("Success");
}
else
{
MessageBox.Show("Abort");
}DealyPosted Oct 28, 2009, 4:01 PM
I will try to change it in order to work with sproc instead of having sql statements in my code.
I hope this works for me!
Thanks again :)
theLizardPosted Oct 27, 2009, 7:04 PM
//=================================
using System;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;
//copyright theLizard 2006 - 2009
//
///
/// Summary description for sql
///
public class sqlCon
{
public SqlConnection con = null;
public SqlDataReader reader = null;
public SqlCommand cmd = null;
public sqlCon()
{
cmd = new SqlCommand();
}
//-----------------------------------------------------------------
public void connect()
{
System.Configuration.AppSettingsReader asr = new System.Configuration.AppSettingsReader();
con = new SqlConnection((string)asr.GetValue("Connection", typeof(string)));
con.Open();
}
//-----------------------------------------------------------------
private static SqlDataAdapter SetupDataAdapter(String sqlString)
{
// Assuming all the default settings, create a SqlDataAdapter working
// current database in SQL Server.
System.Configuration.AppSettingsReader asr = new System.Configuration.AppSettingsReader();
return new SqlDataAdapter(sqlString, (string)asr.GetValue("Connection", typeof(string)));
}
//-----------------------------------------------------------------
public string get(string field)
{
return reader[field].ToString();
}
public void ExecuteReader()
{
reader = cmd.ExecuteReader();
}
//-----------------------------------------------------------------
public SqlDataReader ExecReader()
{
return (cmd.ExecuteReader());
}
///
///
///
//-----------------------------------------------------------------
public void write()
{
int i = cmd.ExecuteNonQuery();
}
//-----------------------------------------------------------------
public bool read()
{
return reader.Read();
}
//-----------------------------------------------------------------
public void command(String s)
{
cmd.Connection = con;
cmd.CommandText = s;
}
//-----------------------------------------------------------------
public void terminate()
{
try
{
if (reader != null)
{
reader.Close();
reader.Dispose();
}
}
catch (Exception err)
{
}
try
{
if (cmd != null)
{
cmd.Connection.Close();
cmd.Connection.Dispose();
}
}
catch (Exception err)
{
}
try
{
if (con != null)
{
con.Close();
con.Dispose();
}
}
catch (Exception err)
{
}
openConnections(false);
}
//-----------------------------------------------------------------
public void beginTrans()
{
cmd.Transaction = con.BeginTransaction();
}
//-----------------------------------------------------------------
public void commitTrans()
{
cmd.Transaction.Commit();
}
//-----------------------------------------------------------------
public void rollbackTrans()
{
try
{
if (cmd != null)
cmd.Transaction.Rollback();
}
catch (Exception err)
{
}
}
//-----------------------------------------------------------------
public void openConnections(bool add)
{
}
//-----------------------------------------------------------------
public bool execSQL(string s)
{
bool success = true;
sqlCon con = new sqlCon();
try
{
con.connect();
con.command(s);
con.beginTrans();
con.write();
con.commitTrans();
}
catch (Exception err)
{
con.rollbackTrans();
success = false;
}
finally
{
con.terminate();
}
return (success);
}
//-----------------------------------------------------------------
}
//app configuration file
//on your form
private void button2_Click(object sender, EventArgs e)
{
sqlCon q = new sqlCon(); //class with SQL Records handling
q.connect();
q.command("SELECT * FROM Customer ORDER BY name");
q.ExecuteReader();
if (q.read()) //this can be a while if you are loading a grid or a combo box
{
orderID_txt.Text = q.get("id");
custID_txt.Text = q.get("name");
}
q.terminate();
}
Where you put this code depends on what you want to achieve
As for navigating records, there are a number of ways you can do this, 1st you can create an array of ids then use the array to retrieve records like
load the array with ALL the ids from the DB using a
while(q.read())
{
......
}
in ButtonNext_click
recordId = array[currentIndex +1];
currentIndex ++;
in ButtonPrevious_Click
recordId = array[currentIndex -1];
currentIndex --;
q.command("SELECT * FROM Table WHER id = " + recordId)
q.ExecuteReader();
if(q.read()) //this can be a while if you are loading a grid or a combo box
{
orderID_txt.Text = q.get("OrderID");
custID_txt.Text = q.get("CustomerId");
}
q.terminate();
in DeleteRecord_Click
if(messagebox .... ) == ID_YES)
{
q.execSql("DELETE FROM Customer WHERE id = " + recordId);
// at this point you would also delete the array element by looping until you find the record id to remove from the arry
// or simply reload the array from the DB, whichever is easire for you.
// you should also ensure that if there are records in other tables which rely on the record you are deleting that you also
// delete these records by issuing a multi statement sql.
// execSQL will take care of errors and roll back any transactions that fail.
// eg. string sql = "DELETE * FROM siblingTable WHERE ownerId = " + IntToStr(recordId);
// sql += ", DELETE FROM table WHERE id=" + IntToStr(recordId) if any part fails the transaction will roll back...
}
hope you understand this...
DealyPosted Oct 27, 2009, 6:01 AM
If it's not too much trouble can you change the code in the example I sent you for me?
Thanks again for all the help.
theLizardPosted Oct 27, 2009, 5:45 AM
records all you need is the method to assign field values to text and combo boxes its not that much more code and you will be more able to get jobs done without needing to resort to components you have little control over.
public SqlDataReader Reader = null;
public string get(string field)
{
return Reader[field].ToString();
}
TextBox1.Text = ClassNameHoldingFuncttion.get("CustomerId");
there is a bit more to this but you can do everything you need to without a binding navigator, research SqlDataReader.
You should control whether changes are saved or if you really want to delete a record or not.
DealyPosted Oct 27, 2009, 3:18 AM
http://rapidshare.com/files/298481532/NorthWindTesting.rar
It uses Northwind database, I added the stored procedures as well.
Thank you!
theLizardPosted Oct 23, 2009, 7:28 AM
can you post your form so I can get a picture of what you are trying to do,
If you are using SQL Server you may be interested in the controls I have created to handle data entry forms without using data aware controls.
without a bindingNavigator you have a add, edit, delete, save and cancel button, a Next and Previous buttons.
then you create your sql statements for the various events,
then you could use the code that you have and control all aspects of the work flow.
A standard grid to show records and standard buttons to do what the bindingNavigaor does without the head aches.
eg:
if(MessageBox( whatever the message.......)== ID_OK)
if(con.ExecuteNoQuery("DELETE FROM Orders WHERE id=23") == 0)
MessagebOX(SUCCESS....);
ELSE
//nothing to do really because there are no pending actions as you would have using the bindingNavigator.
do you understand what I am saying?
Nilanka DharmadasaPosted Oct 23, 2009, 7:05 AM
You can try this method.
1. First go to bindingnavigator's properties window.
2. Then set 'DeleteItem' property to '(none)'.
3. Then write the following code under bindingNavigatorDeleteItem_Click event.
DealyPosted Oct 23, 2009, 6:47 AM
theLizardPosted Oct 23, 2009, 6:21 AM
try this in the else part of your if statement
BindingSourceName.CancelEdit()
You may also have to do a BindingSourceName.ResetBindindings or BindingSourceName.ResetItem
DealyPosted Oct 23, 2009, 5:45 AM
theLizardPosted Oct 23, 2009, 5:38 AM
See if EventArgs e has an cancel like e.Cancel = true;
else
MessageBox.Show("ABORT");}
DealyPosted Oct 23, 2009, 5:25 AM
If I abort the action the record dissappears from the bindingNavigator (like is being deleted) and i have to refresh the bindingSource in order to get it again.
theLizardPosted Oct 23, 2009, 5:15 AM
Is the record deleted from the database? if not, how did you cancel the delete operation, I cannot see any code in any example in this post that shows the operation is canceled when the bindingNavigatorDeleteItem_Click event is fired.
DealyPosted Oct 23, 2009, 4:49 AM
Nilanka DharmadasaPosted Oct 19, 2009, 11:18 PM
This will help you overcome the said issue. If you still have any problem let me know.
private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
{
btnCommit.PerformClick();
}
private void btnCommit_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Delete?", "Confirm Delete", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
//Write your code to delete the item here.
MessageBox.Show("DELETE");
}
else
{
//Code for Cancel action
MessageBox.Show("ABORT");
return; //This line will help you to abort the action.
}
}
Kirtan PatelPosted Oct 19, 2009, 11:23 AM
Its Happening Because its a BindingNaviagtor and its performing some operation automatic after binding it to source so this is also its automatic operation .and we are performing manual methods on it :)
so we cant to on what it does automatically so we have to find some way as i suggested you in your other post :)
if you use your own button control then it will not be the issue :)
thank you :)
DealyPosted Oct 19, 2009, 11:01 AM
I still have the same problem though. I need to refresh the form in order to get the record that i aborted the deletion.
Nilanka DharmadasaPosted Oct 19, 2009, 8:28 AM
private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Delete?", "Confirm Delete", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
//Write your code to delete the item here.
MessageBox.Show("DELETE");
}
else
{
//Code for Cancel action
MessageBox.Show("ABORT");
return; //This line will help you to abort the action.
}
}
Kirtan PatelPosted Oct 16, 2009, 6:56 AM
because code i provided only fires events on correct button press :-)
DealyPosted Oct 16, 2009, 6:53 AM
If you check my code, you will see this i what i do. The problem is that it still removes the CurrentItem :S
Kirtan PatelPosted Oct 16, 2009, 6:45 AM
Here it is how to do that :-)
private void button1_Click(object sender, EventArgs e)
{
DialogResult x= MessageBox.Show("Are you Sure Delete?", "Delete Confirmation", MessageBoxButtons.OKCancel);
if (x == DialogResult.OK)
{
MessageBox.Show("Deleted");
}
else if (x == DialogResult.Cancel)
{
MessageBox.Show("Canceled ");
}
}