Solved

VB.net SQL make sure only single value returned

Posted on 2014-03-12
2
232 Views
Last Modified: 2014-03-13
Hi. I am using the following code to pull a single result from a SQL table.
I need a way to check if the result returns more than one value first.
Is there a way to build that into this code? If not what code would I need
to run first to see if there is a single value returned for this SQL statement

                Dim connection As New SqlConnection(oConnectionString)
                Dim cmd As New SqlCommand(sSQL, connection)
                connection.Open()
                oResult = cmd.ExecuteScalar.ToString
                If IsDBNull(oResult) = True Then
                    Get_Value = "Null"
                Else
                    Get_Value = oResult
                End If
                connection.Close()
0
Comment
Question by:murbro
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 total points
ID: 39925689
Assuming your SQL command doesn't have something such TOP 1 (to force maximum one row being returned in the query) you would need to use a reader:
http://msdn.microsoft.com/en-us/library/9kcbe65k%28v=vs.110%29.aspx

Instead of the ExecuteScalar you would use ExecuteReader.  Store the result from the first returned row if any and then check if there was a second row to see if more than one row was returned.
0
 

Author Closing Comment

by:murbro
ID: 39925782
Thanks very much
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Can we attach PDF to table 2 46
store vs query adhoc - no show rows 4 37
how to use ROW_NUMBER() correctly 8 43
Need split for SQL data 4 34
I have a large data set and a SSIS package. How can I load this file in multi threading?
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question