开发者

need the required checkboxes to the checked when searching patients in a database

i need two check boxes to be checked or unchecked with reference to a database. im new to programming and what is required is that when a patient is searched it will display whether a patient would like to receive spam and if they are a donor or not. whats happening with my code is when i search a patient i get an error "invalid cast exception was unhandled by user code" something about converting a string to boolean here is my code

< % input type="checkbox" name="spam" value="Y" <%if (String.Compare((String) ViewData["spam"],"Y",false)==0) Response.Write("checked"); %>

ive been told the if statement is wrong and been given this:

if (mystring=="Y") myboolean=true; else myboolean=false;

however I'm not sure what mystring or myboolean is

my controller is:

    public void patientInit()
    {

        hospitalSQLEntities db = new hospitalSQLEntities();
        ViewData["bloodtypeList"] = db.bloodtypes.ToList();
        ViewData["patientid"] = "";
        ViewData["patientname"] = "";
        ViewData["bloodtype"] = 0;
        ViewData["gender"] = "";
        ViewData["spam"] = "";
        ViewData["organs"] = "";
        ViewData["address"] = "";
        ViewData["message"] = "";
    }

    public void patientNewButtPressed()
    {
        if (Request.Params["submitter"] == "New Patient")
        {
            ViewData["patientid"] = "(System Specified)";
            ViewData["patientname"] = "";
            ViewData["bloodtype"] = 0;
            ViewData["gender"] = "";
            ViewData["spam"] = "";
            ViewData["organs"] = "";
            ViewData["address"] = "";
            ViewData["message"] = "";
        }

    }

    public void patientSearchByID()
    {
        hospitalSQLEntities db = new hospitalSQLEntities();
        String formpatientid = Request.Params["searchpatientid"];
        patient mypatient = null;
        bool foundship = true;
        try
        {
            mypatient = db.patients.Single(u => u.patientid == formpatientid);
        }
        catch (Excep开发者_如何学运维tion e)
        {
            foundship = false;
        }
        if (foundship)
        {
        ViewData["patientid"] = "" + mypatient.patientid;
        ViewData["patientname"] = mypatient.patientname;
        ViewData["bloodtype"] = mypatient.bloodtype;
        ViewData["gender"] = mypatient.gender;
        ViewData["spam"] = mypatient.spam;
        ViewData["organs"] = mypatient.organs;
        ViewData["address"] = mypatient.address;
        ViewData["message"] = "patient " + mypatient.patientid +" found";
        }
        else
        {
            ViewData["patientid"] = "(System Specified)";
            ViewData["patientname"] = "";
            ViewData["bloodtype"] = 0;
            ViewData["gender"] = "";
            ViewData["spam"] = "";
            ViewData["organs"] = "";
            ViewData["address"] = "";
            ViewData["message"] = "Cannot find patient with ID " + formpatientid;
        }
    }



    public String patientSearchByName()
    {
        hospitalSQLEntities db = new hospitalSQLEntities();
        patient mypatient = null;
        List<patient> patientlist = null;
        string mypatientname = Request.Params["searchpatientname"];
        patientlist = db.patients.Where(u => u.patientname.Contains(mypatientname)).ToList();
        if (patientlist.Count() == 0)
            ViewData["message"] = "Matching name not found!";
        else if (patientlist.Count() == 1)
        {
            mypatient = db.patients.Single(u => u.patientname.Contains(mypatientname));
            ViewData["patientid"] = "" + mypatient.patientid;
            ViewData["patientname"] = mypatient.patientname;
            ViewData["bloodtype"] = mypatient.bloodtype;
            ViewData["gender"] = mypatient.gender;
            ViewData["spam"] = mypatient.spam;
            ViewData["organs"] = mypatient.organs;
            ViewData["address"] = mypatient.address;
            ViewData["message"] = "patient " + mypatient.patientname +" found";
        }
        else
        {
            return mypatientname ;
        }
        return "";
    }


idk if its a typo or worth mentioning this

< % input type="checkbox" name="spam" value="Y" <%if (String.Compare((String) ViewData["spam"],"Y",false)==0) Response.Write("checked"); %>

should probably look like

<input type="checkbox" name="spam" value="Y" <%if (String.Compare((String) ViewData["spam"],"Y",false)==0) Response.Write("checked"); %> />
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜