473,405 Members | 2,444 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Display ASP .NET Error in JavaScript Alert Window.

Hello:

I am going through an effort to standardize web applications for my
company. We are trying to figure out the best practices for security,
error handling, etc. I've just done a large amount of work to figure
out the built-in ASP .NET security features (which was fairly easy to
do). So now I am tackling error handling.

What we would like to do is show a JavaScript Alert box with the error
information in it. Here is the code I wished worked (but it doesn't):

protected void DetailsObjectDataSource_Modified(object sender,
ObjectDataSourceStatusEventArgs e)
{
if (e.Exception != null)
{
StringBuilder builder = new StringBuilder();
Exception exception = e.Exception;
while (exception != null)
{
builder.Insert(0, Environment.NewLine);
builder.Insert(0, exception.Message);
exception = exception.InnerException;
}
ScriptManager.RegisterClientScriptBlock(this,
this.GetType(),
"DetailsUpdate",
"Alert('The following error occurred: " +
builder.ToString() + "');",
true);
e.ExceptionHandled = true;
}
OverViewGridView.DataBind();
}

How can I relay a message to the user via an Alert box?

Another question: We have issues with data here such that our
DropDownLists are regularly throwing exceptions. What is the best way
to indicate to a user that a value in a drop down list is invalid and
*must* be changed? How do you prevent the DDL from throwing an
exception? or how do you prevent it from killing the application?
Dec 26 '07 #1
4 2962

<je**********@gmail.comwrote in message
news:e2**********************************@s8g2000p rg.googlegroups.com...
Hello:

I am going through an effort to standardize web applications for my
company. We are trying to figure out the best practices for security,
error handling, etc. I've just done a large amount of work to figure
out the built-in ASP .NET security features (which was fairly easy to
do). So now I am tackling error handling.

What we would like to do is show a JavaScript Alert box with the error
information in it. Here is the code I wished worked (but it doesn't):

protected void DetailsObjectDataSource_Modified(object sender,
ObjectDataSourceStatusEventArgs e)
{
if (e.Exception != null)
{
StringBuilder builder = new StringBuilder();
Exception exception = e.Exception;
while (exception != null)
{
builder.Insert(0, Environment.NewLine);
builder.Insert(0, exception.Message);
exception = exception.InnerException;
}
ScriptManager.RegisterClientScriptBlock(this,
this.GetType(),
"DetailsUpdate",
"Alert('The following error occurred: " +
builder.ToString() + "');",
true);
e.ExceptionHandled = true;
}
OverViewGridView.DataBind();
}

How can I relay a message to the user via an Alert box?
Try RegisterStartupScript instead of RegisterClientScriptBlock.
Another question: We have issues with data here such that our
DropDownLists are regularly throwing exceptions. What is the best way
to indicate to a user that a value in a drop down list is invalid and
*must* be changed? How do you prevent the DDL from throwing an
exception? or how do you prevent it from killing the application?
You have to catch the exception when the SelectedIndex or SelectedValue of
the DDL is set. I'm not sure how to do this if you're using data-bound
controls (we don't use databound controls). What is the default value of the
DDL for new items? I'd set it to that.

Obviously you'll want to figure out where all this bad data is coming from
and correct that as well. :)

Dec 26 '07 #2
Try RegisterStartupScript instead of RegisterClientScriptBlock.

That got it!
>
Another question: We have issues with data here such that our
DropDownLists are regularly throwing exceptions. What is the best way
to indicate to a user that a value in a drop down list is invalid and
*must* be changed? How do you prevent the DDL from throwing an
exception? or how do you prevent it from killing the application?
This is mostly an issue because our DBAs are rather slow when it comes
to correcting data. In most cases, they call the user and have them
specify what the value *should* be. It would be much easier to allow
the user to do this directly, rather than go through the 15-20 minute
rigamarole. I suppose it would be less frustrating if our DBAs learned
to identify these errors instead of needing our participation -
wishful thinking.
>
You have to catch the exception when the SelectedIndex or SelectedValue of
the DDL is set. I'm not sure how to do this if you're using data-bound
controls (we don't use databound controls). What is the default value of the
DDL for new items? I'd set it to that.
We are extremely dependent on data binding. We have had bad
experiences with manual binding and tend to avoid it. The DDL for
ASP .NET lacks many useful features, but that is a limitation from
HTML. For instance, you cannot add a new value to the list directly.
Instead, you have to modify the data source and rebind. The
SelectedIndexChanged event is "too late" since it takes place after
binding is completed (or after the error has already occurred). My
idea is to have a check occur when the business object with the
selected value is first queried. It would do the check first - if it
fails it could redirect the user to a screen where they can chose a
meaningful value. However, the details seem too complicated to do
generically.
>
Obviously you'll want to figure out where all this bad data is coming from
and correct that as well. :)- Hide quoted text -
Oh, we do. We have newer systems that are upgrades from Oracle Forms.
The old systems had poor validation and on occasion these old mistakes
crop up. Additionally, data is often imported in-mass from outside
sources which may or may not adhere to the new system's requirements
(or our DBAs screw up something horrible).

Thank you for your help. It will definitely come in handy.

Dec 26 '07 #3
>You have to catch the exception when the SelectedIndex or SelectedValue
of
the DDL is set. I'm not sure how to do this if you're using data-bound
controls (we don't use databound controls). What is the default value of
the
DDL for new items? I'd set it to that.

We are extremely dependent on data binding. We have had bad
experiences with manual binding and tend to avoid it.
I've had exactly the opposite experience! Regardless......
The DDL for
ASP .NET lacks many useful features, but that is a limitation from
HTML. For instance, you cannot add a new value to the list directly.
Instead, you have to modify the data source and rebind.
I believe that you can add "static" items then set the
"AppendDataBoundItems" property to True. Careful though, the DDL will not
automatically clear items when you rebind if you do this. You'll need to
handle situations where you re-bind DDLs carefully.
The
SelectedIndexChanged event is "too late" since it takes place after
binding is completed (or after the error has already occurred). My
idea is to have a check occur when the business object with the
selected value is first queried. It would do the check first - if it
fails it could redirect the user to a screen where they can chose a
meaningful value. However, the details seem too complicated to do
generically.
Indeed. If you have a business object then all validation should occur there
anyway. Obviously DB constraints would be best, if possible. If anyone says
"But that will break application XYZ!" you can simply respond "Application
XYZ is already broken, it's just not generating exceptions when it breaks!".
:)

Dec 26 '07 #4
On Dec 26, 1:43*pm, "Scott Roberts" <srobe...@no.spam.here-webworks-
software.comwrote:
You have to catch the exception when the SelectedIndex or SelectedValue
of
the DDL is set. I'm not sure how to do this if you're using data-bound
controls (we don't use databound controls). What is the default value of
the
DDL for new items? I'd set it to that.
We are extremely dependent on data binding. We have had bad
experiences with manual binding and tend to avoid it.

I've had exactly the opposite experience! Regardless......
The DDL for
ASP .NET lacks many useful features, but that is a limitation from
HTML. For instance, you cannot add a new value to the list directly.
Instead, you have to modify the data source and rebind.

I believe that you can add "static" items then set the
"AppendDataBoundItems" property to True. Careful though, the DDL will not
automatically clear items when you rebind if you do this. You'll need to
handle situations where you re-bind DDLs carefully.
The only static item we could make would be a blank or "null" field.
We have many instances where "null" is not allowed, so that eliminates
that option. Currently, the application just explodes and we are sent
a message about where to find the bad data. The real problem is that
ASP .NET will attempt to validate each record returned by an
ObjectDataSource. So, if we have a FormView with a validated
DropDownList in it, we may get an error even if the current record is
fine. Somewhere in the results there is a bad record (and there could
be hundreds of records). We can only conclude that the bad record
belongs to a parent record, but not know the pin-point location. We
some times have to search carefully for the record that is in error -
it would be nice to get the bad record directly.

Perhaps I should simply search through my records and remove bad
records prior to releasing them to the interface. Then for each bad
record I can send an email to our DBAs. At least this way our users
don't experience bad data. I could indicate to them that some records
have been excluded and they have to contact the DBA to retrieve them.
>
The
SelectedIndexChanged event is "too late" since it takes place after
binding is completed (or after the error has already occurred). My
idea is to have a check occur when the business object with the
selected value is first queried. It would do the check first - if it
fails it could redirect the user to a screen where they can chose a
meaningful value. However, the details seem too complicated to do
generically.

Indeed. If you have a business object then all validation should occur there
anyway. Obviously DB constraints would be best, if possible. If anyone says
"But that will break application XYZ!" you can simply respond "Application
XYZ is already broken, it's just not generating exceptions when it breaks!".
:)
DB constraints here are rare and far between. Our databases are
ancient and lack some basics like foreign key constraints. We could
add them anytime, but for some reason we don't. I would hate to step
on someone's toes. :-\

Thanks, you got my wheels turning,
Travis
Dec 26 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: DettCom | last post by:
Hello, I would like to be able to display or hide fields based on whether a specific Yes/No radio button is selected. This is in conjunction with a posting a just made here in the same group...
2
by: Chuck Martin | last post by:
I am having a most frustrating problem that references, web searches, and other resources are no help so far in solving. Basically, I'm trying to design a pop-up window to be called with a funciton...
7
by: Stefan Finzel | last post by:
Hi, is there a way to change the display property on Windows Mobile 2003 SE Mobile/Pocket Internet Explorer? See following example. Please note: visibilty property has the same problem. Is...
1
by: C.Joseph Drayton | last post by:
Hi All, I am not sure if this is a JavaScript or PHP error. Basically what is happening, is that I have an HTML page with the following JavaScript. As you can see makeRequest() calls a PHP...
9
by: tshad | last post by:
This was posted before but the message got messed up (all NLs were stripped out for some reason). I have 2 labels that hold the name of different images on my .aspx page. <asp:Label ID="Logo"...
4
by: ArrK | last post by:
I want to use a control frame to create and/or modify the content (images and text) of a display frame - all files and images are client side. . When I try to write to the display frame from the...
3
by: Steve Kershaw | last post by:
Hi, I need to display a seperate error page (off a try...catch block) in ASP.NET. Response.Redirect and Server.Transfer won't work in this case because they replace the old page with a new one...
23
by: Marge | last post by:
This may be in the wrong forum, but here goes. How can I display an alert in the users browser window from an asp page. For example, a welcome message. I know I can do this with response.write,...
9
by: ahilar12 | last post by:
1. <head> 2. <script type="text/javascript"> 3. </script> 4. </head> 5. <body> 6. <form> 7. <select name="team" id="mylist" > 8. <option></option> 9....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.