473,383 Members | 1,862 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

handling external exceptions in ADO.NET

Hello,

my question is rather connected with some aspects of ADO.NET but I've
been trying to ask on .dotnet.framework.adonet with no result. I hope
someone here can help me.

The problem is here: some stored procedures raise errors and return results
in the same time. One of such sps is sp_droplogin [but it is fairlty easy to
even write such procedure, so the problem is more general].

I wish to intercept the exception and show the conflicted databases
[look at the snippet below, the sp_droplogin returns the recordset of
conflicting databases, you can try it in the Query Analyzer]. This does not
work in my approach: the thrown exception clears any results returned from
the database. I have no idea if this is the fault of ADO.NET or just my
ignorance.

The question is: how this can be achevied [EnterpriseManager does that
for example] in C#+ADO.NET? as far as I can remember this worked in old
vb6+ado because the errors raised from the stored procedures were not
reported as exceptions. I've even found a kb article on that (Q245179) but
still have no idea how to work this out in ado.net.

could anyone share his/her experience with me and englighten me a little
bit?

thanks in advance,
Wiktor Zychla
// code snippet
private void someMethod()
{
SqlConnection c = null;
SqlDataReader r = null;
try
{
c = new SqlConnection( ... );
c.Open();

SqlCommand cmd = new SqlCommand( "sp_droplogin", c );
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add( "@loginame", ... );
r = cmd.ExecuteReader(); // should return the conflicted databases
}

catch ( SqlException ex )
{
// cannot look at the result set because it has been cleared out!!
while ( r.Read() ) { ... // wrong! but how to read the result? }
MessageBox.Show( ex.Message );
}
finally
{
if ( r != null ) r.Close();
if ( c != null && c.State == ConnectionState.Open ) c.Close();
}
}

Nov 15 '05 #1
3 1875
Wiktor,

My colleague has just suggested there is a dedicated SP that just returns
the list of databases used by a login. You can employ this SP first to check
for conflicts and then safely drop the login.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://www.x-unity.net/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Wiktor Zychla" <us**@nospam.com.eu> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hello,

my question is rather connected with some aspects of ADO.NET but I've
been trying to ask on .dotnet.framework.adonet with no result. I hope
someone here can help me.

The problem is here: some stored procedures raise errors and return results in the same time. One of such sps is sp_droplogin [but it is fairlty easy to even write such procedure, so the problem is more general].

I wish to intercept the exception and show the conflicted databases
[look at the snippet below, the sp_droplogin returns the recordset of
conflicting databases, you can try it in the Query Analyzer]. This does not work in my approach: the thrown exception clears any results returned from
the database. I have no idea if this is the fault of ADO.NET or just my
ignorance.

The question is: how this can be achevied [EnterpriseManager does that
for example] in C#+ADO.NET? as far as I can remember this worked in old
vb6+ado because the errors raised from the stored procedures were not
reported as exceptions. I've even found a kb article on that (Q245179) but
still have no idea how to work this out in ado.net.

could anyone share his/her experience with me and englighten me a little
bit?

thanks in advance,
Wiktor Zychla
// code snippet
private void someMethod()
{
SqlConnection c = null;
SqlDataReader r = null;
try
{
c = new SqlConnection( ... );
c.Open();

SqlCommand cmd = new SqlCommand( "sp_droplogin", c );
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add( "@loginame", ... );
r = cmd.ExecuteReader(); // should return the conflicted databases
}

catch ( SqlException ex )
{
// cannot look at the result set because it has been cleared out!! while ( r.Read() ) { ... // wrong! but how to read the result? }
MessageBox.Show( ex.Message );
}
finally
{
if ( r != null ) r.Close();
if ( c != null && c.State == ConnectionState.Open ) c.Close();
}
}


Nov 15 '05 #2
> My colleague has just suggested there is a dedicated SP that just returns
the list of databases used by a login. You can employ this SP first to check for conflicts and then safely drop the login.


Thanks Dmitriy. I know that.

For me its more general problem - how to retrieve the results from the
command that raises exception and returns the result.

If you run the sp_droplogin in QueryAnalyzer, you can see the exception
message and the resultset (on the separate grid). I do not think the QA is
smart enough to know that there is another stored procedure to be run before
sp_droplogin.

We were trying to handle this case properly using 3 different development
environments: C#+ado.net, delphi 6+ado and vb6+ado. none of them succeded
(the exception is always thrown) so I really wonder how QA handles this one.

Wiktor
Nov 15 '05 #3
QA probably uses a low-level API which deals with error codes but not
exceptions. I guess not every error assumes that the returned results should
be discarded, but ADO / ADO .NET seem not to distinguish between various
errors and discard the results in case of any error.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://www.x-unity.net/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Wiktor Zychla" <us**@nospam.com.eu> wrote in message
news:e7****************@TK2MSFTNGP09.phx.gbl...
My colleague has just suggested there is a dedicated SP that just returns the list of databases used by a login. You can employ this SP first to check
for conflicts and then safely drop the login.


Thanks Dmitriy. I know that.

For me its more general problem - how to retrieve the results from the
command that raises exception and returns the result.

If you run the sp_droplogin in QueryAnalyzer, you can see the exception
message and the resultset (on the separate grid). I do not think the QA is
smart enough to know that there is another stored procedure to be run

before sp_droplogin.

We were trying to handle this case properly using 3 different development
environments: C#+ado.net, delphi 6+ado and vb6+ado. none of them succeded
(the exception is always thrown) so I really wonder how QA handles this one.
Wiktor


Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Hans-Joachim Widmaier | last post by:
Hi all. Handling files is an extremely frequent task in programming, so most programming languages have an abstraction of the basic files offered by the underlying operating system. This is...
12
by: Ritz, Bruno | last post by:
hi in java i found that when a method has a throws clause in the definition, callers must either handle the exceptions thrown by the method they are calling or "forward" the exception to the...
28
by: Frank Puck | last post by:
Meanwhile there are at least 8 years that compilers exist, which provide a working implementation of C++ Exception Handling. Has anything changed meanwhile? From my point of view nothing has...
5
by: juergen perlinger | last post by:
Hello out there. sometimes I need to have proper control of the floating point arithmetic of the C(and C++) runtime system, and using the f.p. exception handling of the C99 standard is quite...
10
by: Justin Dutoit | last post by:
Hey. I'm still not experienced at error handling, and I need to know if Try.. Catch blocks are meant to be used to handle errors in your own app, ie bugs. Or, are they only for external things like...
41
by: Jordan | last post by:
While writing some code, I realized I had never developed a consistent pattern for checking errors from a method. I have two styles I jump back and forth between, but I'm wondering which is...
132
by: Zorro | last post by:
The simplicity of stack unraveling of C++ is not without defective consequences. The following article points to C++ examples showing the defects. An engineer aware of defects can avoid...
35
by: jeffc226 | last post by:
I'm interested in an idiom for handling errors in functions without using traditional nested ifs, because I think that can be very awkward and difficult to maintain, when the number of error checks...
41
by: Zytan | last post by:
Ok something simple like int.Parse(string) can throw these exceptions: ArgumentNullException, FormatException, OverflowException I don't want my program to just crash on an exception, so I must...
35
by: eliben | last post by:
Python provides a quite good and feature-complete exception handling mechanism for its programmers. This is good. But exceptions, like any complex construct, are difficult to use correctly,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.