Solved

VB.net SQL make sure only single value returned

Posted on 2014-03-12
2
230 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server Express or Standard? 5 26
Related to SQL Query 5 21
Trouble installing msi file with msiexe.exe 2 17
SQL profiler 3 12
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how the fundamental information of how to create a table.

756 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