Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将数据库中的多个数据项检索到一个标签中_C#_Asp.net_Sql Server - Fatal编程技术网

C# 将数据库中的多个数据项检索到一个标签中

C# 将数据库中的多个数据项检索到一个标签中,c#,asp.net,sql-server,C#,Asp.net,Sql Server,我想从数据库中检索多个值并显示在一个标签上 我目前的数据库设计是: courseid scholarshipid course -------------------------------- 1 1 maths 2 1 english 3 1 science scholarshipid schName -----------------------

我想从数据库中检索多个值并显示在一个标签上

我目前的数据库设计是:

courseid  scholarshipid  course
--------------------------------
    1           1        maths
    2           1        english
    3           1        science

scholarshipid   schName    
-----------------------
      1         moon 
      2         start
      3         light
我希望将此信息检索到单个标签中,例如

Maths, English, Science
然而,现在我设法找回了其中一个,那就是数学

这是我在代码隐藏中的代码:

protected void btnShowDetails_Click(object sender, EventArgs e)
{
    string Selectedid = GVFBAcc.SelectedRow.Cells[1].Text;//get user id
    int selectedIdtoPass = Convert.ToInt32(Selectedid);

    courseBLL getRecord = new courseBLL();
    addcourse courseRetrieve = new addcourse();
    courseRetrieve = getRecord.getCourseDetail(selectedIdtoPass);

    lbCourse.Text = courseRetrieve.course1 + "," + courseRetrieve.course1;
}
protected void btnShowDetails_Click(object sender, EventArgs e)
{
    string Selectedid = GVFBAcc.SelectedRow.Cells[1].Text;//get user id
    // Make sure this is the scholarshipid or the whole thing will
    // not work.
    int selectedIdtoPass = Convert.ToInt32(Selectedid);

    courseBLL courseRetriever = new courseBLL();

    // Remember you will get a list here
    List<addcourse> courses = courseRetriever.getCourseDetail(selectedIdtoPass);

    // This will take the list and separate the course property with a comma and space
    var courseNames = string.Join(", ", courses);

    // Now assign it to the text property
    lbCourse.Text = courseNames;
}
在我的课程中

public addcourse getCourseDetail(int idcourse)
{
        addcourse userObj = new addcourse();
        courseDAL objtoPass = new courseDAL();
        userObj = objtoPass.displayCourseInfo(idcourse);
        return userObj;
}
在我心中

public addcourse displayCourseInfo(int idcourse)
{
        string strCommandText = "Select course from course where schokarshipid = @schokarshipid";

        SqlCommand cmd = new SqlCommand(strCommandText, conn);
        cmd.Parameters.AddWithValue("@scholarshipid", idcourse);

        conn.Open();

        SqlDataReader myReader = cmd.ExecuteReader();
        addcourse userObj = new addcourse();

        while (myReader.Read())
        {
            int sID = Convert.ToInt32(myReader["courseID"].ToString());

            string course = myReader["course"].ToString();

            userObj = new addcourse(sID, course);
        }

        myReader.Close();
        return userObj;
}
编辑:

public addcourse displayCourseInfo(int idcourse)
{
        var courses = new List<addcourse>();
        string strCommandText = "Select statement here"

        SqlCommand cmd = new SqlCommand(strCommandText, conn);
        cmd.Parameters.AddWithValue("@scholarshipid", idcourse);

        conn.Open();

        SqlDataReader myReader = cmd.ExecuteReader();

        while (myReader.Read())
        {
            int sID = Convert.ToInt32(myReader["scholarshipid"].ToString());

            string course = myReader["course"].ToString();

            courses.Add(new addcourse(sID,course));
        }

        myReader.Close();

        return courses;
}

为此,我删除了一些字段,以免混淆您创建for循环,然后将其添加到正在使用的变量中。您应该从getCourseDetail和displayCourseInfo返回一个列表,如果addCourse具有Course属性,则可以执行此操作:

lbCourse.Text = string.Join(", ",  courses.Select(x => x.Course));
如果属性名称不同,则将x.Course更改为该名称

所以在你的读者中,你会这样做

    var courses = new List<addcourse>();
    while (myReader.Read())
    {
        int sID = Convert.ToInt32(myReader["courseID"].ToString());

        string course = myReader["course"].ToString();


        courses.Add( new addcourse(sID, course)) ;
   } 
   return courses;
所以,您应该修复这个问题,并确保获得所需的列,因为现在它只获取课程列,并且有语法错误。使用ManagementStudio直接对数据库运行查询。一旦你的查询工作,复制粘贴它,并用一个参数替换where部分,就像你现在拥有的那样

编辑

看来你还坚持着这一点,所以我会给你一个更详细的答案;然而,您应该真正学习一些基本的编程概念。不管怎样,这是:

您的代码隐藏:

protected void btnShowDetails_Click(object sender, EventArgs e)
{
    string Selectedid = GVFBAcc.SelectedRow.Cells[1].Text;//get user id
    int selectedIdtoPass = Convert.ToInt32(Selectedid);

    courseBLL getRecord = new courseBLL();
    addcourse courseRetrieve = new addcourse();
    courseRetrieve = getRecord.getCourseDetail(selectedIdtoPass);

    lbCourse.Text = courseRetrieve.course1 + "," + courseRetrieve.course1;
}
protected void btnShowDetails_Click(object sender, EventArgs e)
{
    string Selectedid = GVFBAcc.SelectedRow.Cells[1].Text;//get user id
    // Make sure this is the scholarshipid or the whole thing will
    // not work.
    int selectedIdtoPass = Convert.ToInt32(Selectedid);

    courseBLL courseRetriever = new courseBLL();

    // Remember you will get a list here
    List<addcourse> courses = courseRetriever.getCourseDetail(selectedIdtoPass);

    // This will take the list and separate the course property with a comma and space
    var courseNames = string.Join(", ", courses);

    // Now assign it to the text property
    lbCourse.Text = courseNames;
}
您的BLL:

// See here you are retuning a list
// But you need to pass the scholarshipid so you can get the courses for that scholarshipid
public List<addcourse> GerCourses(int scholarshipId)
{
        courseDAL courseRetriever = new courseDAL();

        // Get the list
        List<addcourse> courses = courseRetriever.GetCourses(scholarshipId);
        return courses;
}
您的DAL:

// See here you are retuning a list
// But you need to pass the scholarshipid so you can get the courses for that scholarshipid
public List<addcourse> GetCourses(int scholarshipId)
{
        // Make sure your query is correct, see you have spelling mistakes
        // it should be scholarshipid not schokarshipid. it has to match the column in your database
        // Make sure your table is called course and it has 3 columns: 1. courseid 2. course
        // 3. scholarshipid or this query will not work
        string strCommandText = "Select courseid, course from course where scholarshipid = @scholarshipId";

        SqlCommand cmd = new SqlCommand(strCommandText, conn);
        cmd.Parameters.AddWithValue("@scholarshipid", scholarshipId);

        conn.Open();

        SqlDataReader myReader = cmd.ExecuteReader();

        // create a list so we can hold all the courses
        List<addcourse> userObjs = new List<addcourse>();

        // This will read one row at a time so keep reading until there are no more records
        while (myReader.Read())
        {
            int sID = Convert.ToInt32(myReader["courseid"].ToString());

            string course = myReader["course"].ToString();

            addcourse userObj = new addcourse(sID, course);

            // add it to the list
            userObjs.Add(userObj);
        }

        myReader.Close();

        // return the list
        return userObjs;
}
还有一些提示:

给你的方法起个更有意义的名字。看看我给他们的名字,现在它读起来像英语,而且很容易理解。 你的课程应该叫做课程。addcourse听起来像是一个ActionAddCourse,如果您正在添加课程,这可能是一个很好的方法名称。类名称和方法名称也使用Pascal表示法。参数、参数和局部变量使用驼峰式大小写。 了解如何使用Visual Studio调试器。在VisualStudio上观看一些YouTube视频。
GetCourseDetail中发生了什么?你当前的lbCourse输出是什么?Text??我已经编辑了代码,请看@un luckycurrent output是:Math,mathsdo你有什么例子吗?我使用vb.net,如果你想检查你的查询,你可以转换它,你似乎正在调用courseid,id是:1 course=maths,尝试删除where条件,它将输出数据库中的所有数据。您能建议我如何执行列表吗?与您现在调用它的方式相同。只需按照我的代码和返回列表中的那些方法我得到了一个错误,说它不能隐式转换从列表到BLL近返回课程我已经添加了编辑代码以上,请帮助采取我的坏。我还有一些其他字段要检索,但我没有相应地删除它。基本上,courseID是另一个表的外键。因此,当courseID=1时,它将在另一个表中显示所有这些课程和其他详细信息