C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » ADO.NET »

Sqlexceptionclasses


Posted Date: 27 Oct 2009    Resource Type: Code Snippets    Category: ADO.NET
Author: krishnavenikaladiMember Level: Gold    
Rating: 1 out of 5Points: 10



sqlexception class is creted when .NET framework data provider for sqlserver encounters an error generated from server.it always contains atleat one instance of sqlerror

namespaces to be used
system.data;
system.data.sqlclient;


public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{


//creating connection object for sql connection.
try
{
SqlConnection cn = new SqlConnection("user id=sa;password= ");
cn.Open();
}
//catching the exception
catch (SqlException ex)
{


string msg = "";
//iterating through types of errors

for (int i = 0; i < ex.Errors.Count; i++)
{
msg += "index#" + i + "\n" + "message" + ex.Errors[i].Message + "\n" + "linenumber" + ex.Errors[i].LineNumber + "\n" + "source" + ex.Errors[i].Source + "\n" + "procedure" + ex.Errors[i].Procedure;



}
//errors r logged into eventlog .
System.Diagnostics.EventLog log = new System.Diagnostics.EventLog();
log.Source = "myapplication";
log.WriteEntry(msg);
Response.Write("an exception occured.Please contact u'r admin...");

}
}
}


explanation:

-->the event of connection to sqlserver takes place by event of button click event.
-->use try & catch block to catch exceptions
-->if connection is developed without any probs try block is execute
-->if connection is not developed catch block is executed
--->using sqlexception class an d instance of sqlerror erroe messages r entered into log file (only maningful info is given to user)which is used by admin



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Try  .  Sqlexception  .  Erros  .  Catch  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Data Relation in Sql using C#
Previous Resource: BindingManagerBase
Return to Discussion Resource Index
Post New Resource
Category: ADO.NET


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use