I have a table called Product(the ProductCode field data type is varchar ):
ID ProductName ProductCode
1 aaa 0001
2 bbb 0002
3 ccc 0004
4 ddd 0006
5 eee 0007
6 fff 0008
My problem is : IF current the ProductCode field value is ‘0006’.I would like to generate
a value that is ‘0009’ .
public string GenerateNumber(string codeNumber)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("select count(1) from Product");
strSql.Append(" where ProductCode=@codeNumber");
SqlParameter[] parameters = {
new SqlParameter("@codeNumber", SqlDbType.NChar,8)
};
parameters[0].Value = codeNumber;
bool existInDatabase = SQLHelper.Exists(strSql.ToString(), parameters);
if (existInDatabase)
{
this.GenerateNumber((Convert.ToInt32(codeNumber) + 1).ToString().PadLeft(4, '0'));
}
return (Convert.ToInt32(codeNumber) + 1).ToString().PadLeft(4, '0');
}
Call this function some thing like :GenerateNumber(“0006”)
The result shuold return 0009 ,but is not !
Can anyone help me out ?Thanks ,I am a newbie!
Using recursion in asp.net for this seems like too much of an overhead.
You could do this best by writing a stored procedure to GenerateNextProductCode that used a common table expression to generate this value.
The reason this code is failing is you are not returning the result of your recursion.
Correct it like this: