dustinos3
dustinos3

Reputation: 974

Get return value from c# function and display in html

I am trying to display the count of a table from my database. I have a function that returns the count of the rows. I'm new to c# and I'm not sure how to call the function in my html so that it displays the returned value.

Here's my c# function that I got from How to count the number of rows from sql table in c#?:

public int Get_Count(object sender, EventArgs e)
{
    {
        string stmt = "SELECT COUNT(*) FROM dbo.listing";
        int count = 0;

        using (SqlConnection thisConnection = new SqlConnection("Data Source=DATASOURCE"))
        {
            using (SqlCommand cmdCount = new SqlCommand(stmt, thisConnection))
            {
                thisConnection.Open();
                count = (int)cmdCount.ExecuteScalar();
            }
        }
        return count;
    }
} 

And here's the HTML line that I would like to display the returned count:

<p class="lead text-muted block text-center"><strong>Get_Count()</strong> results found</p>

Upvotes: 2

Views: 3498

Answers (1)

mr.hir
mr.hir

Reputation: 564

in page load event

     protected void Page_Load(object sender, EventArgs e)
    {
        this.DataBind();
    }

    public int Get_Count()
   {
      {
          string stmt = "SELECT COUNT(*) FROM dbo.listing";
          int count = 0;

          using (SqlConnection thisConnection = new SqlConnection("Data Source=DATASOURCE"))
            {
               using (SqlCommand cmdCount = new SqlCommand(stmt, thisConnection))
                 {
                    thisConnection.Open();
                    count = (int)cmdCount.ExecuteScalar();
                 }
           }
          return count;
       }
    }

then

    <p class="lead text-muted block text-center"><strong><%# Get_Count() %></strong> results found</p>

Upvotes: 5

Related Questions