I'm new with C#. I would like some help finishing the code to execute a query to return the row number after an order by (currency). The table has user (person name) and currency (their amount of points). The query will order the database by currency then return the row number of that user (string input). I was told it would be more efficient to make a temp table and fill it by order by (not sure how to implement that).
I'm getting error (Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object) on the last line (return result) from the code below that I been working on:
public int overallrank(string user)
{
String sql = "SELECT (SELECT COUNT(*) FROM " + channel + " AS chan2 WHERE chan2.currency > chan1.currency ) as ChanRank FROM " + channel + " as chan1 WHERE chan1.user = ' " + user + "'";
cmd = new SQLiteCommand(sql, myDB);
var result = cmd.ExecuteScalar();
return result == DBNull.Value ? 0 : (int)result;
}
This function is called like so:
int rank = overallrank(user)
User and channel is a string.
Aucun commentaire:
Enregistrer un commentaire