I want to print all the numbers in the loop .....
How should I do it ?
Please help !

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication5._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat ="server" >
void Page_load()
{
  int counter = 0;
  while (counter <= 10)
     {
       messageLabel.Text = counter.ToString();
       counter++;
     }
}
</script>  

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Loops with C#</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID = "messageLabel" runat = "server"/>
    </div>
    </form>
</body>
</html>

declare an array and it will be okay

The ToString() method is applicable to objects and not to data types. Make use of CType instead and you should be good.

It would probably be easier if you declared a string and then moved it after the loop. I also declared a string variable so you don't have to mess with messageLabel and object notation inside the loop.

This will print all of the numbers between 0 and 10 (inclusive).

int counter = 0;
string str; 
while (counter <= 10)     {       
  str += counter.ToString() + ',';  
  counter++;     
}
str.Remove(str.Length - 1, 1);
messageLabel.Text =str;

while (counter <= 10)
{
messageLabel.Text = counter.ToString();
response.write(counter)
counter++;
}

int counter = 0; 
string append="";
while (counter <= 10)  
   {      
      if  append=""
           append = counter.ToString();   
      else
           append =append & "," & counter.ToString(); 
      end if  
      counter++;  
   }
messageLabel.Text =append;
while (counter <= 10)
{
messageLabel.Text += " :" +  counter.ToString();
counter++;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.