Results 1 to 6 of 6

Thread: Testing for data in SQL Server

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2004
    Location
    Utah, USA
    Posts
    353

    Testing for data in SQL Server

    I need to test to see if case numbers exist in the database or not. This is what I have so far:

    VB Code:
    1. If IsNumeric(txtSearchCase.Text.Substring(0)) Then
    2.             cmdSqlCommand1.CommandText = "SELECT * FROM tblCapRec WHERE CaseNumber LIKE '" & txtSearchCase.Text & "%' ORDER BY CaseNumber, PartNumber, LastName, FirstName"

    It works fine if the case is found in the database, but if it's not it gives me an error. How do I test to see if it exists or not?

    Thanks!

    Brenda

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2004
    Location
    Utah, USA
    Posts
    353
    Here is more of my coding if that helps:

    VB Code:
    1. If IsNumeric(txtSearchCase.Text.Substring(0)) Then
    2.             cmdSqlCommand1.CommandText = "SELECT * FROM tblCapRec WHERE CaseNumber LIKE '" & txtSearchCase.Text & "%' ORDER BY CaseNumber, PartNumber, LastName, FirstName"
    3.         ElseIf txtSearchCase.Text.Length = 0 Then
    4.             MessageBox.Show("Please Enter a Case Number or Last Name!", "Message Box", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    5.             txtSearchCase.Focus()
    6.             Exit Sub
    7.         ElseIf Not IsNumeric(txtSearchCase.Text.Substring(0)) Then
    8.             cmdSqlCommand1.CommandText = "SELECT * FROM tblCapRec WHERE LastName LIKE '" & txtSearchCase.Text & "%' ORDER BY LastName, FirstName, CaseNumber, PartNumber"
    9.         Else
    10.         End If
    11.         cmdSqlCommand1.Connection = SqlConn
    12.         SqlConn.Open()
    13.         Dim drSqlDataReader1 As SqlDataReader = cmdSqlCommand1.ExecuteReader()
    14.         drSqlDataReader1.Read()
    15.  
    16.         'Read in values from CapRec Table
    17.         CaseNumber = drSqlDataReader1.Item(0)
    18.         g_CaseNum = drSqlDataReader1.Item(0)
    19.                                ....
    20. End Sub

    Thanks!

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2004
    Location
    Utah, USA
    Posts
    353
    Maybe it has something to do with "HasRows." Have any of you ever used that before. I try to put it in my coding, but it doesn't like it.

    VB Code:
    1. If drSqlDataReader1.HasRows Then
    2.            drSqlDataReader1.Read()
    3.            
    4.         Else
    5.             Console.WriteLine("No rows returned.")
    6.         End If

    Maybe something like that? HELP!!

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    VB Code:
    1. If drSqlDataReader1.Read() Then
    2. ...
    3. 'Read in values from CapRec Table
    4. CaseNumber = drSqlDataReader1.Item(0)
    5. g_CaseNum = drSqlDataReader1.Item(0)
    6. ...
    7. Else
    8. Msgbox "No Case Numbers Matched."
    9. End if

    Or you can use the Try...Catch...Finally syntax.

    TG
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2004
    Location
    Utah, USA
    Posts
    353
    Thanks!

  6. #6
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    I use the HasRows method all the time and it has never failed.

    HasRows is only available in .NET 1.1, is that what you mean by "it doesn't like it"? Are you getting a compile error or runtime error.

    Be more specific when explaining your problems.

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