1

目前正在修复我的代码,我正在添加一个搜索功能..但是当我调试它时,出了点问题并弹出一个错误。

这是我的代码:

public ViewResult Index(string currentFilter, string searchString, int? page) {
    if(Request.HttpMethod=="GET") {
        searchString=currentFilter;
    }
    else {
        page=0;
    }

    ViewBag.CurrentFilter=searchString;

    var connString=ConfigurationManager.ConnectionStrings["ApplicantDB"].ConnectionString;
    List<Applicant> instructors=new List<Applicant>();

    using(var conn=new SqlConnection(connString)) {
        conn.Open();

        var query=new SqlCommand(
            "SELECT TOP 50 APPLICANT_ID, APPLICANT_Lastname, APPLICANT_FirstName, APPLICANT_MiddleName, APPLICANT_Address, APPLICANT_City"+
            " FROM APPLICANT", conn);

        var reader=query.ExecuteReader();

        int currentPersonID=0;
        Applicant currentInstructor=null;

        while(reader.Read()) {
            var personID=Convert.ToInt32(reader["APPLICANT_ID"]);

            if(personID!=currentPersonID) {
                currentPersonID=personID;

                if(currentInstructor!=null) {
                    instructors.Add(currentInstructor);
                }

                currentInstructor=new Applicant();
                currentInstructor.APPLICANT_ID=Convert.ToInt32(reader["APPLICANT_ID"].ToString());
                currentInstructor.APPLICANT_Lastname=reader["APPLICANT_Lastname"].ToString();
                currentInstructor.APPLICANT_FirstName=reader["APPLICANT_FirstName"].ToString();
                currentInstructor.APPLICANT_MiddleName=reader["APPLICANT_MiddleName"].ToString();
                currentInstructor.APPLICANT_Address=reader["APPLICANT_Address"].ToString();
                currentInstructor.APPLICANT_City=reader["APPLICANT_City"].ToString();
            }

        }

        if(!String.IsNullOrEmpty(searchString)) {
            currentInstructor=
                instructors.Where(
                    s => 
                        s.APPLICANT_Lastname.ToUpper().Contains(searchString.ToUpper())
                        ||
                        s.APPLICANT_FirstName.ToUpper().Contains(searchString.ToUpper())
                    ).FirstOrDefault();

            currentInstructor.APPLICANT_ID=Convert.ToInt32(reader["APPLICANT_ID"].ToString());
            currentInstructor.APPLICANT_Lastname=reader["APPLICANT_Lastname"].ToString();
            currentInstructor.APPLICANT_FirstName=reader["APPLICANT_FirstName"].ToString();
            currentInstructor.APPLICANT_MiddleName=reader["APPLICANT_MiddleName"].ToString();
            currentInstructor.APPLICANT_Address=reader["APPLICANT_Address"].ToString();
            currentInstructor.APPLICANT_City=reader["APPLICANT_City"].ToString();
        }

        if(currentInstructor!=null) {
            instructors.Add(currentInstructor);
        }

        reader.Close();
        conn.Close();
    }

    int pageSize=10;
    int pageNumber=(page??0);
    return View(instructors.ToPagedList(pageNumber, pageSize));
}

错误出现在这一行..说:

不存在数据时尝试读取无效。

描述:

当前 Web 请求执行期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详情:

System.InvalidOperationException:不存在数据时尝试读取无效。

currentInstructor=
    instructors.Where(
        s =>
            s.APPLICANT_Lastname.ToUpper().Contains(searchString.ToUpper())
            ||
            s.APPLICANT_FirstName.ToUpper().Contains(searchString.ToUpper())
        ).FirstOrDefault();

currentInstructor.APPLICANT_ID=Convert.ToInt32(reader["APPLICANT_ID"].ToString());
currentInstructor.APPLICANT_Lastname=reader["APPLICANT_Lastname"].ToString();
4

1 回答 1

1

应该像下面这样...

public ViewResult Index(string currentFilter, string searchString, int? page)
{
    if (Request.HttpMethod == "GET")
        searchString = currentFilter;
    else
        page = 0;
    ViewBag.CurrentFilter = searchString;    
    var connString = ConfigurationManager.ConnectionStrings["ApplicantDB"].ConnectionString;
    List<Applicant> instructors = new List<Applicant>();
    using (var conn = new SqlConnection(connString))
    {
        conn.Open();
        var query = new SqlCommand("SELECT TOP 50 APPLICANT_ID, APPLICANT_Lastname, APPLICANT_FirstName, APPLICANT_MiddleName, APPLICANT_Address, APPLICANT_City" +
                " FROM APPLICANT", conn);
        int currentPersonID = 0;
        Applicant currentInstructor = null;

        using (var reader = query.ExecuteReader())
        {
            while (reader.Read())
            {
                var personID = Convert.ToInt32(reader["APPLICANT_ID"]);
                if (personID != currentPersonID)
                {
                    currentPersonID = personID;
                    if (currentInstructor != null)
                        instructors.Add(currentInstructor);
                    currentInstructor = new Applicant();
                    currentInstructor.APPLICANT_ID = Convert.ToInt32(reader["APPLICANT_ID"].ToString());
                    currentInstructor.APPLICANT_Lastname = reader["APPLICANT_Lastname"].ToString();
                    currentInstructor.APPLICANT_FirstName = reader["APPLICANT_FirstName"].ToString();
                    currentInstructor.APPLICANT_MiddleName = reader["APPLICANT_MiddleName"].ToString();
                    currentInstructor.APPLICANT_Address = reader["APPLICANT_Address"].ToString();
                    currentInstructor.APPLICANT_City = reader["APPLICANT_City"].ToString();
                }
                if (!String.IsNullOrEmpty(searchString))
                {
                    currentInstructor =
                        instructors.Where(
                            s => s.APPLICANT_Lastname.ToUpper().Contains(searchString.ToUpper()) ||
                                    s.APPLICANT_FirstName.ToUpper().Contains(searchString.ToUpper())).
                            FirstOrDefault();
                    currentInstructor.APPLICANT_ID = Convert.ToInt32(reader["APPLICANT_ID"].ToString());
                    currentInstructor.APPLICANT_Lastname = reader["APPLICANT_Lastname"].ToString();
                    currentInstructor.APPLICANT_FirstName = reader["APPLICANT_FirstName"].ToString();
                    currentInstructor.APPLICANT_MiddleName = reader["APPLICANT_MiddleName"].ToString();
                    currentInstructor.APPLICANT_Address = reader["APPLICANT_Address"].ToString();
                    currentInstructor.APPLICANT_City = reader["APPLICANT_City"].ToString();
                }
            }
        }
        if (currentInstructor != null)
        {
            instructors.Add(currentInstructor);
        }
    }

    int pageSize = 10;
    int pageNumber = (page ?? 0);
    return View(instructors.ToPagedList(pageNumber, pageSize));
}
于 2013-05-15T01:10:29.720 回答