Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
| Search | Today's Posts | Mark Forums Read
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old July 31st, 2004, 10:19 AM
Authorized User
Join Date: Jun 2004
Location: , , .
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default C# handling SQL Server exceptions

Is there anyway in C# that you can check to see if a table exists in SQL Server? Does anyone know of a good resource for learning how to handle C# exceptions specifically relating to calls to SQL Server?

Also in the code below I get a 'Unreachable code detected' warning if I uncomment the lines
// cn.Close();

I'm guessing the datareader and the sql connection will be closed automatically after the return statement. Is that correct? Any help much appreciated.

public SqlDataReader GetReportInfo ()

String connectionString = GetConnectionString();
//get connection string
SqlConnection cn = new SqlConnection();
cn.ConnectionString = connectionString;
SqlCommand sqlCommand = new SqlCommand("select ReportName from ReportInfoTable",cn);
SqlDataReader sqlDataReader;
sqlDataReader = sqlCommand.ExecuteReader();
return sqlDataReader;
// cn.Close();
  #2 (permalink)  
Old July 31st, 2004, 07:00 PM
Friend of Wrox
Join Date: Oct 2003
Location: Cairo, , Egypt.
Posts: 336
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alyeng2000

General Query Used:
SELECT [name] FROM sysobjects
      WHERE name = 'TableName'
      AND type = 'U'

Ahmed Ali
Software Developer
  #3 (permalink)  
Old September 7th, 2006, 11:24 AM
Registered User
Join Date: Sep 2006
Location: Westerville, OH, USA.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to ratish_aravind


Has anyone found a solution for the "unreachable code detected" compilation error ? Iam facing the same problem with reader.
Any suggestions will be greatly appreciated...


  #4 (permalink)  
Old September 16th, 2006, 09:40 AM
Authorized User
Join Date: Jan 2004
Location: Cluj-Napoca, Cluj, Romania.
Posts: 11
Thanks: 1
Thanked 0 Times in 0 Posts
Send a message via Yahoo to adyrotaru

Hi ratish,
The following piece of code will always yield an "unreachable code detected" warning or even an error compiler message (depend on your compiler's warning level settings).

return sqlDataReader;
// cn.Close();

The reason is because you have an unconditional return statement before the tho so-called "buggy" lines:

// cn.Close();

The code execution will ALWAYS return before reaching those lines (when uncommented).

I recommend that you ALWAYS set your project's settings to "Treat warnings as errors". Alternatively, NEVER let unsolved warnings in your code as more subtile error may be produced later.

Go to: Project/Properties/Configuration Properties/Buil/Treat Warnings as Errors and set this param to "True".
You also may want to change the Warning level, but I also recommend to leave it to level 4 - the most restrictive. I use this level in conjuction with "Treat Warnings as Errors" = True.

Similar Threads
Thread Thread Starter Forum Replies Last Post
Error handling with SQL Server mike_remember SQL Server 2000 2 January 2nd, 2007 07:16 PM
Server acting as browser - handling cookies? Philibuster PHP How-To 1 August 15th, 2006 09:39 PM
handling server side button visibility property shilpa528 General .NET 3 December 13th, 2004 05:32 AM
handling server side buttons visibility property d shilpa528 Java GUI 0 December 13th, 2004 03:02 AM
Server.Execute() Error handling nickelsberry Classic ASP Professional 2 April 14th, 2004 04:45 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.