Asp.net Try Catch Sql Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and sql error handling in function policies of this site About Us Learn more about Stack Overflow the company sql error handling in stored procedure Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users
Sql Error Handling Best Practices
Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a http://www.asp.net/web-forms/overview/getting-started/getting-started-with-aspnet-45-web-forms/aspnet-error-handling minute: Sign up Catch System.Data.SqlClient.SqlException error up vote 3 down vote favorite I have met the exception System.Data.SqlClient.SqlException when I tried to access a webpage on my ASP.NET web application without any network connection. I have tried to catch the exception by using a try catch, however it does not work. Below is my code snippet: protected void Page_Load(object sender, EventArgs http://stackoverflow.com/questions/8239239/catch-system-data-sqlclient-sqlexception-error e) { try { SqlDataSource1.SelectCommand = "SELECT * FROM [UserDB]"; SqlDataSource1.DataBind(); } catch (SqlException ex) { Response.Redirect("/App/ErrorPage.aspx"); } } My try catch worked on other functions within my webpage itself. E.g. ErrorPage will be shown if user try to delete record when not connected to network. But as for the pageload method, the try catch did not work as expected, and the error showed as the webpage below: Anyone can tell me where I have gone wrong? Thank you asp.net exception exception-handling share|improve this question asked Nov 23 '11 at 8:41 gymcode 1,26293670 add a comment| 1 Answer 1 active oldest votes up vote 4 down vote accepted I'm not sure about the reason but I believe this code will work for you: try { SqlDataSource1.SelectCommand = "SELECT * FROM [UserDB]"; SqlDataSource1.DataBind(); } catch (System.Data.SqlClient.SqlException ex) //Catch SqlException { Response.Redirect("/App/ErrorPage.aspx"); } catch(Exception ex) //Catch Other Exception { Response.Write(ex.Message); } share|improve this answer answered Nov 23 '11 at 10:11 NaveenBhat 2,44322341 add a comment| Your Answer draft saved draft discarded Sign up or log in Sign up using Google Sign up using
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About http://stackoverflow.com/questions/1892609/outputting-the-exception-from-a-sqlexception-error Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the http://www.c-sharpcorner.com/uploadfile/de41d6/exceptionerror-handling-in-asp-net-simplified/ Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Outputting the exception from a SQLException sql error error up vote 2 down vote favorite I have a .aspx page calling a .asmx page's web service. In that .net web service I attempt to open a database connection to SQLServer 2008. The connection is failing. I am not sure why. I am doing this in a try / catch and the catch does get hit when I debug. I'm not sure what I can sql error handling output there though as I don't have access to the server's filesystem to write a log file. I found this posting: try { SqlCommand cmd = new SqlCommand("raiserror('Manual SQL exception', 16, 1)",DBConn); cmd.ExecuteNonQuery(); } catch (SqlException ex) { string msg = ex.Message; // msg = "Manual SQL exception" } here and it might do the trick for me, but I don't know how to make the msg string output to the page which called this web service? Is there a way to propagate it up the exception chain by having the calling page also implement that same exception handler? Thanks // :) asp.net sql-server error-handling asmx sqlexception share|improve this question edited Dec 12 '09 at 8:20 marc_s 450k918621027 asked Dec 12 '09 at 7:01 Spanky 2,24652634 Thanks for the edit Mark, it helped :) –Spanky Dec 12 '09 at 8:32 add a comment| 5 Answers 5 active oldest votes up vote 3 down vote accepted Your should be able to trap the SQL Exception as a specifc type and read the particular error message: try{ ... } catch(SQLException sqlex) { /// do some work here with sqlex.Message } catch(Exception ex) { /// this will trap
Version 0.5.3 Released We want your feedback: Comment Emotions C# Corner Annual Conference 2017 Announced Future of .NET: Why Is .NET Core Important? Must Read: 50 Important Queries In SQL Server C# Corner Contribute An Article A Blog A News A Video A Link An Interview Question Ask a Question TECHNOLOGIES .NET Chapters Internet & Web Oracle String in C# .NET Core Cloud Internet of Things Outsourcing Swift ADO.NET Coding Best Practices iOS PHP TypeScript AJAX Cognitive Services Java Power BI Universal Windows Platform Android Cryptography JavaScript Products Visual Studio Angular 2 Current Affairs JQuery Project Management WCF AngularJS Databases & DBA JSP Python Web Development Architecture Design Patterns & Practices Knockout R Web Services ASP.NET Dynamics CRM Learn C# Corner Robotics & Hardware Windows 10 ASP.NET Core Entity Framework LINQ Security Windows Controls Azure Error Zone Machine Learning Servers Windows Forms Big Data Games Programming Microsoft Office SharePoint Windows PowerShell BizTalk Server GDI+ & Graphics Mobile Development SignalR WPF Bot Framework Google Development Node.js Smart Devices Xamarin C# HoloLens Office Development Software Testing XAML C, C++, MFC How do I OOP/OOD SQL Language XML Career Advice HTML 5 Open Source SQL Server Request a new Category| View All ANSWERS BLOGS VIDEOS INTERVIEWS BOOKS NEWS CHAPTERS CAREER Jobs CODE IDEAS Scroll To Top Reader Level: Article Exception/Error Handling in ASP.Net Simplified By Abhishek Singh on Sep 26, 2014 This article explains the exception and error handling in ASP.Net. 67.4k 0 4 facebook twitter linkedIn google Plus Reddit WhatsApp expand IntroductionGuys, this is not first time when something is written for exceptions and error handling in the web. There are enormous articles written earlier for this topic. I have tried writing this article for beginners explaining these concepts in simple language and step-by-step.This is a very basic article for beginners that show exception handling and error handling techniques in ASP.NET.Where there are codes, the chances for exceptions / error always exist so it is very important for developers when developing web applications to understand the errors and implement the error handling techniques to avoid breaking pages or providinig unhandled errors to end uses.What Exceptions areGuys, the MSDN says that an exception is an error condition or unexpected behavior encountered by an executing program. Exceptions can be raised because of a fault in your code or in code that you call (such as a shared library), unavailable operating system resources or unexpected conditions the C