I have crated one function to return the result of executed SQL query as follows:
EDITED :
public int GetChips(int points, string username)
{
int chip = 0;
string getChips = "SELECT Chips from tbl_UserInfo where UserName =' " + username + " '";
con = new MySqlConnection(conString);
con.Open();
MySqlCommand cmd = new MySqlCommand(getChips, con);
MySqlDataReader chips = cmd.ExecuteReader();
while (chips.Read())
{
chip = chips.GetInt32(0);
if (chip > points)
{
if (points == 5000)
{
chip = chip - 5000;
}
else if (points == 10000)
{
chip = chip - 10000;
}
}
}
con.Close();
return chip;
}
It returns chip’s value as 0. This code does not go in ‘while’ condition.
What can be the problem ?
How can I solve this ?
An answer to your edited question:
Are you sure the select statement is returning any values at all? You have a space inside your
' "and" '"so it will look for' Rohan 'in stead of'Rohan'. TryOr better yet, like Jon suggested, use parameterized queries!