Counting and Displaying the number of rows

timothy2l

Well-known member
Joined
Jul 3, 2003
Messages
61
I want to show the number of registered users on my homepage. What is the easiest way to do this? using VB

I have this much
sql = "Count * FROM Graduates"

Thanks
 
Since you want to display them you should select what you want to display.

Dim strSQL as string = ""

declare a connection
Dim connection as new SQLConnection()
connection.ConnectionString = "Your connection string"

declare a command
Dim command as new SQLCommand()
command.Connection = connection
command.CommandType = CommandType.Text although by default
command.CommandText = "Select * from Graduates"
or select whatever columns you want...

declare a data adapter to fetch the data
Dim daGraduates as new SQLDataAdapter(command)

declare a table to hold your data
Dim dtGraduates as new DataTable()

Fill the table with data
daGraduates.Fill(dtGraduates)

Now you have a table with your data..

You can count the number of graduates by
Dim count as integer = dtGraduates.Rows.Count

You can display them in a datagrid
Datagrid1.DataSource = dtGraduates

If you are doing a web application then add
Datagrid1.DataBind()

Hope this helps,
 
Thanks, ill try this out. Can you also explain to me how to send a Checkbox value to a True/False database field? I keep trying to do it in a SQL statement, but I dont know what checkbox property to use.
Thanks again
 
Ya i saw that post of yours, but still I didnot understand the question could you elaborate more on that ....
 
Ok, I have an online form that gathers information from people, such as address, phone number, email, etc. There is a checkbox list on the form as well. The information is saved in an Access DB. I can get all of the information coming from a text box to save in the DB, but i dont know how to save whether the checkboxes are checked or not. The DB field is set to Yes/No. The Checkboxlist contains about 5 items, each having its own field in the DB.
 
Did you try something like:
If checkbox1.checked Then
mytable.rows(rownumber)(thefieldyouwant) = Yes
Else
mytable.rows(rownumber)(thefieldyouwant) = No
End IF
 
Inserting a checkbox value:
What if I dont know the row number....I want this value to insert with the rest of the form. It should insert all records across the board for that particular user.

Counting records:
I tried what you gave me but I got and "object reference" error for the line:

daGraduates.Fill(dtGraduates)

I checked over my code the best I could and I believe it matched your format exactly. I would rather display it as a Label then in a datagrid. I did this with a session variable and the firstname field. When the user is logged on, it says "hello, firstname". This is what I would like to do here.
Thanks for your help so far.
 
Concerning the Object reference
Are you sure you declared the table dtGraduates as

New Datatable()

And the Dataadapter daGraduates as

New DataAdapter()
 
Back
Top