In addition to the error, which the other solutions have already explained, your code has another problem.
The
Key
parameter passed to the function is used to select the records from you table where the
SNo
column is equal to the
Key
parameter.
The function is then called recursively, passing the
SNo
column from the resultset as the
Key
parameter. But since you already know that the
SNo
column is equal to the
Key
parameter, you've just introduced infinite recursion.
Unless there are no matching records in your database, you will end up with a
StackOverflow
exception.
I suspect you either meant to
SELECT
a different column, or filter on a different column. Selecting a column which is filtered to be equal to a value you already know serves no purpose.
Also, don't use string concatenation to build your query; use a parameterized query. String concatenation can and will lead to
SQL Injection[
^] vulnerabilities.
And there's no point having an empty
Try..Catch
block at the end of the method.