Hii RichardSa,
Kindly refer below sample.
HTML
<asp:Label ID="lblElection" runat="server" Text="Exam 2021" />
<br />
<asp:Label ID="voted" runat="server" />
Namespaces
C#
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data.SqlClient
Imports System.Configuration
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.Exam();
}
}
protected void Exam()
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("SELECT COUNT(StudentID) FROM (SELECT StudentID, ExamName FROM Votes GROUP BY ExamName,StudentID )t WHERE ExamName = @ElectionName", con))
{
cmd.Parameters.AddWithValue("@ElectionName", lblElection.Text.Trim());
con.Open();
voted.Text = Convert.ToString(cmd.ExecuteScalar());
con.Close();
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.Exam()
End If
End Sub
Protected Sub Exam()
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand("SELECT COUNT(StudentID) FROM (SELECT StudentID, ExamName FROM Votes GROUP BY ExamName,StudentID )t WHERE ExamName = @ElectionName", con)
cmd.Parameters.AddWithValue("@ElectionName", lblElection.Text.Trim())
con.Open()
voted.Text = Convert.ToString(cmd.ExecuteScalar())
con.Close()
End Using
End Using
End Sub
Output
Exam 2021
9