开发者

Trying to retrieve data and then add one on the max string (int-string conversions)

开发者 https://www.devze.com 2023-04-04 01:57 出处:网络
So I\'m trying to take data from a database (patientid in the format pXX) and when I add a new patient to the database through the website it automatically finds the max patient id and then adds 1 to

So I'm trying to take data from a database (patientid in the format pXX) and when I add a new patient to the database through the website it automatically finds the max patient id and then adds 1 to it. So if the max patient id is p12 it makes the new patient p13.

What I've done so far is; isolate two numbers in a string (using sub string method), convert them to an int, find the max number, convert back to a string, test if the string length is smaller then 2, add 1 zero to it, and then I added a p. This is my code;

    public void patientInsert()
    {
        hospitalSQLEntities db = new hospitalSQLEntities();
        int newid = 0;
        string mynumberstring = "patientid".Substring(1, 2);
        int patientid1 = Convert.ToInt32(mynumberstring);
        string p = "p";
        if (db.patients.Count() == 0)
            newid = 1;
        else
            newid = db.patients.Max(u => patientid1) + 1;
            newid = Convert.ToString(newid);
            newid = newid < 2, + "0"; 
            newid = (p + patientid1);
        patient newpatient = new patient();
        newpatient.patientid = Convert.ToString(Request.Params["newid"]);
        newpatient.doctorno = Convert.ToInt16(Request.Params["doctorno"]);
        newpatient.wardno = Request.Params["wardno"];
        newpatient.patientname = Request.Params["patientname"];
        newpatient.address = Request.Params["address"];
        newpatient.gender = Request.Params["gender"];
        newpatient.bloodtype = Request.Params["bloodtype"];
        newpatient.spam = Convert.ToBoolean(Request.Params["spam"]);
        newpatient.organs = Convert.ToBoolean(Request.Params["organs"]);
        db.AddTopatients(newpatient);
        db.SaveChanges();
    }

Am I doing this right? I know I'm tripping up in the conversion from int to string and again on adding the 0 and the p but I'm just a bit lost.

p.s. first time trying to program so go easy if it开发者_运维问答's a silly mistake!


First off, instead of storing the patientid as PXX, I would just store the XX (in the database, just store the integer. If they all have a P in the beginning, that would just be a display issue (show a P and any needed leading zeros - only when displaying it). If you do this, then the database could even handle the numbering for you (in SQL Server, its called an IDENTITY field).

Second, here is how I would change your code - this is not to make it work, just to show you some mistakes

(NOTE: I put a comment of "LOOK HERE" where changes were made)

public void patientInsert()
{
    hospitalSQLEntities db = new hospitalSQLEntities();
    int newid = 0;
    //LOOK HERE: "patientid" - removed quotes (assuming patientid is a variable)
    //LOOK HERE: Substring - removed second parameter (isn't needed, and anyway, once you get higher than 100, you'll need to change your code from 2 to 3)
    string mynumberstring = patientid.Substring(1);
    int patientid1 = Convert.ToInt32(mynumberstring);
    //LOOK HERE: removed p variable (too simple - waste of space)
    //string p = "p";
    if (db.patients.Count() == 0)
    {
        newid = 1;
    }
    else
    {
        //LOOK HERE: are you trying to get the max id from the database?  if so, then this wont work. you need something like this:
        //newid = db.patients.Max(u => patientid1) + 1;
        newid = db.patients.Max(u => Int32.Parse(u.patientid.Substring(1))) + 1;
        //LOOK HERE: newid is defined as int. It can't be changed to string.
        //newid = Convert.ToString(newid);
        //newid = newid < 2, + "0"; 
        //newid = (p + patientid1);
    }
    patient newpatient = new patient();
    //LOOK HERE: newid is a variable, not a page parameter
    //newpatient.patientid = Convert.ToString(Request.Params["newid"]);
    //LOOK HERE: Look at String.format - this is your key to the leading zeros
    newpatient.patientid = String.Format("P{0:00}", newid);
    newpatient.doctorno = Convert.ToInt16(Request.Params["doctorno"]);
    newpatient.wardno = Request.Params["wardno"];
    newpatient.patientname = Request.Params["patientname"];
    newpatient.address = Request.Params["address"];
    newpatient.gender = Request.Params["gender"];
    newpatient.bloodtype = Request.Params["bloodtype"];
    newpatient.spam = Convert.ToBoolean(Request.Params["spam"]);
    newpatient.organs = Convert.ToBoolean(Request.Params["organs"]);
    db.AddTopatients(newpatient);
    db.SaveChanges();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消