Computing.Net > Forums > Programming > Counting rows in a DB, VB.NET

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Counting rows in a DB, VB.NET

Reply to Message Icon

Name: mking (by mkingrey)
Date: September 15, 2005 at 10:45:14 Pacific
OS: xp
CPU/Ram: xp
Comment:

Hello,

I have a web application that is linked up to an Access database. The table is called "Messages" and I want to display how many messages are unread on my webform. I need to count the number of rows in my database where: colRead = "No." Any suggestions?



Sponsored Link
Ads by Google

Response Number 1
Name: StuartS
Date: September 15, 2005 at 11:33:35 Pacific
Reply:

Use the following SQL statement:

SELECT COUNT(*) AS [RecordCount] FROM Messages WHERE colRead = No

The variable RecordCount will contain the number of matching records.

Stuart


0

Response Number 2
Name: mking (by mkingrey)
Date: September 15, 2005 at 12:08:51 Pacific
Reply:

I have the SQL statement that I need I just don't know what I need to use to access the database in my vb code. This is what I have but

Dim SQLResult As Object
Dim connStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Inetpub\wwwroot\Organizer1.0\DB\organizer.mdb"
Dim dbConnection As OleDbConnection = New OleDbConnection(connStr)
Dim SQL As New OleDbCommand
dbConnection.Open()
SQL.Connection = dbConnection
SQL.CommandText = "SELECT COUNT * FROM Messages WHERE Read = No"
SQLResult = SQL.ExecuteNonQuery
Summary = SQLResult
lblMessages.Text = "You have " & Summary & " unread messages"

This is generally what I use (either ExecuteNonQuery or ExecuteScalar) to update and query my databases but it does not work with a COUNT statement.


0

Response Number 3
Name: SN
Date: September 16, 2005 at 08:07:16 Pacific
Reply:

Looks like you did Read = No instead of colRead = No. Is that on purpose?

Also, you should use ExecuteScalar, not ExecuteNonQuery. ExecuteScalar indicates you want just one value back (in this case, a count), whereas execute nonquery implies you're not expecting anything back.

Good luck,
-SN


0

Response Number 4
Name: mking (by mkingrey)
Date: September 16, 2005 at 09:27:04 Pacific
Reply:

I thought ExecuteScalar returned the value of field in the first row returned, this is why you can't use a COUNT statement with .ExecuteScalar.


0

Response Number 5
Name: SN
Date: September 16, 2005 at 10:28:13 Pacific
Reply:

That's exactly why you need to do execute scalar...count will return a 1x1 table with the only value being the count you asked for. ExecuteScalar will return that value.

Good luck,
-SN


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Counting rows in a DB, VB.NET

Return # of Rows in Web Form,VB.NET www.computing.net/answers/programming/return-of-rows-in-web-formvbnet/12410.html

rinning .reg in vb.net www.computing.net/answers/programming/rinning-reg-in-vbnet/13813.html

Add Data in table via VB.Net?? www.computing.net/answers/programming/add-data-in-table-via-vbnet/12525.html