Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6566025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:10:04+00:00 2026-05-25T14:10:04+00:00

So I’m trying to take data from a database (patientid in the format pXX)

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T14:10:04+00:00Added an answer on May 25, 2026 at 2:10 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.