X



Home>Tutorial>

Adding Forum or Topics Count in a Website

Admin Wednesday, July 22, 2020 ASP.Net

In this article, I will teach you techniques for adding Forum or Topics Count in a website. The language am using is asp.net and vb.net. If you study the image below you will see red notifications telling the user that his/her cart, mail, notifications are empty.
forums or topics count
I have not done anything technical here but rather just used normal SQL statement to get what I want with just few lines of code. The SQL statement I used was SELECT, WHERE and COUNT statements and volla it worked perfectly without having to write 100 lines of code. We used the SQL SELECT statement to retrieve or fetch records from database, WHERE statement is used whenever you want to retrieve or fetch specific records why the COUNT statement as the name implies is used to Count the number of records found. So let's start it without wasting of time. In my case, I declared a function inside a Class file since I will use the function on several occasions so that the site size can be reduced and I assumed you will do the same. Now create a Class file and name it TopicsClass.vb then paste the following codes The VB.Net Code
Public Shared Sub countTopics(tableName As String, column As String,Param As String, lblBadge As Label)
Dim con As New SqlConnection
con.ConnectionString=ConfigurationManager.ConnectionString("myConstr").ToString
If con.State=ConnectionState.Closed Then con.Open
Dim cmd As New SqlCommand("SELECT COUNT (*) FROM " & tableName & " WHERE " & column & "='" & Param & "'", con)
Dim I As Integer = cmd.ExecuteScaler

lblBadge.Text = I

If I>0 Then

lblBadge.ForeColor = Color.Green
     Else
lblBadge.ForeColor = Color.Red

End If
cmd.Close
con.Close
End Sub
Having created the class file and pasted or typed the above codes, you need to call this function from your page load and at the same time specifying all the function's parameters. To call the function, open your code window then Click on General ComboBox and Select Page Events, Click on Declaration ComboBox then select Load.
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
TopicsClass.countTopics("tblprogramming","forum","vb",lblBadge)

End Sub

| | Views(1009)

See Demo | Download File


Add your comment. Indicate code using code tag


Words: 0 | Chars: 0 | Reading Duration: 0



Images Attachment




Emmason Integratded Services(2017-2024)
All Rights Reserved
Designed and Maintained By Emmason Integrated Services