User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the VB.NET section within the Software Development category of DaniWeb, a massive community of 456,530 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,868 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our VB.NET advertiser: Programming Forums
Views: 3814 | Replies: 2
Reply
Join Date: Jul 2005
Posts: 19
Reputation: JavaFish is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
JavaFish JavaFish is offline Offline
Newbie Poster

Question How to display my sql 'Count' result...

  #1  
Oct 9th, 2007
Hi

I am trying to get to grips with querying an access database and displaying the information. I have started with a coun sql command to just return the number of rows but wanted to display this to the screen but each time it shows as null - there are records in my db so either my sql is wrong or a msgbox will not retrieve the data as perhaps the data is already lost by the time I try calling the msgbox?

My code is:

    Function TotalOutOfSpec()

        Dim conn = New Odbc.OdbcConnection
        ' connectionstring using session variable ConnString to connect to user's database
        conn.ConnectionString = Session.Item("ConnString")

        Dim sql = "SELECT count (*) FROM TestResults WHERE OutofSpec = 'True'"

        Dim command As New Odbc.OdbcCommand(sql, conn)
       
        Dim returnValue As Odbc.OdbcDataReader

        Try
            'Open the connection
            conn.Open()
            returnValue = command.ExecuteScalar
            TotalOutOfSpec = returnValue

            'close and dispose of the connection to the database
            conn.close()
            conn.dispose()
            command.Dispose()
            returnValue.Close()
        Catch ex As Exception
        End Try

    End Function

I then try and display it using:

MsgBox("Record count: " & OutOfSpec)

But it always shows as nothing!

I am sure I am missing something obvious(well to everyone else but not me!).

Can anyone help...?

Maybe it is easier to display in a list?

Thanks....
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2005
Location: Braintree, UK
Posts: 1,166
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Rep Power: 7
Solved Threads: 59
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: How to display my sql 'Count' result...

  #2  
Oct 9th, 2007
the ExecuteScalar method returns an object (in VB.NET in VB6 I can't remember perhaps a variant) not a reader. The object is the value in the first column of the first row of the odbc result set.

Also your code doesn't look like valid VB.NET to me. Don't you have to declare the return type of the function? it's looking like VB6 or VBA syntax to me, especially with the brackets missing from the end of ExecuteScalar. Are you in the right forum?
Last edited by hollystyles : Oct 9th, 2007 at 10:02 am.
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote  
Join Date: Jul 2005
Posts: 19
Reputation: JavaFish is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
JavaFish JavaFish is offline Offline
Newbie Poster

Re: How to display my sql 'Count' result...

  #3  
Oct 9th, 2007
Thanks for the reply. I am using VB.net but just messing up my syntax I'm afraid!

I was using executescalar() was initially I was doing a Count, so point taken for my revised sql statement I should change this to ExecuteReader()

I will continue looking at this, but thanks for he response
Last edited by JavaFish : Oct 9th, 2007 at 10:38 am.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb VB.NET Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the VB.NET Forum

All times are GMT -4. The time now is 4:31 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC