Question

When I try to access a control, it's NULL.

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" ... DeleteMethod="DeleteEntry">
public void DeleteEntry(long entryID)
{

  try 
  {
     ... Delete ...
  }
  catch (Exception ex)
  {
     lblErrorMessage.Text = ... => lblErrorMessage is NULL!
  }
}

I cannot access page controls. What's another way to return an error message to the user?

Was it helpful?

Solution

On your form code behind you can catch the error here. I created a subclass of the Exception object and throw that so I know what I'm catching.

Page code:

protected void ObjectDataSource1_Deleted(object sender, ObjectDataSourceStatusEventArgs e)
{
    if (e.Exception != null)
    {
        if (e.Exception.InnerException is MyException)
        {
            lblErrorMessage.Text = e.Exception.InnerException.Message;
            e.ExceptionHandled = true;
        }
    }
}

Object code:

try
{
    // Whatever
}
catch (Exception ex)
{
    throw new MyException();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top