在数据库中搜索患者时需要选中所需的复选框

时间:2011-09-09 05:10:19

标签: c# asp.net entity-framework

我需要两个复选框来参考数据库进行检查或取消选中。我不熟悉编程,所需要的是,当搜索患者时,它将显示患者是否想要接收垃圾邮件以及他们是否是捐赠者。 我的代码发生了什么,当我搜索病人时,我得到一个错误“用户代码未处理无效的强制转换异常”这里有关于将字符串转换为布尔值的内容,这是我的代码

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

我被告知if语句是错误的并且已经给出了这个:

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

但是我不确定mystring或myboolean是什么

我的控制器是:

    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 (Exception 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 "";
    }

1 个答案:

答案 0 :(得分:0)

idk如果它是一个错字或值得一提

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

应该看起来像

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