-1

I have a database with some info, and I need to display the number of rows to webpage. I tried getting the number according to How to retrieve the count of the number of rows in asp.net mvc from sql database? : code in Model:

   public int getNumber()
   {
       using(var con = new SqlConnection("ConnectionString")
       {
           con.Open();
           string query = "SELECT COUNT(*) FROM AspNetUsers"; // table name - AspNetUsers

           using (var cmd = new SqlCommand(query, con))
           {
              return (int)cmd.ExecuteScalar();
           }
       }
   }

How do I display the number to the view? Creating object of the model class doesn't seem to work, since it need Ilogger. I now wonder whether I should put this code in a controller.

3
  • What makes you think it needs an ILogger? Am I correct in assuming this is ASP.NET MVC? Please tag your question with the appropriate framework. Commented Jul 7, 2022 at 15:54
  • @mason The constructor of the Model class requires ILogger<ModelName>. I have tried to create another blank constructor, but got error - there can only be one Commented Jul 7, 2022 at 17:31
  • I don't know why a model would need an ILogger....and I don't know what error you would get if you added an additional constructor: C# classes can have more than 1 model. When you run into issues, make sure you fully describe them. You haven't provided the code for the model class you're trying to work with, so when you say "doesn't seem to work" in your question, it's not clear at all what you've tried and specifically what isn't working about it. Commented Jul 7, 2022 at 20:31

1 Answer 1

0

Assuming you drop in a button, and a text box to display the count?

Say, this markup:

        <asp:Label ID="Label1" runat="server" Text="Rows in database = "></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        <asp:Button ID="Button1" runat="server" Text="Show table row count" OnClick="Button1_Click" />

And this code:

    protected void Button1_Click(object sender, EventArgs e)
    {
        using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
        {
            using (SqlCommand cmdSQL = new SqlCommand("SELECT COUNT(*) FROM tblHotelsA", conn))
            {
                conn.Open();
                int? RowCount = (int?)cmdSQL.ExecuteScalar();
                TextBox1.Text = RowCount.ToString();
            }
        }
        
    }

And we get this:

enter image description here

Now, above is web forms, but the idea is really the same - you do a count(*) on that table - get the value and then shove it into a text box, or use in code, or do whatever you want.

Sign up to request clarification or add additional context in comments.

11 Comments

Why use a nullable int? Won't the result always be a not-null int?
Sure, but if the table don't exist, then no value is returned - that line of code thus wouldn't compile if I used a non nullible int
If the table doesn't exist, an exception will be thrown, and thus no assignment would even happen. It wouldn't return a NULL value. So yes, this absolutely will compile with a non-nullable int. Since the RowCount variable can never be NULL in this code, it makes no sense to store it in a nullable int.
Not a huge deal in this case because it then gets converted to a string. But imagine for a moment that this was a properly encapsulated method that didn't mix UI and database concerns, and instead returned the value as a nullable int in a method. Now all downstream down has to deal with the nullable int even though it will never actually be null. More complicated code for no reason. In both that case and the code you've shown, the only thing that really makes sense here is a non-nullable int.
If I was new to the language, like the asker presumably is here, I'd be very confused why someone is using a non-nullable int, and I might just conclude that somehow that could get a null value, when in reality there's absolutely no way that can happen. Let's set a good example in the code we write and not make changes that don't add any value and only add confusion.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.