简体   繁体   English

在列表和会话中使用类asp.net c#

[英]using class in list and session asp.net c#

I'm trying to get data from the database and put it in a list. 我正在尝试从数据库中获取数据并将其放在列表中。 Then, i want to create dropdown list with the received data. 然后,我想用接收到的数据创建下拉列表。

The db table: db表:

ID
projectName

The code: 编码:

public void ProjectsList()
{
    List<ProjectsListClass> ProjectsList=new List<ProjectsListClass>(); //list of projects
    ProjectsListClass projectDetails=new ProjectsListClass();//1 project details

    //creating session of projects table
    using (SqlConnection conn = new SqlConnection(mainDBConn))//connect to the database table
    {
        SqlCommand sqlProjectsList = new SqlCommand("SELECT * FROM dbo.Projects ORDER BY projectName", conn);

        conn.Open();

        SqlDataReader rsProjectsList = sqlProjectsList.ExecuteReader();

        if (rsProjectsList.HasRows && rsProjectsList.Read())//there is a project, insert into the array
        {
            projectDetails.ProjectsListProjectID = Convert.ToInt16(rsProjectsList["ID"]);
            projectDetails.ProjectsListProjectName = rsProjectsList["projectName"].ToString();
            ProjectsList.Add(projectDetails);
        }
        Session["ProjectsList"] = ProjectsList;

        rsProjectsList.Close();
        conn.Close();
    }  
}

and in the other page: 并在另一页中:

    List<ProjectsListClass>projectsArray=new List<ProjectsListClass>();
    projectsArray=(List<ProjectsListClass>)Session["ProjectsList"];

    foreach(var project in projectsArray)
    {
        ListItem l = new ListItem(project.ProjectsListProjectID.ToString()
            , project.ProjectsListProjectName.ToString()
            , true);
        projectIDDropDownList.Items.Add(l);
    }

i get the error: 我得到错误:

Object reference not set to an instance of an object. 你调用的对象是空的。

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. 异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例。

Source Error: 源错误:

Line 20: foreach(var project in projectsArray) - this one in red 第20行:foreach(projectsArray中的var project)-红色

Line 21: { Line 22: ListItem l = new ListItem(project.ProjectsListProjectID.ToString() 第21行:{第22行:ListItem l =新的ListItem(project.ProjectsListProjectID.ToString()

What should i do? 我该怎么办?

I think there's a few things you need to be mindful of. 我认为您需要注意一些事项。

Although I don't know you're particular requirements, generally speaking I don't believe storing the your projectlist in session is the best approach. 尽管我不知道您有什么特殊要求,但总的来说,我不认为在会话中存储项目列表是最好的方法。 Generally I would have a Project Class with a GetProjectList() method that return a List of Projects. 通常,我会有一个带有GetProjectList()方法的Project类,该方法返回项目列表。 You could then call this method from your "other" page and bind to your DropDownList. 然后,您可以从“其他”页面调用此方法并绑定到DropDownList。

Also, I believe you may have your projectDetails declaration in the wrong place. 另外,我相信您可能在错误的位置放置了projectDetails声明。 If you want to return a list of projects try something like this (I'm a VB developer but I've had a go at the C#). 如果您想返回项目列表,请尝试类似的操作(我是VB开发人员,但已经使用过C#)。

 SqlCommand sqlProjectsList = new SqlCommand("SELECT * FROM dbo.Projects ORDER BY projectName", conn); conn.Open(); SqlDataReader rsProjectsList = sqlProjectsList.ExecuteReader(); if (rsProjectsList.HasRows)//there is a project, insert into the array { while rsProjectsList.Read() { ProjectsListClass projectDetails=new ProjectsListClass(); projectDetails.ProjectsListProjectID = Convert.ToInt16(rsProjectsList["ID"]); projectDetails.ProjectsListProjectName = rsProjectsList["projectName"].ToString(); ProjectsList.Add(projectDetails); } } rsProjectsList.Close(); conn.Close(); 

To bind to your dropdown list, try the following code, 要绑定到您的下拉列表,请尝试以下代码,

 projectIDDropDownList.DataSource = Project.GetProjects(); projectIDDropDownList.DataBind(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM