Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

showing RaiseError message in ASP.NET page

Status
Not open for further replies.

sds814

Programmer
Feb 18, 2008
164
US
Can anyone provide a link that shows the proper way of raising an error in a stored procedure that inserts records and showing that error on an ASP.NET page?

Thanks!
 
The error messages from SQLServer (Raiserror) are available in ASP.NET Here is how I used them:

Code:
 SET NOCOUNT ON;
                IF EXISTS
                (
                        SELECT  1
                        FROM    dbo.People
                        WHERE   UserName = @UserName
                            AND SiteID   = @SiteID
                )
                BEGIN
                        RAISERROR (N'UserName %s already exists. Please choose another username.',
                        16, -- Severity,
                        1,  -- State,
                        @UserName)
                        RETURN -1
                END

This is in the SP.

Here is some code in ASP.NET

Code:
protected void Form_ItemInserted(object sender, FormViewInsertedEventArgs e)
    {
        string Text;
        bool CloseWindow = false;

        if (e.Exception == null)
        {
            if (Request.QueryString["pid"] != null && Request.QueryString["pid"].Length > 0)
            {               
                Text = string.Format("You just inserted {0} {1}", e.Values["FirstName"], e.Values["LastName"]);
                this.UPDATE_OPENER.Text = "<script language=\"JavaScript\">changeParent('" + Request.QueryString["pid"] + "','" + e.Values["LastName"] + ", " + e.Values["FirstName"] + " " + (e.Values["MiddleName"] ?? "") + "'," + 
                    this.NewPersonID + ",'" + Text.Replace("'","\\'")  + "');</script>";
                CloseWindow = true;
                this.UPDATE_OPENER.Visible = true;     
            }
            else
            {
                Text = string.Format("You just inserted {0} {1} <a href='peopleV.aspx?perid={2}'>" +
                   "Edit Here</a>", e.Values["FirstName"], e.Values["LastName"], this.NewPersonID);
                this.DisplayMessage(Text, CloseWindow);
                this.MltvwViewForms.Visible = false;
            }
        }
        else // Exception case
           {
                Text = "The data was entered in an incorrect format, please check it over." +
                       " " + e.Exception.Message.Replace(Environment.NewLine, "\\n").Replace("\"", "\\\"");
                this.DisplayMessage(Text, CloseWindow);

                e.ExceptionHandled = true;
                e.KeepInInsertMode = true;
            }
    }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top