asp.net:为什么我得到 System.Collections.Generic.List`1[System. String] 当我尝试从 asp.net 网格视图中的列表打印值时?
asp.net: why am I getting System.Collections.Generic.List`1[System. String] when I try to print values from a list in an asp.net grid view?
我正在尝试创建一个会话来存储特定列的值。所以我创建了这个列表来显示选中的值,但我在另一页 System.Collections.Generic.List`1[System. String] 而不是实际值。
这是第一页的代码:
Private Sub BindGrid()
Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("dbconnection").ConnectionString)
con.Open()
Dim cmd As New SqlCommand("select distinct Name,assignment_id, Description ,cod from assignments
INNER Join crmsLecturerXCrsSection ON crmsLecturerXCrsSection.emp_key = assignments.emp_key
INNER Join CRMSStudentsXCrsSection ON CRMSStudentsXCrsSection.CrsSec_id = crmsLecturerXCrsSection.CrsSec_id
INNER JOIN CRMS_CourseXSection ON CRMS_CourseXSection.CrsSec_id = CRMSStudentsXCrsSection.CrsSec_id
INNER JOIN CRMSECTIONS ON CRMSECTIONS.SEC_ID = CRMS_CourseXSection.SEC_ID
left JOIN crmscourses ON crmscourses.crs_id = CRMS_CourseXSection.crs_id
INNER JOIN CRMS_CourseXSection cs ON CRMS_CourseXSection.SEC_ID = CRMSECTIONS.SEC_ID
INNER JOIN CRMSSEMESTER ON CRMSSEMESTER.SEM_ID = CRMS_CourseXSection.SEM_ID
where CRMSSEMESTER.SEM_ID='1'
and crmsLecturerXCrsSection.emp_key='436' and crmscourses.crs_desc='" + Session("crs") + "'", con)
Dim da As New SqlDataAdapter(cmd)
Dim dt As New DataTable()
da.Fill(dt)
Dim listIDs As New List(Of String)
Dim row As DataRow
For Each row In dt.Rows
listIDs.Add(row("assignment_id"))
Next
Session("assid") = listIDs
cmd.Connection = con
GridView1.DataSource = cmd.ExecuteReader()
GridView1.DataBind()
End Sub
这是第二页:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Label1.Text = Session("assid").ToString()
Dim con1 As New SqlConnection(ConfigurationManager.ConnectionStrings("dbconnection").ConnectionString)
Dim cmd1 As New SqlCommand("select * from assignments where assignment_id ='" + String.Join(",", Session("assid")) + "'", con1)
End Sub
您将 List(Of String)
放入 Session("assid")
,这就是您得到的结果。如果你在上面调用 ToString
,你将得到类型名称,这正是你所看到的,即 "System.Collections.Generic.List'1[System.String]"
。与往常一样,如果您想查看列表中的项目,则需要先将项目取出。具体如何操作取决于您要对这些项目执行的操作。例如,如果您只想显示 comma-delimited 列表中的项目,您可以这样做:
Dim ids = DirectCast(Session("assid"), List(Of String))
Label1.Text = String.Join(", ", ids)
我正在尝试创建一个会话来存储特定列的值。所以我创建了这个列表来显示选中的值,但我在另一页 System.Collections.Generic.List`1[System. String] 而不是实际值。
这是第一页的代码:
Private Sub BindGrid()
Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("dbconnection").ConnectionString)
con.Open()
Dim cmd As New SqlCommand("select distinct Name,assignment_id, Description ,cod from assignments
INNER Join crmsLecturerXCrsSection ON crmsLecturerXCrsSection.emp_key = assignments.emp_key
INNER Join CRMSStudentsXCrsSection ON CRMSStudentsXCrsSection.CrsSec_id = crmsLecturerXCrsSection.CrsSec_id
INNER JOIN CRMS_CourseXSection ON CRMS_CourseXSection.CrsSec_id = CRMSStudentsXCrsSection.CrsSec_id
INNER JOIN CRMSECTIONS ON CRMSECTIONS.SEC_ID = CRMS_CourseXSection.SEC_ID
left JOIN crmscourses ON crmscourses.crs_id = CRMS_CourseXSection.crs_id
INNER JOIN CRMS_CourseXSection cs ON CRMS_CourseXSection.SEC_ID = CRMSECTIONS.SEC_ID
INNER JOIN CRMSSEMESTER ON CRMSSEMESTER.SEM_ID = CRMS_CourseXSection.SEM_ID
where CRMSSEMESTER.SEM_ID='1'
and crmsLecturerXCrsSection.emp_key='436' and crmscourses.crs_desc='" + Session("crs") + "'", con)
Dim da As New SqlDataAdapter(cmd)
Dim dt As New DataTable()
da.Fill(dt)
Dim listIDs As New List(Of String)
Dim row As DataRow
For Each row In dt.Rows
listIDs.Add(row("assignment_id"))
Next
Session("assid") = listIDs
cmd.Connection = con
GridView1.DataSource = cmd.ExecuteReader()
GridView1.DataBind()
End Sub
这是第二页:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Label1.Text = Session("assid").ToString()
Dim con1 As New SqlConnection(ConfigurationManager.ConnectionStrings("dbconnection").ConnectionString)
Dim cmd1 As New SqlCommand("select * from assignments where assignment_id ='" + String.Join(",", Session("assid")) + "'", con1)
End Sub
您将 List(Of String)
放入 Session("assid")
,这就是您得到的结果。如果你在上面调用 ToString
,你将得到类型名称,这正是你所看到的,即 "System.Collections.Generic.List'1[System.String]"
。与往常一样,如果您想查看列表中的项目,则需要先将项目取出。具体如何操作取决于您要对这些项目执行的操作。例如,如果您只想显示 comma-delimited 列表中的项目,您可以这样做:
Dim ids = DirectCast(Session("assid"), List(Of String))
Label1.Text = String.Join(", ", ids)