This is a migrated thread and some comments may be shown as answers.

Send mail on insert in a RadGrid

4 Answers 65 Views
Grid
This is a migrated thread and some comments may be shown as answers.
manu
Top achievements
Rank 1
manu asked on 29 Sep 2010, 09:53 AM

Hi:

I have a RadGrid, the Radgrid contains  a form template, his control are binding to a SqlDataSource.

  

When insert an element sends a mail (with .net libraries) into a mail that the user have inserted in a specific field. 

The code works fine, but when the user introduce a invalid mail like  'badmail@badmail.com' when the user press insert button,  send of mail fail (I manage the error in the event), radgrid shows what happen,  but all fields are empty.

The code is the following

01.  protected void SqlDataSource1_Inserting(object sender, SqlDataSourceCommandEventArgs e)
02.  {        
03.  
04.      
05.  
06.        SqlDataSource1_Inserted        
07.        DbCommand command = e.Command;
08.        DbConnection cx = command.Connection;
09.        cx.Open();
10.        DbTransaction tx = cx.BeginTransaction();
11.        command.Transaction = tx;
12.    }
13.  
14.  
15.  
16.  
17.protected void SqlDataSource1_Inserted(object sender, SqlDataSourceStatusEventArgs e)
18.{
19. try
20. {
21.        DbCommand command = e.Command;
22.        DbTransaction tx = command.Transaction;
23.        //Here code that sends email, process can fail.
24.        tx.Commit();
25. }
26. catch (Exception exception)
27. {
28.        tx.Rollback();
29.        throw exception;
30. }
31.}
32.  
33.protected void RadGrid1_ItemInserted(object source, Telerik.WebControls.GridInsertedEventArgs e)
34.    {
35.        if (e.Exception != null)
36.        {
37.            e.ExceptionHandled = true;
38.            e.KeepInInsertMode = true;
39.            //Display a message to show the error
40.        }
41.    }

I whish that when the sent of mail fails, all the data of the form were preserve ┬┐Is it possible?

Thanks.

4 Answers, 1 is accepted

Sort by
0
Iana Tsolova
Telerik team
answered on 01 Oct 2010, 03:27 PM
Hello manu,

To achieve your goal, you can cancel the PerformInsert command on InsertCommand or ItemCommand event. I suggest that you move the code for sending email there and if that fail, just set e.Canceled to true to cancel the insert command. The same is for the Update command.

Best wishes,
Iana
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
manu
Top achievements
Rank 1
answered on 05 Oct 2010, 07:36 AM

Hi:

I don't move the code in this events cause I need send mail when the record is inserted, so I make rollback to transaction if the sending fail.

SqlDataSource1_Inserted has a SqlDataSourceStatusEventArgs which have the command.Cancel() method, but if I invoke this, data disappear.

Can you help me in my goal?

Thanks a lot.

0
Iana Tsolova
Telerik team
answered on 06 Oct 2010, 02:18 PM
Hi manu,

In this case I would suggest that you consider performing the database operations manually instead of using automatic CRUD operations. In this case, you will be able to handle everything in the ItemCommand event of the grid. There you can try sending the email and if that fails, just cancel the Insert/Update command of the grid so the edit form stays as it is and do not make any updates in the database. If the email sending does not fail, just perform the database updates. I assume that this solution is easier to implement and follows rather straight forward logic. It is also better from performance point of view.

Regards,
Iana
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
manu
Top achievements
Rank 1
answered on 08 Oct 2010, 07:11 AM

Hi:

I'm going review the code to make manually the operation. 

Thanks for your answer.

Tags
Grid
Asked by
manu
Top achievements
Rank 1
Answers by
Iana Tsolova
Telerik team
manu
Top achievements
Rank 1
Share this question
or