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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to catch a InvalidCastException before a Gridview is generated?

Status
Not open for further replies.

oldmanbernie

Programmer
Jan 18, 2007
9
0
0
US
I have a gridview that is bound with a SQLDataSource by stored procedures on SQL Server. The stored procedure is a left outer join between two tables that may yield null values for 3 bit-typed variables. This causes a problem because the Gridview would not accept null values. Is there a way to use a try-catch to catch the exception and do an insertion to the database as the gridview object is being displayed?
 
What data are you trying trying to cast and to what type (i.e. the error means your are trying to set a value as a particular data type but that conversion isn't possible)? Some of your options will be to:

1) Change the value in the Stored Procedure (i.e. if it is a null value that is causing the problem, use the IsNull method to return something else).

2) Use the RowDataBound event, get a reference to the value and change it to somethign else.


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244.
 
I created a method with a try-catch to catch the InvalidCastException to handle the problem. It seems to be working now. Thanks for the help!
 
That's really not a good method. Try/Catch blocks should be used to handle exceptions which the above clearly isn't (as you are expecting it to happen) as they will affect performance.


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top