Search code examples
vb.nettry-catchsqldatasource

How to catch error while inserting data with SqlDataSource


I have a SqlDataSource and a GridView.

What I want to do is, while the query is executed (i.e. for inserting a data), then after the data has inserted successfully, it should appear a message sounds: "The data deleted successfully". I've solved it by using GridView1_RowDeleted method.

Now the problem is, I want to catch the error while the query is failed to executed. If the query has failed to execute, then it should appear a message: "The data failed to insert.".

How to do it? I don't have an idea about this.

Need your help guys.

Thanks a lot.


Solution

  • You should be able to add a handler for the relevant event: inserted, deleted. Then, in the handler look at the SqlDataSourceStatusEventArgs property Exception. If it's not null then an exception has occurred. For example if the selected command threw an exception:

    protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
    {
       if (e.Exception != null)
       {
           // handle the exception
       }
    }
    

    --