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

How to handle a exception error in c#?

1 Answer 203 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
Yoongu
Top achievements
Rank 1
Yoongu asked on 25 Mar 2015, 08:17 AM
Hi,

I use the "try...catch" in order to save the data to the server.
And I was exception handling in the "throw ex".

However, this doesn't display page when an error occurs.
So I have to debug to check the exception message.

In such a case, do I need to process How?

here my code.

aspx
    <title></title>
    <telerik:RadScriptBlock ID="RadScriptBlock1" runat="server">
    <script type="text/javascript">
        function Update() {
            var batchManager = grid2.get_batchEditingManager();
            batchManager.saveChanges(grid2.get_masterTableView());
        }
......
<body>
    <form id="form" runat="server">
    <div>
       <telerik:RadAjaxPanel ID="RadAjaxPanel2" runat="server" Height="100%" Width="100%" HorizontalAlign="NotSet" OnAjaxRequest="RadAjaxPanel2_AjaxRequest">
            <telerik:RadGrid ID="RadGrid2" runat="server" AutoGenerateColumns="False" Culture="ko-KR" GroupPanelPosition="Top" OnNeedDataSource="RadGrid2_NeedDataSource" OnBatchEditCommand="RadGrid2_BatchEditCommand" OnItemCreated="RadGrid2_ItemCreated" Height="445px" OnPreRender="RadGrid2_PreRender" AllowAutomaticUpdates="True" ShowFooter="True">
                <ClientSettings AllowColumnsReorder="True" ReorderColumnsOnClient="True" AllowKeyboardNavigation="True">
                    <ClientEvents OnRowCreated="RowCreated" OnBatchEditCellValueChanged="RadGrid2_OnBatchEditCellValueChanged" />
                    <KeyboardNavigationSettings AllowSubmitOnEnter="True" CancelChangesKey="D1" />
                    <Scrolling AllowScroll="True" UseStaticHeaders="True" />
                </ClientSettings>
                <MasterTableView EditMode="Batch" BatchEditingSettings-EditType="Cell">
                    <Columns>
............


aspx.cs
....................
        protected override void OnPreInit(EventArgs e)
        {
            baseScriptManager = new ScriptManager();
            baseScriptManager.AsyncPostBackTimeout = 600;
            Page.Items[typeof(ScriptManager)] = baseScriptManager;
 
            base.OnPreInit(e);
        }
 
        protected override void OnPreLoad(EventArgs e)
        {
            Page.Items[typeof(ScriptManager)] = null;
            baseScriptManager.AllowCustomErrorsRedirect = true;
            baseScriptManager.AsyncPostBackError += this.baseScriptManager_AsyncPostBackError;
 
            this.Form.Controls.Add(baseScriptManager);
 
            base.OnPreLoad(e);
        }
        private void baseScriptManager_AsyncPostBackError(object sender, AsyncPostBackErrorEventArgs e)
        {
            baseScriptManager.AsyncPostBackErrorMessage = e.Exception.Message;
        }
        protected void RadGrid2_BatchEditCommand(object sender, GridBatchEditingEventArgs e)
        {
            ................
 
            using (DbConnection connection = db.CreateConnection())
            {
                connection.Open();
                DbTransaction transaction = connection.BeginTransaction();
                try
                {
                    DbCommand dbCommand = null;
                    foreach (DBParameter DBParameter in parameters)
                    {
                        dbCommand = db.GetStoredProcCommand("uspw_test1");
                        dbCommand.CommandTimeout = commandTimeout;
                        if (DBParameter != null)
                        {
                            foreach (Parameter parameter in DBParameter.ListParameter)
                                db.AddInParameter(dbCommand, parameter.Name, parameter.DBType, parameter.Value);
                        }
 
                        result += db.ExecuteNonQuery(dbCommand, transaction);
                    } 
                    // Commit the transaction
                    transaction.Commit();
                }
                catch (System.Exception ex)
                {
                    // Rollback transaction
                    transaction.Rollback();
                    throw ex;
                }
                connection.Close();
 
                return;
            }
        }

1 Answer, 1 is accepted

Sort by
0
Angel Petrov
Telerik team
answered on 30 Mar 2015, 05:57 AM
Hello,

I am experiencing difficulties understanding the query. Do you want to display the exception message to the user?

In general it is a string and you should be able to display it in some control after the insert/update fails. An example of such an approach can be observed in this online demo where the exception is displayed in a RadListBox.

Regards,
Angel Petrov
Telerik
 

See What's Next in App Development. Register for TelerikNEXT.

 
Tags
Ajax
Asked by
Yoongu
Top achievements
Rank 1
Answers by
Angel Petrov
Telerik team
Share this question
or