PC Review


Reply
Thread Tools Rate Thread

Can I get the SQL command from a SqlException object?

 
 
Big Daddy
Guest
Posts: n/a
 
      21st Mar 2009
Let's say I have some code like this:

try
{
using (SqlConnection conn = new SqlConnection(strConnect))
using (SqlCommand cmd = new SqlCommand("", conn))
{
conn.Open();
cmd.CommandText = "select * from SomeTable";
cmd.ExecuteScalar();
}
}
catch (SqlException ex)
{
// Do logging
}

In my exception handler, when I'm trying to log the error, I'd like to
be able to get the command text that caused the exception to be thrown
(in the example above, it would be "select * from SomeTable"). The
SqlException class has a collection of SqlErrors, but they don't
appear to have the text I want. Is there a way for me to get this?

thanks in advance,
John
 
Reply With Quote
 
 
 
 
Cor Ligthert[MVP]
Guest
Posts: n/a
 
      22nd Mar 2009
Not likely because now you do it dynamicaly.

As you use stored procedures the text is not even in your code.

Cor

"Big Daddy" <(E-Mail Removed)> wrote in message
news:207e2198-0438-4f1e-98ec-(E-Mail Removed)...
> Let's say I have some code like this:
>
> try
> {
> using (SqlConnection conn = new SqlConnection(strConnect))
> using (SqlCommand cmd = new SqlCommand("", conn))
> {
> conn.Open();
> cmd.CommandText = "select * from SomeTable";
> cmd.ExecuteScalar();
> }
> }
> catch (SqlException ex)
> {
> // Do logging
> }
>
> In my exception handler, when I'm trying to log the error, I'd like to
> be able to get the command text that caused the exception to be thrown
> (in the example above, it would be "select * from SomeTable"). The
> SqlException class has a collection of SqlErrors, but they don't
> appear to have the text I want. Is there a way for me to get this?
>
> thanks in advance,
> John


 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SqlException: Invalid object name 'sys.table_types' in SqlConnection.GetSchema() after installing VS 2008 Samuel R. Neff Microsoft C# .NET 2 22nd Jan 2008 06:12 PM
SqlException: Invalid object name 'UserData' Mr.KisS Microsoft ASP .NET 3 7th Jul 2004 03:23 PM
SqlException: EXECUTE permission denied on object Ivan Simurina Microsoft C# .NET 5 2nd May 2004 11:23 AM
Command text was not set for the command object error And DoCmd.OpenQuery tina Microsoft Access Getting Started 1 23rd Jan 2004 08:29 PM
SQLException: retrieve the SQL command string associated with a syntax error? K. Shier Microsoft ADO .NET 1 3rd Oct 2003 05:31 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:10 PM.