Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a problem running a query which is "Select data from DB where data = textbox AND dbdata = True" The query is needed to check to see if the data from the textbox is equal to what the result of the query is. If it is true or equal to then run an if statement to show an error message and stop the rest of the script from running or simply exit or close.

If that makes sense....

I have tried using the USING statement with OleDBDataReader functions but no luck...

The if statement would look something like:

If assestbarcode.text = query then me.close messagebox.show("This item is already checked out")

I have deleted all of what I have tried and only left the code that writes data to a DB which should only run if the if statement is false.

What I have tried:

Private Sub CheckOut_Click_1(sender As Object, e As EventArgs) Handles CheckOut.Click

    Dim str As String
    str = "INSERT INTO TrackingInfo (EmployeeName, AssetName, BadgeNumber, AssetBarcode, CheckedOut, DateNTime) values (DisplayName, AssetName, ChkOutBadge, ChkOutBarcode, True, TimeStamp)"

    Dim cmd As OleDbCommand = New OleDbCommand(str)

    cmd.Connection = objConn

    Try

        objConn.Open()

        cmd.Parameters.Add(New OleDbParameter("EmployeeName", CType(Me.Controls("Displayname").Text, String)))
        cmd.Parameters.Add(New OleDbParameter("AssetName", CType(Me.Controls("DisplayAsset").Text, String)))
        cmd.Parameters.Add(New OleDbParameter("BadgeNumber", CType(Me.Controls("ChkOutBadge").Text, String)))
        cmd.Parameters.Add(New OleDbParameter("AssetBarcode", CType(Me.Controls("ChkOutBarcode").Text, String)))
        cmd.Parameters.Add(New OleDbParameter("DateNTime", CType(Me.Controls("TimeStamp").Text, String)))

        cmd.ExecuteNonQuery()
        cmd.Dispose()
        objConn.Close()

        MsgBox("Loaner has been checked out successfully")

    Catch ex As Exception

        MsgBox(ex.Message)

    End Try
Posted
Updated 15-Jun-17 8:52am
Comments
KD209 14-Jun-17 14:33pm    
Here is the code that I have been trying to use to get the desired result:

Dim fetchstr As String = connStr
Dim fetchconn As OleDbConnection = New OleDbConnection(fetchstr)
Dim cmdfetch As OleDbCommand = New OleDbCommand()
Dim fetch As String = "Select AssetBarcode from Assets Where AssetBarcode = ChkOutBarcode AND CheckedOut = True"
'cmdfetch.CommandText = "Select AssetBarcode from Assets Where AssetBarcode = ChkOutBarcode AND CheckedOut = True"

fetchconn.Open()

cmdfetch = New OleDbCommand(fetch, fetchconn)
cmdfetch.Parameters.AddWithValue("AssetBarcode", ChkOutBarcode.Text)

cmdfetch.ExecuteNonQuery()
fetchconn.Close()

If cmdfetch.ExecuteNonQuery > 0 Then
MessageBox.Show("This item has already been checked out. Please alert a Helpdesk team member.")
Me.Close()
End If
Richard Deeming 14-Jun-17 17:51pm    
Try using a question-mark as the parameter placeholder:
Select AssetBarcode from Assets Where AssetBarcode = ? AND CheckedOut = True
Richard Deeming 14-Jun-17 17:53pm    
You're also trying to call ExecuteNonQuery twice, and the second time is after you've closed the connection. That's not going to work. You need to store the result of the first call in a variable, and then test that variable in your If block.
KD209 15-Jun-17 13:28pm    
I have rewritten the code, but now I get an error stating that the "ExecuteReader has not been initialized"

Code:

Dim fetchstr As String = connStr
Dim fetchconn As OleDbConnection = New OleDbConnection(fetchstr)
Dim cmdfetch As OleDbCommand = New OleDbCommand()
Dim fetch As String = "Select AssetBarcode from Assets Where AssetBarcode = ? AND CheckedOut = True"

fetchconn.Open()

cmdfetch.Parameters.AddWithValue("AssetBarcode", OleDbType.VarChar).Value = ChkOutBarcode

Dim fetchreader As OleDbDataReader = cmdfetch.ExecuteReader()

While fetchreader.Read()

If ChkOutBarcode.Text = (fetchreader("AssetBarcode")) Then

MessageBox.Show("Item has already been checked out. Please inform the Helpdesk")

Me.Close()

End If

End While

fetchreader.Close()
fetchconn.Close()
Richard Deeming 15-Jun-17 13:34pm    
You've not set the command's Connection property.

You should also wrap the connection, command and reader objects in Using blocks, and use a case-insensitive string comparison to test whether the strings are equal.
Using fetchconn As New OleDbConnection(connStr)
    Using cmdfetch As New OleDbCommand()
        cmdfetch.Connection = fetchconn
        cmdfetch.CommandText = "Select AssetBarcode from Assets Where AssetBarcode = ? AND CheckedOut = True"
        cmdfetch.Parameters.AddWithValue("AssetBarcode", OleDbType.VarChar).Value = ChkOutBarcode
        
        fetchconn.Open()
        Using fetchreader As OleDbDataReader = cmdfetch.ExecuteReader(CommandBehavior.CloseConnection)
            While fetchreader.Read()
                If String.Equals(ChkOutBarcode.Text, fetchreader("AssetBarcode"), StringComparison.OrdinalIgnoreCase) Then
                    MessageBox.Show("Item has already been checked out. Please inform the Helpdesk")
                    Me.Close()
                    Return
                End If
            End While
        End Using
    End Using
End Using

1 solution

Solution from the comments:
VB.NET
Using fetchconn As New OleDbConnection(connStr)
    Using cmdfetch As New OleDbCommand()
        cmdfetch.Connection = fetchconn
        cmdfetch.CommandText = "Select AssetBarcode from TrackingInfo Where AssetBarcode = ? AND CheckedOut = True"
        cmdfetch.Parameters.AddWithValue("AssetBarcode", ChkOutBarcode.Text)
        
        fetchconn.Open()
        Using fetchreader As OleDbDataReader = cmdfetch.ExecuteReader(CommandBehavior.CloseConnection)
            While fetchreader.Read()
                If String.Equals(ChkOutBarcode.Text, fetchreader("AssetBarcode"), StringComparison.OrdinalIgnoreCase) Then
                    MessageBox.Show("Item has already been checked out. Please inform the Helpdesk")
                    Me.Close() ' Or your custom method to reset the form...
                    Return
                End If
            End While
        End Using
    End Using
End Using

As suggested in your follow-up question[^], the check in the code is redundant. You can simplify this to:
VB.NET
Using fetchconn As New OleDbConnection(connStr)
    Using cmdfetch As New OleDbCommand()
        cmdfetch.Connection = fetchconn
        cmdfetch.CommandText = "Select CheckedOut from TrackingInfo Where AssetBarcode = ?"
        cmdfetch.Parameters.AddWithValue("AssetBarcode", ChkOutBarcode.Text)
        
        fetchconn.Open()
        
        Dim result As Object = cmdfetch.ExecuteScalar()
        
        If result Is Nothing Then
            MessageBox.Show("This item does not exist. Please inform the Helpdesk")
            Me.Close()
            
        Else If CBool(result) Then
            MessageBox.Show("This item has already been checked out. Please inform the Helpdesk")
            Me.Close()
        End If
    End Using
End Using
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900