Results 1 to 6 of 6

Thread: how to test for empty result of query

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    145

    how to test for empty result of query

    hi,

    im using datareader for my select statement. How will I test if this select stmt output empty results?

    thanks in advance,
    marivic

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    145
    im using sqlcommand rather not datareader.

  3. #3
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Its not the best but you can use a Try..Catch..Fail and try to read it, if it fails its empty. Most of the time you can use a while and so it doesn't matter really if its empty.
    VB Code:
    1. While myReader.Read()
    2.         str.Add(myReader.Item(0).Value)
    3.     End While
    4.     If Str.Length=0 then Msgbox "Read returned empty"

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    145
    hi edneeis,

    actually im testing if the record exists. so im issuing an sql select via sqlcommand.

    If recordexist() Then
    UpdateRecord()
    Else
    AddRecord()
    MessageBox.Show("record not exist")
    End If

    and my recordexist function:

    Function recordexist()
    Dim cnSQL As SqlConnection
    Dim cmSQL As SqlCommand
    Dim drSQL As SqlDataReader
    Dim strSQL As String
    Dim x As Integer
    Try
    strSQL = "select grpcode, grpdesc, monday, tuesday," & _
    "thursday, friday from calendar2 " & _
    "where grpcode=" & PrepareStr(CType(cbgroup.Items(cbgroup.SelectedIndex), ListItem).ID)
    cnSQL = New SqlConnection(ConnectionString)
    cnSQL.Open()

    cmSQL = New SqlCommand(strSQL, cnSQL)
    drSQL = cmSQL.ExecuteReader()

    ***' Here I want to test if the result set is empty so i could return true if empty******

    If drSQL.Read Then
    Return True
    Else
    Return False
    End If


    ' Close and Clean up objects
    cnSQL.Close()
    cmSQL.Dispose()
    cnSQL.Dispose()
    refreshgrid()

    Catch Exp As SqlException
    MsgBox(Exp.Message, MsgBoxStyle.Critical, "SQL Error")

    Catch Exp As Exception
    MsgBox(Exp.Message, MsgBoxStyle.Critical, "General Error")
    End Try
    End Function

    Any ideas? What alternative approach shld I use?

    Thanks again.

  5. #5
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Why not just get a counr of the query since you don't actually need any of the select info?

    VB Code:
    1. Try
    2. strSQL = "select [b]Count(*)[/b] from calendar2 " & _
    3. "where grpcode=" & PrepareStr(CType(cbgroup.Items(cbgroup.SelectedIndex), ListItem).ID)
    4. cnSQL = New SqlConnection(ConnectionString)
    5. cnSQL.Open()
    6.  
    7. cmSQL = New SqlCommand(strSQL, cnSQL)
    8.  
    9. [b]'you don't even need the reader
    10. dim count as integer=cmSQL.ExecuteScalar()
    11.  
    12. If count>0 then[/b]
    13. Return True
    14. Else
    15. Return False
    16. End If
    17.  
    18.  
    19. ' Close and Clean up objects
    20. cnSQL.Close()
    21. cmSQL.Dispose()
    22. cnSQL.Dispose()
    23. refreshgrid()
    24.  
    25. Catch Exp As SqlException

    Also to make your post look like code use the [ vbcode ] [ /vbcode ] without the spaces in the brackets.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    145
    Thanks!! You're a big help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width