Reputation: 699
I have function in C#, this is :
public int checkUplinkID(string memID, string original_mem)
{
string original_memID = original_mem;
int retValue = 0;
DataTable dt = new DataTable();
using (SqlConnection mCon = new SqlConnection())
{
mCon.ConnectionString = ConStr;
try
{
mCon.Open();
using (SqlCommand mCom = new SqlCommand())
{
mCom.Connection = mCon;
mCom.CommandText = "Get_UplinkID";
mCom.Parameters.AddWithValue("@memID", memID);
mCom.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(mCom);
da.Fill(dt);
if (dt.Rows.Count > 0 && retValue != 1)
{
string n_upid = dt.Rows[0]["uplineId"].ToString();
if (n_upid.ToLower() == original_memID.ToLower())
{
retValue = 1;
}
else if( n_upid.ToLower() != original_memID.ToLower())
{
if (n_upid.ToLower() == "self")
{
retValue = 0;
}
else
{
checkUplinkID(n_upid, original_memID); // its coming back here after returning and its returning '0'
}
}
}
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
mCon.Close();
mCon.Dispose();
}
}
return retValue;
}
Now the problem is after the retValue = 1
it's returning but it's going back to the checkUplinkID
function and making retValue = 0
and returning it.
so what should I do?
And I am calling this function in a aspx.cs
page, like this:
int getchk = iobj.checkUplinkID(memid, memberId);
Where iobj
is object of the class where that method is.
Upvotes: 0
Views: 1369
Reputation: 498
You're forgetting a basic concept of recursion, the BASE case, that is where the recursion stops and go back resolving all the other calls. That's why your recursion didn't stop;
Upvotes: 0
Reputation: 7676
If I understand correctly, the result of the recursive call is not used. You need to store the value:
retValue = checkUplinkID(n_upid, original_memID);
Otherwise, the result of the recursive call is not used and the retValue
of the "outermost" method call remains 0.
Upvotes: 3
Reputation: 174309
You most likely want to assign the return value of checkUplinkID
to retValue
:
else
{
retValue = checkUplinkID(n_upid, original_memID); // its coming back here after returning and its returning '0'
}
Oh, and your catch
block is very bad style:
It doesn't do anything except removing the original stack trace, so you will have no info on where the exception originally originated from. Just remove that catch
block.
Upvotes: 4