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 7767229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:37:28+00:00 2026-06-01T15:37:28+00:00

I have ADO.Net code as below, that calls a stored procedure. Within the stored

  • 0

I have ADO.Net code as below, that calls a stored procedure. Within the stored procedure, I first get the result set through a SELECT query, and then after the SELECT statement call RAISERROR if the passed @companyId parameter value does not exist. I have run unit test on this code many times with a value of @companyId so RAISEERROR gets called, but I never see the call to ExecuteReader throw an error. Any reason why this strange counter-intuitive thing is happening?

sqlCmd = new SqlCommand("dbo.xyz_sp_Attributes_GetValues", new  SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["VHA_EDM_ConnectionString"].ConnectionString));
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.Parameters.AddWithValue("@attributeId", attributeId);
sqlCmd.Parameters.AddWithValue("@companyId", companyId);
sqlCmd.Parameters.AddWithValue("@attributeScope", "Company");
sqlCmd.Connection.Open();
SqlDataReader dr = sqlCmd.ExecuteReader(CommandBehavior.CloseConnection);
while (dr.Read())
{
    attributeValue = dr["AttributeValue"].ToString();
}

The stored procedure code is something like below

SELECT col1, col2, ... where CompanyId = @companyId and AttributeId = @attributeId
if @companyId not exists (select companyId from Company where CompanyId = @companyId)
begin
 set @errMessage = N'Invalid Company Error'
 RAISERROR (@errMessage, 16, 1)
end
  • 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-01T15:37:29+00:00Added an answer on June 1, 2026 at 3:37 pm

    In this case there are more than one recordsets are returned. The first one is empty and that is why you don’t get any error. You have to call the NextRecordset to get the error.

    dr.NextResult(); // This will throw the error caused by RAISEERROR
    

    It would be much easier to do the error checking in the client code instead of calling RAISEERROR which in any case will have to be handled as an Exception.

    Catch a Sql Server RAISERROR at client side using SqlDataReader

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

Sidebar

Related Questions

I have a project that isn't using USING anywhere with their ADO.NET code. I
I am debugging some C# code that uses Ado.net to call a stored proc
I have an ADO.NET Managed Data Provider that is registered in machine.config in DbProviderFactory
Suppose I have an ADO.NET DataTable that I was to 'persist' by saving it
I have a bit of .NET code that retrieves the results from an Oracle
I'm using ASP.NET MVC4 (EF Code-first) with WCF ADO.NET Data Service October 2011 CTP.
I have the following ado.net code, if I already use using to wrap my
Have an existing C# .NET code base that is currently coded to directly access
have a really annoying time developing some code for a set of UserControls that
How do I call a stored procedure without error in ADO.NET Entity Framework? If

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.