locked
I'M WRITING A CODE TO AUTOMATIC GENERATE STAFF NUMBER. AM STOCK PLEASE HELP ME OUT... RRS feed

  • Question

  • User1527485495 posted

    I'M WRITING A CODE TO AUTOMATIC GENERATE STAFF NUMBER. AM STOCK PLEASE HELP ME OUT...

    // FORMAT OF THE STAFF NUMBER (FMCEB/HR/1043) THAT IS THE LAST NUMBER IN MY DATABASE


    // READING FROM DATABASE
    var a = (from m in db.Staffs orderby m.StaffId descending select m).Take(1).SingleOrDefault();
    if (a != null)
    {

    string stid = a.StaffNo.ToString();
    if (stid.Length == 9)
    {
    string output = (stid.Substring(9, 3));
    int num = Convert.ToInt32(output) + 1;
    newstaffno = "FMCEB/HR/" + num.ToString();


    }
    }
    else
    {
    int id = 1;
    string year = DateTime.Now.Year.ToString();
    newstaffno = "FMCEB/HR/" + year + id.ToString();
    }

    PLEASE HELP A BROTHER OUT!!!

    Thursday, August 3, 2017 4:25 PM

Answers

  • User-1508012637 posted

    Hi Dreal1,

    i am write a code for an application that i am developing now , in d process of developing i notice the code is not working the way i want.

    Please tell me more detail about why your code is not working the way you want(you got an error or you didn’t get the value from database? ... ). 

    And, what i what the code to execute is pick from the last staff number from my database and automatic generate a new one an pass it to d designated textbox

    After you get the value of “num” variable, you could use the following statement to pass the value to your TextBox:
    txtbox.Text = "FMCEB/HR/" + num.ToString();

    If you have any other questions, please feel free to contact me any time.

    Best Regards
    Velen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 7, 2017 1:58 AM

All replies

  • User-1508012637 posted

    Hi Dreal1,

    FORMAT OF THE STAFF NUMBER (FMCEB/HR/1043) THAT IS THE LAST NUMBER IN MY DATABASE

    Since the last number in your Staff number is four digits. So the following code:

    string output = (stid.Substring(9, 3));

    would be changed to:

    string output = (stid.Substring(9, 4));

    Right?

    Besides, I’m not very clear about where you get stuck. If you want to generate the Staff number. Please tell me about the rule you generate the number for me to help you further.

    Best Regards
    Velen

    Friday, August 4, 2017 2:38 AM
  • User1527485495 posted

    thanks boss.
    i am write a code for an application that i am developing now , in d process of developing i notice the code is not working the way i want.
    below highlighted code that i think is the issue:
    ****
    // i'm stock here 


    int num = Convert.ToInt32(output) + 1;
    newstaffno = "FMCEB/HR/" + num.ToString();
    ***
    And, what i what the code to execute is pick from the last staff number from my database and automatic generate a new one an pass it to d designated textbox
    below is the format of the code and how i want it to wrk.

    example 1:
    this is the last staff number in my FMCEB/HR/1234 in my database so i want it to automatic read d last staff number from the data n generate a new one like dis FMCEB/HR/1235
    like dis FMCEB/HR/1235. 


    below is the sample of my code

    // FORMAT OF THE STAFF NUMBER (FMCEB/HR/1043) THAT IS THE LAST NUMBER IN MY DATABASE


    // READING FROM DATABASE

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    var a = (from m in db.Staffs orderby m.StaffId descending select m).Take(1).SingleOrDefault();
    if (a != null)
    {

    string stid = a.StaffNo.ToString();
    if (stid.Length == 9)
    {
    string output = (stid.Substring(9, 3));
    ****
    // i'm stock here and dont know hoe 
    int num = Convert.ToInt32(output) + 1;
    newstaffno = "FMCEB/HR/" + num.ToString();
    ***

    }
    }
    else
    {
    int id = 1;
    string year = DateTime.Now.Year.ToString();
    newstaffno = "FMCEB/HR/" + year + id.ToString();
    }
    Friday, August 4, 2017 5:23 PM
  • User1120430333 posted

    And, what i what the code to execute is pick from the last staff number from my database and automatic generate a new one an pass it to d designated textbox
    below is the format of the code and how i want it to wrk.

    You could use an auto incremented PK for a SQL Server table to keep the count for you. You call it the tblCount. you add a dummy record to the tblCount, which generates the next count number + 1. You can return the next number to use back to your program, which can all be done with simple Stored Procedure.

    I have done it using EF too, which is simple logic not all this complications I see in your code.

     

    Friday, August 4, 2017 8:26 PM
  • User-1508012637 posted

    Hi Dreal1,

    i am write a code for an application that i am developing now , in d process of developing i notice the code is not working the way i want.

    Please tell me more detail about why your code is not working the way you want(you got an error or you didn’t get the value from database? ... ). 

    And, what i what the code to execute is pick from the last staff number from my database and automatic generate a new one an pass it to d designated textbox

    After you get the value of “num” variable, you could use the following statement to pass the value to your TextBox:
    txtbox.Text = "FMCEB/HR/" + num.ToString();

    If you have any other questions, please feel free to contact me any time.

    Best Regards
    Velen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 7, 2017 1:58 AM