Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8123797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:17:01+00:00 2026-06-06T06:17:01+00:00

Intro I have an issue with the SqlClient in .Net 4.0.30319 SP1Rel, in which

  • 0

Intro

I have an issue with the SqlClient in .Net 4.0.30319 SP1Rel, in which no Exception is thrown when my stored procedure proceduces the following error:

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

I’ve created an tiny program and stored proc that will demonstrate this. Sql Server version is 9.0.4053.

Code sample

Stored procedure

create proc test
as
select case (select 1 union select 2) when 1 then 1 else 2 end

.Net Console app

using System;
using System.Data;
using System.Data.SqlClient;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            SqlConnection conn = new SqlConnection("<your connection string>");
            conn.Open();

            SqlCommand cmd = new SqlCommand("test", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            try
            {
                SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                Console.WriteLine("Finished");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            Console.ReadKey();
        }
    }
}

Running this application just writes ‘Finished’ to the console.

Running the stored procedure ‘test’ in de SQL Server Management Studio will report the error in the result pane like:

Msg 512, Level 16, State 1, Procedure test, Line 3
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

Expected behaviour

Replacing the stored proc by the following example, will give an expected System.Data.SqlClient.SqlException.

Raiserror

alter proc test
as
raiserror('Not good at all!', 16, 1)

Alternative issues

Some other definitions I have found don’t throw an exception as well.

Raiserror after

alter proc test
as
select case (select 1 union select 2) when 1 then 1 else 2 end
raiserror('Not good at all!', 16, 1)

This suggests that the select-statement stops execution of the stored procedure.

Divide by zero

alter proc test
as
select 1/0

My conclusion is that the SqlClient does not propertly recognize the error state of the tSql batch. Has anybody encountered this and found a solution? I always trusted on the .Net framework to throw an exception on any tSql error.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T06:17:02+00:00Added an answer on June 6, 2026 at 6:17 am

    Thanks to the helpful and swift comment of Martin, I found the cause of this strange behaviour.

    In the past, one of the programmers wrapped the reads on the reader in a test to see if there were any rows at all, like this:

    SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
    
    if (reader.HasRows)
    {
        while (reader.Read()) { /* Do Something */ }
    }
    

    I don’t know why, as the read() method will return false on the first call, when there are no rows.

    Anyway, when an error occurred in the tSQL, the HasRows property returned false and the Read() method was never called. Due to that, SQL errors where not revealed to the SqlClient and no exception was thrown.

    You can add this code-snippet to the sample project, just after ExecuteReader, with or without test for HasRows, and see the effect.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an issue with the below stored procedure. It runs fine as long
I have a weird issue where I have a stored procedure that is inserting
I have a little issue concerning an animation-effect which loads a certain div into
Short Intro Currently I have a UITableView which is filled with custom cells that
I have the following issue whith this very simple task. I want to create
I have following issue: I'm introducing new feature into application (run as Windows Service)
I have an issue in a PHP script which checks if the tax number
I have an issue while storing some special character values into db. For e.g.
I have ran into an interesting issue while trying to create a more usable
I have run into a small issue in my program. I have a class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.