Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can I create a line of code in c# that I will use to give every new product that I add to my product table?I am using MySQL database.The first two should be a letter followed by a number and back slash and then the word POS. for example PC01/POS.
This is what I have tried so far although it's not what I really need.

What I have tried:

//auto product number
static string IncrementID(string startValue, int numNonDigits)
{
    string nonDigits = startValue.Substring(0, numNonDigits);
    int len = startValue.Length - numNonDigits;
    int number = int.Parse(startValue.Substring(numNonDigits));
    number++;
    if (number >= Math.Pow(10, len)) number = 1; // start again at 1
    return String.Format("{0}{1:D" + len.ToString() + "}", nonDigits, number);
}
private void Genarate_productcode()
{
    product_code.Text = IncrementID("QIEpl/PO/0000009", 9); // produces QIEpl/PO/0000010 // C00011 PCI0001
}  
Posted
Updated 14-Feb-18 8:19am
Comments
phil.o 14-Feb-18 7:53am    
/ is a slash. A backslash is \ :)

In addition to solution 1 by CHill60[^] i'd strongly suggest to "create" product id with string prefix on MySQL server side. See: How to make MySQL table primary key auto increment with some prefix - Stack Overflow[^]
 
Share this answer
 
I personally would not generate the ID in the C# code I would use the MySQL database to generate the id for me: See MySQL :: MySQL 5.7 Reference Manual :: 3.6.9 Using AUTO_INCREMENT[^]

I certainly would never use a method that relies on me incrementing the "previous" number as it will not work in a multi-user environment.

Once you have that numeric Id you can display it however you want e.g. (Not tested!)
C#
String.Format("PC{0}//POS", id.ToString(D2));
although I would also not limit my output to 1 to 99 .. use D5 at least.

If you really, really want to store the text version on the database then you could use a trigger (MySQL: AFTER INSERT Trigger[^]) to capture the id and generate the text version

[EDIT after OP comment]
Try something like the following (note again, untested!)
SQL
CREATE TRIGGER product_id
 AFTER INSERT
 ON test FOR EACH ROW
 	UPDATE test set textid = concat('PC', LPAD(NEW.id, 5, '0'), '/POS') WHERE id=NEW.id;
Explanation:
The trigger is called product_id and it fires after a row is, or rows are, inserted into the table test
There is only one statement in the trigger so I haven't used BEGIN ... END, but you have already demonstrated that you know how to do that with your code in the comments - I'm just being lazy (and demonstrating a point). (Note, if you do have more than one statement you will need to change the delimiter when creating your trigger - see the documentation)
The trigger is going to UPDATE the table test using NEW.id. The special table NEW contains ... the new rows. You can't update it. There is another special table OLD which contains the old values too.

I'm sorry I haven't been able to test this properly and this isn't the correct place to tutor you further, but at least now you have some search terms to help your further research.

[EDIT - further code samples after OP comments. This solution is hidden in the reams of comments below].
Both of these will work
SQL
DELIMITER //

CREATE TRIGGER product_id
 AFTER INSERT
 ON test 
 BEGIN
      String.Format("PC{0}//POS", id.ToString(D2));
 END; //
DELIMITER ;
or
CREATE TRIGGER product_id
 AFTER INSERT
 ON test 
      String.Format("PC{0}//POS", id.ToString(D2));

Specifically, if you want to include a BEGIN and END you MUST use DELIMITER as well!
 
Share this answer
 
v3
Comments
harristars 14-Feb-18 10:42am    
let me try this too
Maciej Los 14-Feb-18 14:04pm    
Completely agree!
5ed!
harristars 14-Feb-18 15:27pm    
i have tried i am unable to go about it .i have studied the link plus the code u provide..but where exactly should i place the MYSQL Trigger in the code
CHill60 14-Feb-18 16:58pm    
Look at the link, triggers go onto the database, not in the code
harristars 14-Feb-18 17:14pm    
i am actually at the database.i know how to set the auto increment but this one where by i am suppose to have my product_id look like this PC01/POS. i am unable
That's a whacky question, but here ya go:

C#
public static string IncrementID2(string startValue, int numNonDigits)
{
    int max = 999999999;
    string[] parts = startValue.Split('/');
    // is it safe to assume that the third splitted string is the number you're looking for?
    int number;
    if (Int32.TryParse(parts[2], out number))
    {
        number++;
    }
    else
    {
        if the number doesn't parse correctly, set it to zero
        number = 0;
    }
    number = (number > max) ? 1 : number;
    return string.Format("PC{0:D9}/POS", number);
}
 
Share this answer
 
Comments
harristars 14-Feb-18 10:33am    
let me try it and see if it will work
#realJSOP 14-Feb-18 10:37am    
Of course it will work, assuming you pass it the same string you're passing to the original method.
harristars 21-Feb-18 3:38am    
if the number doesn't parse correctly, set it to zero
number = 0;


on this line there is a problem
CHill60 21-Feb-18 4:16am    
That is a comment in the code. Looks like the comment marker is missing, it should be
//if the number doesn't parse correctly, set it to zero;
harristars 23-Feb-18 5:50am    
private void Genarate_productcode()
{
product_code.Text = IncrementID2(" ", 9);

}
is this the correct way to call the function generating the product_code

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