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
IDENTITYfield).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)