Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I need to increment a product code (smthg lyk " BK001 ") which is in a database.
so i wrote a sql statement and got the MAX value from the table n tried to increment it using the logic from ( Alphanumeric increment in C# By Sandip Dalvi Tung) ... bt wen retrive the max value n send it to the below class it dsnt work is shows the same amount which i gt from the sql statement as the MAX value.

i passed the value i gt from the sql statement ("BK001") as a string to the NxtKeyCode method


****increment class code********************************
public string NxtKeyCode(string KeyCode) 
{
    byte[] ASCIIValues = ASCIIEncoding.ASCII.GetBytes(KeyCode) ;
    int StringLength = ASCIIValues.Length ;
    bool isAllZed = true;
    bool isAllNine = true;
    //Check if all has ZZZ.... then do nothing just return empty string.
    for(int i =0; i < StringLength-1; i++)
    {
        if(ASCIIValues[i] != 90)
        {
            isAllZed  = false;    
            break;
        }
    }
    if(isAllZed && ASCIIValues[StringLength-1] == 57)
    {
        ASCIIValues[StringLength-1] = 64;
    }
    
    // Check if all has 999... then make it A0
    for(int i =0; i < StringLength; i++)
    {
        if(ASCIIValues[i] != 57)
        {
            isAllNine = false;    
            break;
        }
    }
    if(isAllNine)
    {
        ASCIIValues[StringLength-1] = 47;
        ASCIIValues[0] = 65;
        for(int i =1; i < StringLength-1; i++)
        {
            ASCIIValues[i] = 48;
        }
    }
    
    for(int i = StringLength; i > 0; i--)
    {
        if(i-StringLength == 0)
        {
            ASCIIValues[i-1] +=1;
        }
        if(ASCIIValues[i-1] == 58)
        {
            ASCIIValues[i-1] = 48;
            if(i-2 ==-1)
            {
                break;
            }
            ASCIIValues[i-2] += 1;
        }
        else if(ASCIIValues[i-1] == 91)
        {
            ASCIIValues[i-1] = 65;
            if(i-2 ==-1)
            {
                break;
            }
            ASCIIValues[i-2] += 1;
    
        }
        else
        {
            break;
        }
    
    }    
    KeyCode = ASCIIEncoding.ASCII.GetString(ASCIIValues);
    return KeyCode; 
}
public string NxtKeyCod(string KeyCode) 
{ 
    //int startint = KeyCode.IndexOf("0123456789",0,1);
    StringBuilder sb = new StringBuilder();
    //Regex digitregex = new Regex("^[A-Z])");
    //KeyCode = digitregex.Replace(KeyCode, ""); 
        
    return KeyCode;

***********************************************************************
please can anyone help me im stuck with a project

thank you


[edit:rjm]Placed the code within <pre></pre> tags to improve readability[/edit:rjm]
Posted
Updated 7-Feb-10 1:49am
v3

I don't know how you tested/debugged this but when I pass "BK001" to the NxtKeyCode() method it returns the string "BK002".
 
Share this answer
 
v2
Yup Alphanumeric increment in C#[^] The code works fine
 
Share this answer
 
v2
Are you sure you're using the NxtKeyCode() function and not the one below called NxtKeyCod() (note no 'e') which does nothing but return what is passed to it?

From Richard M: I didn't think this obvious question worth asking, but I am willing to bet you are right.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900