locked
cshtml db query error handling RRS feed

  • Question

  • User616014865 posted

    I am running the db SP query from Razor.   

    @{

    Var objConn = Database.Open("connectionA");

    var strSQL = "EXEC SP @ID= @0";

    var data = objConn.Query(strSQL, Request["id"]);

    }

     I  like to show any error from this query.  I will use try and catch and show the error as a popup (alert).

    Please let me know how to do this in

    1) Inside Razor head (@...)

    2) Razor and HTML combo

    3) Razor and Javascript

    4) Razor and JQuery

    Thank you  in advance.

    Friday, May 1, 2015 4:27 PM

Answers

  • User-821857111 posted
    @{
        var message = "";
        try{
            var objConn = Database.Open("connectionA");
            var strSQL = "EXEC SP @ID= @0";
            var data = objConn.Query(strSQL, Request["id"]);
        }
        catch(Exception ex)
        {
            message = ex.Message;
        }
    }
    
    
    
    <!DOCTYPE html>
    <html lang="en">
        <head>
            <meta charset="utf-8" />
            <title></title>
            <script>
                @if(!message.IsEmpty()){
                    <text>alert('@message');</text>
                }
            </script>
        </head>
        <body>
    
        </body>
    </html>
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 2, 2015 2:50 AM

All replies

  • User-821857111 posted

    If you want to show it as a popup, you will need to use Razor to generate javascript to show the popup if there is an error.

    Friday, May 1, 2015 4:39 PM
  • User616014865 posted

    Please tell me how to do that..  I mean generating javascript from Razor and input the string into it.

    Friday, May 1, 2015 7:01 PM
  • User-821857111 posted
    @{
        var message = "";
        try{
            var objConn = Database.Open("connectionA");
            var strSQL = "EXEC SP @ID= @0";
            var data = objConn.Query(strSQL, Request["id"]);
        }
        catch(Exception ex)
        {
            message = ex.Message;
        }
    }
    
    
    
    <!DOCTYPE html>
    <html lang="en">
        <head>
            <meta charset="utf-8" />
            <title></title>
            <script>
                @if(!message.IsEmpty()){
                    <text>alert('@message');</text>
                }
            </script>
        </head>
        <body>
    
        </body>
    </html>
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 2, 2015 2:50 AM