Home Get return value from c# function and display in html
Reply: 1

Get return value from c# function and display in html

dginos3
1#
dginos3 Published in 2017-12-08 00:11:03Z

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>
mr.hir
2#
mr.hir Reply to 2017-12-08 05:51:03Z

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>
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.330958 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO