Click here to Skip to main content
16,016,345 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have a combo Box where I can get display record in Access Database and then I can also delete the record !
The button function works fine and the code is like this !
C#
private void btnDelete_Click(object sender, EventArgs e)
{
    Person p = new Person();
    p = cmbPersons.SelectedItem as Person;

    b.Delete(p);

}


Now I want to add a confirmation when button is clicked, " The data containing abc has been deleted"
My question is do I need to add something only in this button part ? or I need to add something in this part also ?
C#
public void Delete(Person p)
       {
           try
           {
               command.CommandText = "DELETE FROM TPersons WHERE ID= "+p.Id;
               command.CommandType = CommandType.Text;
               connection.Open();

               command.ExecuteNonQuery();

           }
           catch (Exception)
           {
               throw;
           }
           finally
           {
               if (connection != null)
               {
                   connection.Close();
               }
           }
       }
Posted
Comments
[no name] 26-Feb-14 7:11am    
What do u meant by the question part?
It's not clear and quite contradictory.
Confirmation mean assent.
but... the message 'The data containing abc has been deleted' is status/ack .
confirmation - pre deletion and
ur message text is a status - post deletion,
be clear..

Add Below code into "btnDelete" button :

OnClientClick = "return confirm('Are you sure you want to delete the selected Item?');"
 
Share this answer
 
ExecuteNonQuery returns an int containing the number of rows affected so you could just put an if statement around your query execution
C#
if(command.ExecuteNonQuery() > 0)
{
     MessageBox.Show("Person deleted");
}
else
{
     MessageBox.show("Person not found");
}
 
Share this answer
 
v2
Comments
Jahangir Khan 26-Feb-14 6:16am    
and still I have add something in the buttoun part also ???
Paddy84 26-Feb-14 6:30am    
No. The call to the Delete function, when the button is clicked, will display the message. But only if a Person has been deleted from the database.
So you could add an else statement also to give information to the user. Check the edited solution above.
Jahangir Khan 26-Feb-14 7:40am    
Ok I am getting red wavey lines under MessageBox, it say Message Box does not exit in the current context ? any ideas
Paddy84 26-Feb-14 7:54am    
Sounds like you don't have the reference. Are you using Visual Studio?
Check if you have this reference at the top of main.

using System.Windows.Forms;

You can add it only in the button part.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900