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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:56:16+00:00 2026-05-13T08:56:16+00:00

I want to print a value that is returned by SQL Server. If NOT

  • 0

I want to print a value that is returned by SQL Server.

If NOT Exists(SELECT * FROM ItemList WHERE ItemName='txtItemNama') 
   BEGIN   
    INSERT INTO ItemList (ItemName) VALUES('txtItemNamea')  
   END 
ELSE  
   BEGIN 
    Print 'Duplicate' 
   END

This query will either return me either number of rows affected or Duplicate

I want to use this Duplicate in C# in MessageBox.Show()

string query1 = "If NOT Exists(SELECT * FROM ItemList WHERE ItemName='txtItemName') BEGIN  INSERT INTO ItemList (ItemName) VALUES('txtItemName')  END ELSE  BEGIN Print 'Duplicate' END";
            SqlCommand cmd = new SqlCommand(query1, conn);
            SqlDataReader dr;
            conn.Open();
            dr=cmd.ExecuteReader();
            conn.Close();
MessageBox.Show(dr);

I don’t know how to use dr to do this. Please help me out to print Duplicate here

MessageBox.Show(dr);

What do I need to do here?

  • 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-05-13T08:56:17+00:00Added an answer on May 13, 2026 at 8:56 am

    Using ADO.NET there are four options for returning information from an SQL query:

    • Use a DataSet object to gather the returned rows and to work with these rows in addition to the return values and the return parameters.
    • Use a DataReader object to gather the returned rows, to move through these rows, and to gather return values and return parameters.
    • Use the ExecuteScalar method to return the value from the first column of the results’ first row with the return values and the return parameters. This is most useful with aggregate functions.
    • Use the ExecuteNonQuery method to return only the return parameters and the return values. Any returned rows are discarded. This is most useful for executing action queries.

    These are all methods called from your command object.

    There are lots of different ways to skin a cat, you can use output parameters, you can use ExecuteScalar, you can use return values or you can use dummy recordsets.

    You should be able to something like the following in your C# to get the return value from a query

    // add a new parameter, with any name we want - its for our own use only 
    SqlParameter sqlParam = com.Parameters.Add("@ReturnValue", SqlDbType.Int); 
    // set the direction flag so that it will be filled with the return value 
    myParm.Direction = ParameterDirection.ReturnValue;
    

    The code above the captures the return value that you can set as you need, perhaps with a 0 for exists and a 1 for not exists.

    If NOT Exists(SELECT * FROM ItemList WHERE ItemName='txtItemNama')    
       BEGIN      
        INSERT INTO ItemList (ItemName) VALUES('txtItemNamea') 
        Return 0;    
       END    
    ELSE     
       BEGIN    
        Return 1;    
       END 
    

    There is a bit caveat with the above advice however – I usually either use an ORM like Linq-to-SQL or NHibernate, or I use Stored Procedures. I find inline SQL quite cumbersome. So, while my answer should be sound in general, you will probably need to work through some details to get it working exactly as you need.

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

Sidebar

Related Questions

I want to print styled html pages with their images from a script. Can
I want to write a program that would print every combination of a set
I want to write a small program that should print something like testing CPU...
I want to print the first 10000 prime numbers. Can anyone give me the
I want to print the full length of a C-string in GDB. By default
I am iterating over a list and I want to print out the index
I have an array which is a list of domains, I want to print
Say I have the classic 4-byte signed integer, and I want something like print
I want to do this (no particular language): print(foo.objects.bookdb.books[12].title); or this: book = foo.objects.bookdb.book.new();
Consider: print $foo, AAAAAAAA, $foo, BBBBBBBB; Let's say I want to use this code

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.