Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi, I have the code below but is it possible to return the exception message from C# class file to aspx.cs so I can return it to use with a customized message box? (ie. customized message box would be a modal in bootstrap)

What I have tried:

Here is how I call the method in class file

C#
parameters = new string[] { "@TrxNum", "@ShortDesc", "@PaymentOption" };
vals = new string[] { lblTrxNum.Text, txtShortDesc.Text, drpPaymentOptions.SelectedValue };
DataTable dt = reimburseClass.getDataTableParameter("[NewSP_InsertUpdateReimburseTrxHDR]", parameters, vals);


Here is my c# class code
C#
public DataTable getDataTableParameter(string qry, string[] param, string[] values)
    {
        dt = new DataTable();
        try
        {
            con.Open();
            cmd = new SqlCommand(qry, con);
            cmd.CommandType = CommandType.StoredProcedure;
            for (int i = 0; i <= param.Length - 1; i++)
            {
                cmd.Parameters.AddWithValue(param[i], values[i]);
            }
            da = new SqlDataAdapter(cmd);
            da.Fill(dt);
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            con.Close();
        }
        return dt;
    }



If this C# Class code return exception, how can I pass the message to the aspx.cs ?
Posted
Updated 5-Dec-16 17:13pm
v2

Put a try/catch block around the call to that method, and in the catch section, display the exceptions message property to the user.
 
Share this answer
 
Along with John's suggestion (which is quick and good), you can use a ref variable. The calling method will look like the following (please not the new string variable, and a new parameter is passed in the function call):

C#
parameters = new string[] { "@TrxNum", "@ShortDesc", "@PaymentOption" };
vals = new string[] { lblTrxNum.Text, txtShortDesc.Text, drpPaymentOptions.SelectedValue };

string ExceptionMsg = "";

DataTable dt = reimburseClass.getDataTableParameter("[NewSP_InsertUpdateReimburseTrxHDR]", parameters, vals, ref ExceptionMsg);

if (!ExceptionMsg.Equals(""))   // If there is something in the 'ExceptionMsg'.
    {
    ...........
    ...........
    }


The method getDataTableParameter now looks like the following. See a new parameter is added at the end, and there is a change in the CATCH block.

C#
public DataTable getDataTableParameter(string qry, string[] param, string[] values, ref string ExceptionMsg)
{
    dt = new DataTable();
    try
    {
        con.Open();
        cmd = new SqlCommand(qry, con);
        cmd.CommandType = CommandType.StoredProcedure;
        for (int i = 0; i <= param.Length - 1; i++)
        {
            cmd.Parameters.AddWithValue(param[i], values[i]);
        }
        da = new SqlDataAdapter(cmd);
        da.Fill(dt);
    }
    catch (Exception ex)
    {
        ExceptionMsg = Ex.Message;
    }
    finally
    {
        con.Close();
    }
    return dt;
}
 
Share this answer
 
v2
Comments
Chris Ross 2 6-Dec-16 6:46am    
My three stars - in general the idea is fine.
Regarding use of 'ref' - given that the exception message is only ever outbound, I would recommend 'out', not 'ref'.
Regarding (in the caller) testing for !ExceptionMsg.Equals("") I would play it a little safer, with !string.IsNullOrWhiteSpace(ExceptionMsg) ... which still tests for empty strings but also for null and strings that have no meaningful content (i.e. are all white space).
Mehedi Shams 6-Dec-16 17:08pm    
Hi Chris, thanks for your improvement suggestions. These are good. 'ref' and 'out' need a little more exploration. In general 'out' should be practiced more than 'ref' as 'ref' incurs carrying a value over the network (both way), whereas 'out' has overhead for one way only.

Just on usage, if 'out' is used that has to be initialized in the called method (as a rule - an 'out' variable must be initialized).

Thanks again. Cheers :)!
bjay tiamsic 6-Dec-16 19:23pm    
Hi. Thanks for this and I am currently trying to use your suggestion. However, when I replaced 'ref' with 'out', the 'return dt;' line prompts error saying "The out parameter 'ExceptionMsg' must be assigned to before control leaves the current method"
Mehedi Shams 6-Dec-16 19:29pm    
Hi Bjay, if you see the last line of my earlier msg - 'out' variable needs to be initialized before it can be used.

At the beginning of the called method, just put 'ExceptionMsg = "";'. That will do :).
bjay tiamsic 7-Dec-16 3:03am    
It works perfectly! Thank you so much!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900