Error messages

T

tshad

If you get an exception or SqlException, how do you get the actual message?

Not the detailed breakdown, but the actual message.

For example, in the following message, I would like to find just "Incorrect
syntax near the keyword 'and'." or "System.Data.SqlClient.SqlException:
Incorrect syntax near the keyword 'and'."

instead of:

errMessage = System.Web.HttpUnhandledException: Exception of type
System.Web.HttpUnhandledException was thrown. --->
System.Data.SqlClient.SqlException: Incorrect syntax near the keyword 'and'.
at ASP.viewPositions_aspx.BindData(String SortFieldName) in
C:\Inetpub\wwwroot\staffingworkshop\applicant\secure\viewPositions.aspx:line
566
at ASP.viewPositions_aspx.Page_Load(Object sender, EventArgs e) in
C:\Inetpub\wwwroot\staffingworkshop\applicant\secure\viewPositions.aspx:line
189
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain()
--- End of inner exception stack trace ---
at System.Web.UI.Page.HandleError(Exception e)
at System.Web.UI.Page.ProcessRequestMain()
at System.Web.UI.Page.ProcessRequest()
at System.Web.UI.Page.ProcessRequest(HttpContext context)
at
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication+IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&
completedSynchronously)errMessage = System.Web.HttpUnhandledException:
Exception of type System.Web.HttpUnhandledException was thrown. --->
System.Data.SqlClient.SqlException: Incorrect syntax near the keyword 'and'.
at ASP.viewPositions_aspx.BindData(String SortFieldName) in
C:\Inetpub\wwwroot\staffingworkshop\applicant\secure\viewPositions.aspx:line
566
at ASP.viewPositions_aspx.Page_Load(Object sender, EventArgs e) in
C:\Inetpub\wwwroot\staffingworkshop\applicant\secure\viewPositions.aspx:line
189
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain()
--- End of inner exception stack trace ---
at System.Web.UI.Page.HandleError(Exception e)
at System.Web.UI.Page.ProcessRequestMain()
at System.Web.UI.Page.ProcessRequest()
at System.Web.UI.Page.ProcessRequest(HttpContext context)
at
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication+IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&
completedSynchronously)

Thanks,

Tom
 
B

Buddy Ackerman

try{
cmd.ExecuteNonQuery();
}
catch (SqlException se){
Response.Write(se.Message);
}
 
T

tshad

Buddy Ackerman said:
try{
cmd.ExecuteNonQuery();
}
catch (SqlException se){
Response.Write(se.Message);
}

Actually, you're right.

But how do you do that from the Application Error section of the Global.asax
file. That was where I got the long message.

Also,

I tried doing:

Catch ex as SQLexception
RcdCount = 0
If ex.number = 7619 then
ErrorMessage.text = "<br>There were only ignored words in your list.
These are words that are too general to search such as: the and of.<br><br>"
end if
ErrorMessage.Text = "<br>Error message 2"
trace.warn("Sql Error from SqlException = " & ex.message)
Catch ex2 as Exception
ErrorMessage.TExt = "<br>Error Message 3"
trace.warn("Regular Error = " & ex2.message)
' throw ex
end try

And it always takes the 1st Catch and never the 2nd.

How do I tell if this is an SqlException or just a normal exception?

I saw an example program where they did this, but what is the point if the
first one is always going to take the exception?

Thanks,

Tom
 
J

Joshua Mitts

In Global.asax, you need to call Server.GetLastError() to get the exception.
Then, you need to test if the Exception returned is a SqlException. In C#,
you use the is or as operator for this purpose, i.e.:

<pseudo-code>
Exception ex = Server.GetLastError();
SqlException sqlex = ex as SqlException;
if (sqlex != null) // ex is SqlException
{
// process it as a sqlexception
}

I don't know the equivilent operator in VB for is or as -- anyone else know?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top