?
Solved

Where should I Try/Catch the ObjectDataSource AutoBinding?

Posted on 2006-04-27
2
Medium Priority
?
296 Views
Last Modified: 2012-06-27
When DropDownLists are bound declaratively to ObjectDataSource objects, how can I try catch their binding process to ensure the user doesn't see an ugly error in the event that the binding crashes.

I thought maybe I could override the Page's DataBind method but it doesn't seem to fire.  Any insights are appreciated.

Thanks.

Scott.
0
Comment
Question by:tradeline
2 Comments
 
LVL 21

Accepted Solution

by:
MogalManic earned 2000 total points
ID: 16560947
You can handle the error on the page with the OnError method:
  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuitemplatecontrolclassonerrortopic.asp


Just implement the following:
 void Page_Load(object sender, System.EventArgs e)
 {
   Page.Error += new System.EventHandler(Error_Handler);
   //...Rest of page_load...
 }
 
 void Error_Handler(object sender,System.EventArgs e)
 {
      Exception ex=Request.GetLastException();

      MessageLiteral.Text="<b>An error occured while binding the page:"
             +ex.Message
             +"</b>";
 }
0
 
LVL 10

Expert Comment

by:SystemExpert
ID: 16561218
Hi
This is the link whee i found this code
http://authors.aspalliance.com/aldotnet/examples/masterdetail_ddl_datagrid.aspx

Thanks
=========================================================

<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<script language="VB" runat="server">
 Sub Page_Load(sender As Object, e As EventArgs)
      
      Dim myConnection As SqlConnection = new SqlConnection(ConfigurationSettings.AppSettings("DSN_pubs"))

      If Not Page.IsPostBack Then            
        Dim myCommand As SqlCommand = new SqlCommand("Select pub_id, pub_name From publishers", myConnection)
      
        Dim myDataReader As SqlDataReader

        Try
          myConnection.Open()

          myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)

           myDropDownList.DataSource = myDataReader
          myDropDownList.DataBind()
        Catch myException As Exception
          Response.Write("An error has occurred: " & myException.ToString())
        Finally
          If Not myDataReader Is Nothing Then
            myDataReader.Close()
           End If
            
              myDropDownList.Items.Insert(0, "Select an Item")
           myDropDownList.SelectedIndex = 0
        End Try
      End If      

      'We don't want this code to run when the default item is selected
      If Not myDropDownList.SelectedIndex = 0 Then
        myDataGrid.Visible = true

        Dim myCommand As SqlCommand = new SqlCommand("Select * From employee Where pub_id=" & myDropDownList.SelectedItem.Value, myConnection)
        
        Dim myDataReader As SqlDataReader

        Try
          myConnection.Open()

          myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)

          myDataGrid.DataSource = myDataReader
          myDataGrid.DataBind()
        Catch myException As Exception
          Response.Write("An error has occurred: " & myException.ToString())
        Finally
          If Not myDataReader Is Nothing Then
            myDataReader.Close()
          End If
        End Try
      Else
        myDataGrid.Visible = false
      End If

 End Sub
</script>
<body>
<form runat="Server">
<asp:DropDownList id="myDropDownList"
      runat="server"
      DataTextField="pub_name"
      DataValueField="pub_id"
      AutoPostBack="true" />
<hr>
<asp:DataGrid id="myDataGrid"
        runat="server" />
</form>
</body>
</html>

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Integration Management Part 2
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question