Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > ADO.NET
|
ADO.NET For discussion about ADO.NET.  Topics such as question regarding the System.Data namespace are appropriate.  Questions specific to a particular application should be posted in a forum specific to the application .
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ADO.NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 10th, 2006, 10:02 AM
Registered User
 
Join Date: Apr 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default No Error, Just stops when no records

Hello, I am trying to verify if a record exists before letting my user continue. This code works great if the item they are looking for exists in the database. However, If the record doesn't exist it just stops on the line

drCheckItem = cmdCheckItem.ExecuteReader(CommandBehavior.SingleR esult)

In the immediate window, it displays the error

A first chance exception of type 'System.InvalidOperationException' occurred in System.Data.dll

Any Ideas what I am doing wrong? I am totally new to ADO as I have always programmed in VB6 and Access using DAO. Now I am trying to do this in Visual Studio 2005 and ADO.net.

Thanks,



Imports System.Data
Imports System.Data.SqlClient
Dim conn As New SqlConnection("Data Source=MYIP;" + "Initial Catalog=Tracker;" + "User ID=USEID;")

Function CheckItem(ByVal ItemIn As String) As String
        Dim cmdCheckItem As New SqlCommand()
        Dim drCheckItem As SqlDataReader

        cmdCheckItem.CommandTimeout = 10
        cmdCheckItem.Connection = conn
        cmdCheckItem.CommandType = CommandType.Text
        cmdCheckItem.CommandText = "Select Item_Number from Items where Item_Number = '" & ItemIn & "' or UPC = '" & ItemIn & "'"

        'Make sure that the connection is Open
        If conn.State <> ConnectionState.Open Then conn.Open()

        drCheckItem = cmdCheckItem.ExecuteReader(CommandBehavior.SingleR esult)

        'Read the first line
        drCheckItem.Read()

        'Did we find something?
        If drCheckItem(0).ToString <> "" Then
            CheckItem = ItemIn
        Else
            CheckItem = ""
        End If

    End Function

 
Old May 19th, 2006, 07:30 AM
Friend of Wrox
 
Join Date: May 2006
Posts: 106
Thanks: 0
Thanked 0 Times in 0 Posts
Default

drCheckItem.Read() method will only execute if it has any record, so the error.
use if(drChreckItem.Read .
Read method return 1 when it has any value else 0


Bijgupt





Similar Threads
Thread Thread Starter Forum Replies Last Post
Windows Service Starts and Stops rodmcleay C# 1 May 13th, 2007 05:16 PM
Serial Port Stops Receiving tclancey Pro Visual Basic 2005 0 March 14th, 2007 06:00 AM
schema stops stylesheet from working? stekker XSLT 4 June 15th, 2006 02:02 PM
Service Starts and Stops rodmcleay .NET Web Services 1 January 30th, 2006 04:30 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.