Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I got some problems in retrieving a values from a function here's my code.

C#
public int _countContacts(int count)
        {
            string query = @"select count(Contact ID) as count from Contacts";

            dt = _result(query);

            for (int x = 0; x < dt.Rows.Count; x++)
            {
                count = Convert.ToInt16(dt.Rows[x]["count"].ToString());
            }

            return count;
        }


And here my code in other form

C#
private void btnRefresh_Click(object sender, EventArgs e)
        {
            //this is the wrong part
            label1.Text = func._countContacts();
        }
Posted
Comments
Vani Kulkarni 22-Jul-12 23:35pm    
What is problem you are facing? Are you experiencing any error?

It seems your function definition does not make sense. You should try to use only the return integer, not the parameter as:
C#
public int _countContacts()
{
  int count = 0;
  string query = @"select count(Contact ID) as count from Contacts";

  dt = _result(query);
  
  for (int x = 0; x < dt.Rows.Count; x++)
  {
    count = Convert.ToInt16(dt.Rows[x]["count"].ToString());
  }
 
  return count;
}

One other thing I noticed in your code that may be an error (or it may be correct and I don't truly understand your algorithm): In the line count = Convert.ToInt16... you are overwriting the value of count for every row in your data table. Is this wanted? Better code may be this:
C#
for (int x = 0; x < dt.Rows.Count; x++)
{
  count += Convert.ToInt16(dt.Rows[x]["count"].ToString());
}
 
Share this answer
 
v3
Comments
newbie011292 23-Jul-12 1:00am    
thank you sir, ahm whats wrong? i just want to get the value from my query ")
First thing is that you don't need to provide parameter in function, you can declare variable inside function as it has given in solution 1. Second thing is that your function is returning integer value so you need to write following code on button_Click:
C#
int i = func._countContacts();
label1.Text = i.ToString()


Good luck.
 
Share this answer
 
v2
Comments
newbie011292 23-Jul-12 1:00am    
thanks sir.
Raje_ 23-Jul-12 1:19am    
you are welcome!

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