?
Solved

searching function

Posted on 2008-01-29
3
Medium Priority
?
211 Views
Last Modified: 2010-04-23
using VB.NET 05, I have implemented a form and some controls. I have some difficulties with the following function:
the class "DataValidator"
 Public Shared Function InvoiceExists(ByVal search As frmSearch) As Boolean
        Dim connection As OleDbConnection = GetConnection()
        Dim countStatement As String
        countStatement = "SELECT Count(BillsID) FROM Invoices WHERE studentID =?"
        Dim countCommand As New OleDbCommand(countStatement, connection)
        countCommand.Parameters.AddWithValue("?", search.txtStudentID.Text.Trim)
        Try
            connection.Open()
            Dim searchCount As Integer = countCommand.ExecuteNonQuery
            If searchCount > 0 Then
                Return True
            Else
                Return False
            End If
        Catch ex As Exception
            Return False
        Finally
            connection.Close()
        End Try
    End Function

Even though, I enter an existing billsID, I still get a non existence message, from the following class "frmInvoice"
if DataValidator.InvoiceExists then
        frmBills.Show
else
       messageBox.show("hughbkihnnjnnkk, "non existence")
end if

can you help me.
0
Comment
Question by:Arnaud241
  • 2
3 Comments
 
LVL 11

Accepted Solution

by:
PlatoConsultant earned 1000 total points
ID: 20774091
For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. For all other types of statements, the return value is -1.

Use Execute Scaler Mathod to get a single value

Executes the query and returns the first column of the first row in the result set returned by the query. All other columns and rows are ignored.

Dim searchCount As Integer = countCommand.ExecuteScaler
0
 
LVL 11

Expert Comment

by:PlatoConsultant
ID: 20774098
Exexute Non Query Method  returns For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. For all other types of statements, the return value is -1.


check detail Documentation in MSDN

Just Replace Execute Non Query With the Execute Scaler Method
0
 
LVL 48

Expert Comment

by:Jorge Paulino
ID: 20775689
Try this way

Public Shared Function InvoiceExists(ByVal search As frmSearch) As Boolean
        Dim connection As OleDbConnection = GetConnection()
        Dim countStatement As String
        countStatement = "SELECT Count(BillsID) As Total FROM Invoices WHERE studentID = '" & search.txtStudentID.Text.Trim & "'"
        Dim countCommand As New OleDbCommand(countStatement, connection)
        Try
            connection.Open()
            Dim searchCount As Integer = countCommand.ExecuteNonQuery()
            If searchCount > 0 Then
                Return True
            Else
                Return False
            End If
        Catch ex As Exception
            Return False
        Finally
            connection.Close()
        End Try
    End Function
0

Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
How to fix display issue, screen flickering issue when I plug in power cord to the machine. Before I start explaining the solution lets check out once the issue how it looks like after I connect the power cord. most of you also have faced this…
If you are looking for an automated solution for backup single or multiple Office 365 user mailboxes to Outlook data file, then you can use Kernel Office 365 Backup & Restore tool. Go through the video to check out the steps to backup single or mult…
Suggested Courses
Course of the Month6 days, 6 hours left to enroll

589 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