All pastes #243628 Raw Edit

Someone

public text v1 · immutable
·published 2006-11-08 18:57 UTC
rendered paste body
the issue is, i am trying to count the number of Males in my database table, which is easy as pie in java. but in asp, weve only jus started out.  
 
while(myDataReader.Read()==true) 
{ 
if (Convert.ToString(myDataReader["Sex"]) == "M") 
{ 
 
Label1.Text = Label1.Text; 
} 
} 
easy stuff... 
but
it prints out 1111111 rather than 8
in the label
thats what i dont get
benji  18:42:49
 
"Label1.Text = Label1.Text; " wtf is that supposed todo? 
Richard Smythe  18:43:11
 
add one to label 1 whenever it finds out that that person is Male 
benji  18:43:24
 
You're assigning it to itself... 
Richard Smythe  18:43:58
 
then how do i add one to it 
benji  18:44:39
 
I'm guessing you want something like Label1.Text = (int.Parse(Label1.Text) + 1) 
Richard Smythe  18:45:27
 
no dice 
:(
benji  18:45:41
 
do you get a compile error? 
Richard Smythe  18:45:53
 
 Compiler Error Message: CS0029: Cannot implicitly convert type 'int' to 'string' 
benji  18:46:08
 
ah use .ToString 
or cheat and use Label1.Text = "" + (int.Parse(Label1.Text) + 1)
but .ToString is more memory efficient
Richard Smythe  18:46:55
 
i tried that 
Label1.Text = (int.ToString(Label1.Text) + 1);
 Compiler Error Message: CS0120: An object reference is required for the nonstatic field, method, or property 'int.ToString(string)'
benji  18:47:18
 
no 
Label1.Text = (int.Parse(Label1.Text) + 1).ToString() should work
I don't have a c# compiler to hand
and I'm far more familiar with java
Richard Smythe  18:48:56
 
it doesnt like this 
Label1.Text = (int.Parse(Label1.Text) + 1).ToString();
:(
benji  18:49:01
 
what error? 
Richard Smythe  18:50:38
 
i cant find it lol 
benji  18:50:58
 
that compiles here 
Richard Smythe  18:51:05
 
 Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
 
Exception Details: System.FormatException: Input string was not in a correct format. 
benji  18:51:23
 
Sounds like you put a non-number in the input field, or it started off blank or something 
Richard Smythe  18:51:40
 
should i have the label text set to 0 b4? 
benji  18:52:07
 
yes, do a try catch to set it to 0 if it can't be parsed or something 
Richard Smythe  18:52:40
 
  while(myDataReader.Read()==true) 
        { 
            Label1.Text = "0"; 
            if (Convert.ToString(myDataReader["Sex"]) == "M") 
            { 
 
                Label1.Text = (int.Parse(Label1.Text) + 1).ToString(); 
                
            } 
             
i put label.text = 0
and all it returns is