Solved

VB.net SQL make sure only single value returned

Posted on 2014-03-12
2
223 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
2 Comments
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 total points
Comment Utility
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
Comment Utility
Thanks very much
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now