Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
for (int i = 0; i = count.ToString(); i++)


How to define ,,count,,?

What I have tried:

SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "SELECT COUNT(*) FROM roba_usluge";
            Int32 count = (Int32)cmd.ExecuteScalar();

            int top = 50;
            int left = 50;
            for (int i = 0; i = count.ToString(); i++)
            {
                Button button = new Button();
                button.Left = left;
                button.Top = top;
                button.Text = (i + 1).ToString();
                this.Controls.Add(button);
                top += button.Height + 2;
            }
Posted
Updated 5-Apr-18 9:35am

1 solution

Just use:
for (int i = 0; i < count; i++)
Also see: [For loops]

And I would recommend using a using statement like this:
using (SqlConnection conn = new SqlConnection(connString))
    {
        SqlCommand cmd = new SqlCommand(sql, conn);
        ...
    }
 
Share this answer
 
v3
Comments
Goran Bibic 5-Apr-18 15:43pm    
SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=bss;Integrated Security=True");
SqlCommand cmd = new SqlCommand();

cmd.CommandText = "SELECT COUNT(*) FROM roba_usluge";
con.Open();
Int32 count = (Int32)cmd.ExecuteScalar();
con.Close();

int top = 50;
int left = 50;
for (int i = 0; i < count; i++)
{
Button button = new Button();
button.Left = left;
button.Top = top;
button.Text = (i + 1).ToString();
this.Controls.Add(button);
top += button.Height + 2;
}
RickZeeland 5-Apr-18 15:44pm    
That should work, just run it :)
Goran Bibic 5-Apr-18 15:43pm    
Where I wrong here...

SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=bss;Integrated Security=True");
SqlCommand cmd = new SqlCommand();

cmd.CommandText = "SELECT COUNT(*) FROM roba_usluge";
con.Open();
Int32 count = (Int32)cmd.ExecuteScalar();
con.Close();
RickZeeland 5-Apr-18 15:45pm    
What is the error message ?
Goran Bibic 5-Apr-18 15:46pm    
{"ExecuteScalar: Connection property has not been initialized."}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900