0

I'm having issue with creating product code in recursion. What I want to do is:

I enter code 1000

-If code exist in database regenerateCode(string code)

-else insert into database

Code:

(...) if(codeExist) CodeTB.Text = regenerateCode(string toParse); //toParse = 1000

       string regenerateCode(string toParse)
                {
                    string helper = "";

                    int parseCode = int.Parse(toParse);
                    helper = new string('0', 4 - parseCode.ToString().Length);
                    helper += parseCode + 1;


                    using (SqlConnection conn = new SqlConnection(cString.c_String))
                    {
                        conn.Open();
                        using (SqlCommand cmd = new SqlCommand("SELECT Product.productID FROM Product " +
                          "WHERE Product.PLU = '" + helper + "' ", conn))
                        {
                            using (SqlDataReader rdr = cmd.ExecuteReader())
                            {
                                if (rdr.HasRows)
                                {
    // if code still exist in database, regenerate it
                                    regenerateCode(helper);
                                }
                                else
                                {
//else return code
                                    return helper;
                                }
                            }
                        }
                    }
                    return helper;
                }

Actually it works fine with example: 1000 (exist) 1001 (exist) 1002 (not exist, insert), but when code = 1002, it goes into line with else {return helper;} and have no idea why going again to regenerateCode() method..

Any ideas?

user13657
  • 745
  • 3
  • 17
  • 36
  • You said that when code = 1002, it goes into else clause... it *should* do... you also said '1002 (not exist)'... therefore `rdr.HasRows` = false. What exactly *is* your problem? – Sheridan Sep 02 '13 at 10:47
  • I have in database existing codes: 1001, 1002, 1003. So next one should be 1004. When i put '1001' in code text box it goes exactly there: regenerateCode(helper); //1001, regenerateCode(helper); //1002, regenerateCode(helper); //1003, regenerateCode(helper); //1004 - at this moment string code is ok and it should return 1004 code but it doesnt, just goint to: return helper; //last line, and again goes into regenerateCode(helper); //1001 regenerateCode(helper); //1002 – user13657 Sep 02 '13 at 10:55
  • Are you just trying to find the highest 'code' number in the database, or are there 'gaps' in the code number sequence that you are trying to find? – Sheridan Sep 02 '13 at 11:01
  • Here is video which show exactly what's my problem. Check please second 25. In database i have codes 1697, 1698, 1699, 1700. Next free is 1701. In CodeTB I put 1697. So problem is that, when it reach 'return' , goes again into method and regenerate code. Movie: http://www.youtube.com/watch?v=Spb9ruDiAUU&feature=youtu.be – user13657 Sep 02 '13 at 11:05
  • I understand your problem. I believe that there is a *much* better solution for you. Please answer my previous question. – Sheridan Sep 02 '13 at 11:10
  • Im not trying to get highest code, just first nearest free. Also code should always have 4 digits (from 0001 to 9999) – user13657 Sep 02 '13 at 11:13
  • Yes, it is. But ID is converted into 4 digits code. – user13657 Sep 02 '13 at 11:17

1 Answers1

1

It happens because you do nothing with the return value. You pass helper to the recursion, but do not place the return value anywhere. your method should look like this:

string regenerateCode(string toParse)
{
  string helper = "";

  int parseCode = int.Parse(toParse);
  helper = new string('0', 4 - parseCode.ToString().Length);
  helper += parseCode + 1;

  using (SqlConnection conn = new SqlConnection(cString.c_String))
  {
    conn.Open();
    using (SqlCommand cmd = new SqlCommand("SELECT Product.productID FROM Product " +
      "WHERE Product.PLU = '" + helper + "' ", conn))
    {
      using (SqlDataReader rdr = cmd.ExecuteReader())
      {
        // Return the next code that doesn't exist
        return rdr.HasRows ? regenerateCode(helper) : helper;
      }
    }
  }
}

Also, remember that while string are reference types, they are immutable. which means that a string that is passed as an argument would not change like a normal array. see: How are strings passed in .NET?

Community
  • 1
  • 1
CKII
  • 1,439
  • 14
  • 26